Skip to content

Commit

Permalink
feat: support custom provider headers in verification
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Mar 6, 2022
1 parent aeef054 commit d63e1a9
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 5 deletions.
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"include_dirs": [
"<!(node -p \"require('node-addon-api').include_dir\")",
"<(module_root_dir)/native"
"<(module_root_dir)/native",
"<(module_root_dir)/ffi",
],
"conditions": [
[
Expand Down
1 change: 1 addition & 0 deletions native/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "pactffiVerifierSetFilterInfo"), Napi::Function::New(env, PactffiVerifierSetFilterInfo));
exports.Set(Napi::String::New(env, "pactffiVerifierSetProviderState"), Napi::Function::New(env, PactffiVerifierSetProviderState));
exports.Set(Napi::String::New(env, "pactffiVerifierSetConsumerFilters"), Napi::Function::New(env, PactffiVerifierSetConsumerFilters));
exports.Set(Napi::String::New(env, "pactffiVerifierAddCustomHeader"), Napi::Function::New(env, PactffiVerifierAddCustomHeader));
exports.Set(Napi::String::New(env, "pactffiVerifierAddFileSource"), Napi::Function::New(env, PactffiVerifierAddFileSource));
exports.Set(Napi::String::New(env, "pactffiVerifierAddDirectorySource"), Napi::Function::New(env, PactffiVerifierAddDirectorySource));
exports.Set(Napi::String::New(env, "pactffiVerifierUrlSource"), Napi::Function::New(env, PactffiVerifierUrlSource));
Expand Down
44 changes: 44 additions & 0 deletions native/provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,50 @@ Napi::Value PactffiVerifierSetConsumerFilters(const Napi::CallbackInfo& info) {
return info.Env().Undefined();
}

/**
* Adds a custom header to be added to the requests made to the provider.
*
* # Safety
*
* The header name and value must point to a valid NULL terminated string and must contain
* valid UTF-8.
*
* C interface:
*
* void pactffi_verifier_add_custom_header(VerifierHandle *handle,
* const char *header_name,
* const char *header_value);
*
*/
Napi::Value PactffiVerifierAddCustomHeader(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 2) {
throw Napi::Error::New(env, "PactffiVerifierAddCustomHeader received < 3 arguments");
}

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

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

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

uint32_t handleId = info[0].As<Napi::Number>().Uint32Value();
std::string name = info[1].As<Napi::String>().Utf8Value();
std::string value = info[2].As<Napi::String>().Utf8Value();

pactffi_verifier_add_custom_header(handles[handleId],
name.c_str(),
value.c_str());

return info.Env().Undefined();
}

/**
* Adds a Pact file as a source to verify.
*
Expand Down
1 change: 1 addition & 0 deletions native/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Napi::Value PactffiVerifierSetProviderState(const Napi::CallbackInfo& info);
Napi::Value PactffiVerifierSetVerificationOptions(const Napi::CallbackInfo& info);
Napi::Value PactffiVerifierSetPublishOptions(const Napi::CallbackInfo& info);
Napi::Value PactffiVerifierSetConsumerFilters(const Napi::CallbackInfo& info);
Napi::Value PactffiVerifierAddCustomHeader(const Napi::CallbackInfo& info);
Napi::Value PactffiVerifierAddFileSource(const Napi::CallbackInfo& info);
Napi::Value PactffiVerifierAddDirectorySource(const Napi::CallbackInfo& info);
Napi::Value PactffiVerifierUrlSource(const Napi::CallbackInfo& info);
Expand Down
2 changes: 1 addition & 1 deletion script/lib/download-ffi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function download_ffi_file {
URL="${BASEURL}/libpact_ffi-${FFI_VERSION}/${FFI_FILENAME}"
DOWNLOAD_LOCATION="$FFI_DIR/${OUTPUT_FILENAME}"

log "Downloading ffi $FFI_VERSION for $SUFFIX"
log "Downloading ffi $FFI_VERSION for $FFI_FILENAME"
download_to "$URL" "$DOWNLOAD_LOCATION"
debug_log " ... downloaded to '$DOWNLOAD_LOCATION'"
}
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import bindings = require('bindings');

const ffiLib: Ffi = bindings('pact.node');

export const PACT_FFI_VERSION = '0.2.2';
export const PACT_FFI_VERSION = '0.2.3';

let ffi: typeof ffiLib;
let ffiLogLevel: LogLevel;
Expand Down
5 changes: 5 additions & 0 deletions src/ffi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ export type Ffi = {
handle: FfiVerifierHandle,
consumers: string[]
): void;
pactFfiVerifierAddCustomHeader(
handle: FfiVerifierHandle,
header: string,
value: string
): void;
pactffiVerifierAddFileSource(handle: FfiVerifierHandle, file: string): void;
pactffiVerifierAddDirectorySource(
handle: FfiVerifierHandle,
Expand Down
6 changes: 5 additions & 1 deletion src/verifier/nativeVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ export const verify = (opts: VerifierOptions): Promise<string> => {
handle,
opts.providerStatesSetupUrl,
opts.providerStatesSetupTeardown || true,
opts.providerStatesSetupBody || true
opts.providerStatesSetupBody || true // dumb, this means they are always set!
);
}

Object.keys(opts.customProviderHeaders || {}).forEach((key, _, obj) =>
ffi.pactFfiVerifierAddCustomHeader(handle, key, obj[key])
);

const filterDescription = process.env.PACT_DESCRIPTION || '';
const filterState = process.env.PACT_PROVIDER_STATE || '';
const filterNoState = process.env.PACT_PROVIDER_NO_STATE ? true : false;
Expand Down
6 changes: 5 additions & 1 deletion src/verifier/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export interface ConsumerVersionSelector {
matchingBranch?: boolean;
}

export type CustomHeaders = {
[header: string]: string;
};

export interface VerifierOptions {
providerBaseUrl: string;
provider?: string;
Expand All @@ -37,6 +41,7 @@ export interface VerifierOptions {
logLevel?: LogLevel;
disableSslVerification?: boolean;
buildUrl?: string;
customProviderHeaders?: CustomHeaders;
}

/** These are the deprecated verifier options, removed prior to this verison,
Expand All @@ -45,7 +50,6 @@ export interface VerifierOptions {
type DeprecatedVerifierOptions = {
format?: 'json' | 'xml' | 'progress' | 'RspecJunitFormatter';
out?: string;
customProviderHeaders?: string[];
verbose?: boolean;
monkeypatch?: string;
logDir?: string;
Expand Down

0 comments on commit d63e1a9

Please sign in to comment.