Skip to content

Commit

Permalink
feat: add pactffi_given_with_params for params
Browse files Browse the repository at this point in the history
Will eventually replace pactffi_given_with_param with pactffi_given_with_params.

See pact-foundation/pact-js#848 for background
  • Loading branch information
mefellows committed Jan 18, 2024
1 parent 6141f27 commit 2fada2a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion native/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "pactffiNewInteraction"), Napi::Function::New(env, PactffiNewInteraction));
exports.Set(Napi::String::New(env, "pactffiUponReceiving"), Napi::Function::New(env, PactffiUponReceiving));
exports.Set(Napi::String::New(env, "pactffiGiven"), Napi::Function::New(env, PactffiGiven));
exports.Set(Napi::String::New(env, "pactffiGivenWithParam"), Napi::Function::New(env, PactffiGivenWithParam));
exports.Set(Napi::String::New(env, "PactffiGivenWithParams"), Napi::Function::New(env, PactffiGivenWithParams));
exports.Set(Napi::String::New(env, "pactffiWithRequest"), Napi::Function::New(env, PactffiWithRequest));
exports.Set(Napi::String::New(env, "pactffiWithQueryParameter"), Napi::Function::New(env, PactffiWithQueryParameter));
exports.Set(Napi::String::New(env, "pactffiWithSpecification"), Napi::Function::New(env, PactffiWithSpecification));
Expand Down
46 changes: 28 additions & 18 deletions native/consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -631,51 +631,61 @@ Napi::Value PactffiGiven(const Napi::CallbackInfo& info) {
// }

/**
* Adds a provider state to the Interaction with a parameter key and value. Returns false if the interaction or Pact can't be
* modified (i.e. the mock server for it has already started)
* Adds a provider state to the Interaction with a set of parameter key and value pairs in JSON
* form. If the params is not an JSON object, it will add it as a single parameter with a `value`
* key.
*
* # Parameters
* * `description` - The provider state description.
* * `params` - Parameter values as a JSON fragment.
*
* # Errors
* Returns EXIT_FAILURE (1) if the interaction or Pact can't be modified (i.e. the mock server
* for it has already started).
* Returns 2 and sets the error message (which can be retrieved with `pactffi_get_error_message`)
* if the parameter values con't be parsed as JSON.
* Returns 3 if any of the C strings are not valid.
*
* * `description` - The provider state description. It needs to be unique.
* * `name` - Parameter name.
* * `value` - Parameter value.
*
* C interface:
*
* bool pactffi_given_with_param(InteractionHandle interaction,
* int pactffi_given_with_params(InteractionHandle interaction,
* const char *description,
* const char *name,
* const char *value);
* const char *params);
*/
Napi::Value PactffiGivenWithParam(const Napi::CallbackInfo& info) {
Napi::Value PactffiGivenWithParams(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 4) {
throw Napi::Error::New(env, "PactffiGivenWithParam received < 4 arguments");
throw Napi::Error::New(env, "PactffiGivenWithParams received < 4 arguments");
}

if (!info[0].IsNumber()) {
throw Napi::Error::New(env, "PactffiGivenWithParam(arg 0) expected a InteractionHandle (uint32_t)");
throw Napi::Error::New(env, "PactffiGivenWithParams(arg 0) expected a InteractionHandle (uint32_t)");
}

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

if (!info[2].IsString()) {
throw Napi::Error::New(env, "PactffiGivenWithParam(arg 2) expected a string");
throw Napi::Error::New(env, "PactffiGivenWithParams(arg 2) expected a string");
}

if (!info[3].IsString()) {
throw Napi::Error::New(env, "PactffiGivenWithParam(arg 3) expected a string");
throw Napi::Error::New(env, "PactffiGivenWithParams(arg 3) expected a string");
}

InteractionHandle interaction = info[0].As<Napi::Number>().Uint32Value();
std::string description = info[1].As<Napi::String>().Utf8Value();
std::string name = info[2].As<Napi::String>().Utf8Value();
std::string value = info[3].As<Napi::String>().Utf8Value();
std::string params = info[2].As<Napi::String>().Utf8Value();

bool res = pactffi_given_with_param(interaction, description.c_str(), name.c_str(), value.c_str());
int res = pactffi_given_with_params(interaction, description.c_str(), params.c_str());

return Napi::Boolean::New(env, res);
if (res > 0) {
return Napi::Boolean::New(env, false);
}
return Napi::Boolean::New(env, true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion native/consumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Napi::Value PactffiCleanupMockServer(const Napi::CallbackInfo& info);
Napi::Value PactffiCreateMockServerForPact(const Napi::CallbackInfo& info);
Napi::Value PactffiCreateMockServer(const Napi::CallbackInfo& info);
Napi::Value PactffiGiven(const Napi::CallbackInfo& info);
Napi::Value PactffiGivenWithParam(const Napi::CallbackInfo& info);
Napi::Value PactffiGivenWithParams(const Napi::CallbackInfo& info);
Napi::Value PactffiMockServerLogs(const Napi::CallbackInfo& info);
Napi::Value PactffiMockServerMatched(const Napi::CallbackInfo& info);
Napi::Value PactffiMockServerMismatches(const Napi::CallbackInfo& info);
Expand Down
6 changes: 3 additions & 3 deletions src/consumer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const makeConsumerPact = (
},
given: (state: string) => ffi.pactffiGiven(interactionPtr, state),
givenWithParam: (state: string, name: string, value: string) =>
ffi.pactffiGivenWithParam(interactionPtr, state, name, value),
ffi.pactffiGivenWithParams(interactionPtr, state, name, value),
withRequestContents: (body: string, contentType: string) =>
ffi.pactffiWithBody(
interactionPtr,
Expand Down Expand Up @@ -240,7 +240,7 @@ export const makeConsumerPact = (
ffi.pactffiUponReceiving(interactionPtr, recieveDescription),
given: (state: string) => ffi.pactffiGiven(interactionPtr, state),
givenWithParam: (state: string, name: string, value: string) =>
ffi.pactffiGivenWithParam(interactionPtr, state, name, value),
ffi.pactffiGivenWithParams(interactionPtr, state, JSON.stringify({[name]: value})),
withRequest: (method: string, path: string) =>
ffi.pactffiWithRequest(interactionPtr, method, path),
withQuery: (name: string, index: number, value: string) =>
Expand Down Expand Up @@ -450,7 +450,7 @@ export const makeConsumerMessagePact = (
},
given: (state: string) => ffi.pactffiGiven(interactionPtr, state),
givenWithParam: (state: string, name: string, value: string) =>
ffi.pactffiGivenWithParam(interactionPtr, state, name, value),
ffi.pactffiGivenWithParams(interactionPtr, state, JSON.stringify({[name]: value})),
withRequestContents: (body: string, contentType: string) =>
ffi.pactffiWithBody(
interactionPtr,
Expand Down
5 changes: 2 additions & 3 deletions src/ffi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,10 @@ export type FfiConsumerFunctions = {
description: string
): boolean;
pactffiGiven(handle: FfiInteractionHandle, providerState: string): boolean;
pactffiGivenWithParam(
pactffiGivenWithParams(
handle: FfiInteractionHandle,
description: string,
name: string,
value: string
params: string
): boolean;
pactffiWithRequest(
handle: FfiInteractionHandle,
Expand Down

0 comments on commit 2fada2a

Please sign in to comment.