From 0edd3aca29fad38211a87b44d187c5851814876d Mon Sep 17 00:00:00 2001 From: Matt Fellows <53900+mefellows@users.noreply.github.com> Date: Mon, 19 Feb 2024 19:56:05 +1100 Subject: [PATCH] feat: support status code matcher via pactffi_response_status_v2 (#486) --- native/consumer.cc | 24 +++++++++++++++++++----- src/consumer/index.ts | 4 ++-- src/ffi/types.ts | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/native/consumer.cc b/native/consumer.cc index 9d1b999e..a1dcd4e4 100644 --- a/native/consumer.cc +++ b/native/consumer.cc @@ -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(); @@ -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().Uint32Value(); - unsigned short status = info[1].As().Uint32Value(); + std::string status = info[1].As().Utf8Value(); - bool res = pactffi_response_status(interaction, status); + bool res = pactffi_response_status_v2(interaction, status.c_str()); return Napi::Boolean::New(env, res); } diff --git a/src/consumer/index.ts b/src/consumer/index.ts index 57ff5517..52fe4391 100644 --- a/src/consumer/index.ts +++ b/src/consumer/index.ts @@ -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 diff --git a/src/ffi/types.ts b/src/ffi/types.ts index 9adcadab..333177cb 100644 --- a/src/ffi/types.ts +++ b/src/ffi/types.ts @@ -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,