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 41caef6
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
3 changes: 2 additions & 1 deletion native/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ 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, "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
58 changes: 58 additions & 0 deletions native/consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,64 @@ Napi::Value PactffiGivenWithParam(const Napi::CallbackInfo& info) {
return Napi::Boolean::New(env, res);
}

/**
* 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.
*
*
* C interface:
*
* int pactffi_given_with_params(InteractionHandle interaction,
* const char *description,
* const char *params);
*/
Napi::Value PactffiGivenWithParams(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

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

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

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

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

if (!info[3].IsString()) {
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 params = info[2].As<Napi::String>().Utf8Value();

int res = pactffi_given_with_params(interaction, description.c_str(), params.c_str());

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

/**
* Configures the request for the Interaction. Returns false if the interaction or Pact can't be
* modified (i.e. the mock server for it has already started)
Expand Down
1 change: 1 addition & 0 deletions native/consumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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
8 changes: 8 additions & 0 deletions src/consumer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const asyncMessage = (ffi: Ffi, interactionPtr: number) => ({
given: (state: string) => ffi.pactffiMessageGiven(interactionPtr, state),
givenWithParam: (state: string, name: string, value: string) =>
ffi.pactffiMessageGivenWithParam(interactionPtr, state, name, value),
givenWithParams: (state: string, params: string) =>
ffi.pactffiMessageGivenWithParams(interactionPtr, state, params),
withContents: (body: string, contentType: string) =>
ffi.pactffiMessageWithContents(interactionPtr, contentType, body),
withBinaryContents: (body: Buffer, contentType: string) =>
Expand Down Expand Up @@ -181,6 +183,8 @@ export const makeConsumerPact = (
given: (state: string) => ffi.pactffiGiven(interactionPtr, state),
givenWithParam: (state: string, name: string, value: string) =>
ffi.pactffiGivenWithParam(interactionPtr, state, name, value),
givenWithParams: (state: string, params: string) =>
ffi.pactffiGivenWithParams(interactionPtr, state, params),
withRequestContents: (body: string, contentType: string) =>
ffi.pactffiWithBody(
interactionPtr,
Expand Down Expand Up @@ -241,6 +245,8 @@ export const makeConsumerPact = (
given: (state: string) => ffi.pactffiGiven(interactionPtr, state),
givenWithParam: (state: string, name: string, value: string) =>
ffi.pactffiGivenWithParam(interactionPtr, state, name, value),
givenWithParams: (state: string, params: string) =>
ffi.pactffiGivenWithParams(interactionPtr, state, params),
withRequest: (method: string, path: string) =>
ffi.pactffiWithRequest(interactionPtr, method, path),
withQuery: (name: string, index: number, value: string) =>
Expand Down Expand Up @@ -451,6 +457,8 @@ export const makeConsumerMessagePact = (
given: (state: string) => ffi.pactffiGiven(interactionPtr, state),
givenWithParam: (state: string, name: string, value: string) =>
ffi.pactffiGivenWithParam(interactionPtr, state, name, value),
givenWithParams: (state: string, params: string) =>
ffi.pactffiGivenWithParams(interactionPtr, state, params),
withRequestContents: (body: string, contentType: string) =>
ffi.pactffiWithBody(
interactionPtr,
Expand Down
3 changes: 3 additions & 0 deletions src/consumer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export type ConsumerInteraction = PluginInteraction & {
uponReceiving: (description: string) => boolean;
given: (state: string) => boolean;
givenWithParam: (state: string, name: string, value: string) => boolean;
givenWithParams: (state: string, params: string) => boolean;
withRequest: (method: string, path: string) => boolean;
withQuery: (name: string, index: number, value: string) => boolean;
withStatus: (status: number) => boolean;
Expand Down Expand Up @@ -233,6 +234,7 @@ export type ConsumerPact = PluginPact & {
export type AsynchronousMessage = RequestPluginInteraction & {
given: (state: string) => void;
givenWithParam: (state: string, name: string, value: string) => void;
givenWithParams: (state: string, params: string) => void;
expectsToReceive: (description: string) => void;
withMetadata: (name: string, value: string) => void;
withContents: (body: string, contentType: string) => void;
Expand All @@ -245,6 +247,7 @@ export type ConsumerMessage = AsynchronousMessage;
export type SynchronousMessage = PluginInteraction & {
given: (state: string) => void;
givenWithParam: (state: string, name: string, value: string) => void;
givenWithParams: (state: string, params: string) => void;
withMetadata: (name: string, value: string) => void;
withRequestContents: (body: string, contentType: string) => void;
withResponseContents: (body: string, contentType: string) => void;
Expand Down
10 changes: 10 additions & 0 deletions src/ffi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ export type FfiConsumerFunctions = {
name: string,
value: string
): boolean;
pactffiGivenWithParams(
handle: FfiInteractionHandle,
description: string,
params: string
): boolean;
pactffiWithRequest(
handle: FfiInteractionHandle,
method: string,
Expand Down Expand Up @@ -278,6 +283,11 @@ export type FfiConsumerFunctions = {
key: string,
value: string
): void;
pactffiMessageGivenWithParams(
handle: FfiMessageHandle,
description: string,
params: string
): void;
pactffiMessageWithContents(
handle: FfiMessageHandle,
contentType: string,
Expand Down

0 comments on commit 41caef6

Please sign in to comment.