Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support status code matcher via pactffi_response_status_v2 #486

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions native/consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1163,9 +1163,23 @@ Napi::Value PactffiWithMultipartFile(const Napi::CallbackInfo& info) {
*
* * `status` - the response status. Defaults to 200.
*
* To include matching rules for the status (only statusCode or integer really makes sense to use), include the
* matching rule JSON format with the value as a single JSON document. I.e.
*
* ```c
* const char* status = "{ \"pact:generator:type\": \"RandomInt\", \"min\": 100, \"max\": 399, \"pact:matcher:type\":\"statusCode\", \"status\": \"nonError\"}";
* pactffi_response_status_v2(handle, status);
* ```
* See [IntegrationJson.md](https://github.com/pact-foundation/pact-reference/blob/master/rust/pact_ffi/IntegrationJson.md)
*
* # Safety
* The status parameter must be valid pointers to NULL terminated strings.
*
*
* C interface:
*
* bool pactffi_response_status(InteractionHandle interaction, unsigned short status);
* bool pactffi_response_status_v2(InteractionHandle interaction,
* const char *status);
*/
Napi::Value PactffiResponseStatus(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Expand All @@ -1178,14 +1192,14 @@ Napi::Value PactffiResponseStatus(const Napi::CallbackInfo& info) {
throw Napi::Error::New(env, "PactffiResponseStatus(arg 0) expected a number");
}

if (!info[1].IsNumber()) {
throw Napi::Error::New(env, "PactffiResponseStatus(arg 1) expected a number");
if (!info[1].IsString()) {
throw Napi::Error::New(env, "PactffiResponseStatus(arg 1) expected a string");
}

InteractionHandle interaction = info[0].As<Napi::Number>().Uint32Value();
unsigned short status = info[1].As<Napi::Number>().Uint32Value();
std::string status = info[1].As<Napi::String>().Utf8Value();

bool res = pactffi_response_status(interaction, status);
bool res = pactffi_response_status_v2(interaction, status.c_str());

return Napi::Boolean::New(env, res);
}
Expand Down
4 changes: 2 additions & 2 deletions src/consumer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ export const makeConsumerPact = (
filename,
mimePartName
) === undefined,
withStatus: (status: number) =>
ffi.pactffiResponseStatus(interactionPtr, status),
withStatus: (status: number | string) =>
ffi.pactffiResponseStatus(interactionPtr, JSON.stringify(status)),
withPluginRequestInteractionContents: (
contentType: string,
contents: string
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export type FfiConsumerFunctions = {
file: string,
partName: string
): void;
pactffiResponseStatus(handle: FfiInteractionHandle, status: number): boolean;
pactffiResponseStatus(handle: FfiInteractionHandle, status: string): boolean;
pactffiWritePactFile(
handle: FfiPactHandle,
dir: string,
Expand Down
Loading