Skip to content

Commit

Permalink
fix: support customProviderHeaders as a string[]
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Aug 13, 2022
1 parent f77dd65 commit 171cfd0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 15 deletions.
29 changes: 24 additions & 5 deletions src/verifier/argumentMapper/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,33 @@ export const ffiFnMapping: FnMapping<
> = {
pactffiVerifierAddCustomHeader: {
validateAndExecute(ffi, handle, options) {
const messages: string[] = [];

if (options.customProviderHeaders) {
if (options.customProviderHeaders) {
Object.entries(options.customProviderHeaders).forEach(
([key, value]) => {
ffi.pactffiVerifierAddCustomHeader(handle, key, value);
if (Array.isArray(options.customProviderHeaders)) {
options.customProviderHeaders.forEach((item) => {
const parts = item.split(':');
if (parts.length !== 2) {
messages.push(
`${item} is not a valid custom header. Must be in the format 'Header-Name: Value'`
);
} else {
ffi.pactffiVerifierAddCustomHeader(handle, parts[0], parts[1]);
}
);
});
} else {
if (options.customProviderHeaders) {
Object.entries(options.customProviderHeaders).forEach(
([key, value]) => {
ffi.pactffiVerifierAddCustomHeader(handle, key, value);
}
);
}
}
if (messages.length > 0) {
return { status: FnValidationStatus.FAIL };
}

return { status: FnValidationStatus.SUCCESS };
}

Expand Down
2 changes: 1 addition & 1 deletion src/verifier/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface VerifierOptions {
logLevel?: LogLevel;
disableSslVerification?: boolean;
buildUrl?: string;
customProviderHeaders?: CustomHeaders;
customProviderHeaders?: CustomHeaders | string[];
consumerFilters?: string[];
}

Expand Down
40 changes: 32 additions & 8 deletions src/verifier/validateOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,38 @@ describe('Verifier argument validator', () => {
});
});

context('when given customProviderHeaders that are defined', () => {
it('should pass through to the Pact Verifier', () => {
expect(() =>
validateOptions({
providerBaseUrl: 'http://localhost',
customProviderHeaders: { my: 'header' },
})
).to.not.throw(Error);
context.only('when given customProviderHeaders', () => {
context('using the object notation', () => {
it('should pass through to the Pact Verifier', () => {
expect(() =>
validateOptions({
providerBaseUrl: 'http://localhost',
customProviderHeaders: { my: 'header' },
})
).to.not.throw(Error);
});
});

context('using the legacy array notation', () => {
it('should pass through to the Pact Verifier', () => {
expect(() =>
validateOptions({
providerBaseUrl: 'http://localhost',
customProviderHeaders: ['My: Header'],
})
).to.not.throw(Error);
});

context('and the format is incorrect', () => {
it('should throw an error', () => {
expect(() =>
validateOptions({
providerBaseUrl: 'http://localhost',
customProviderHeaders: [1 as unknown as string],
})
).to.throw(Error);
});
});
});
});
});
16 changes: 15 additions & 1 deletion src/verifier/validateOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ const consumerVersionTagsValidator =
return true;
};

const customProviderHeadersValidator =
(options: InternalPactVerifierOptions) =>
(): boolean => {
if (options.customProviderHeaders) {
if (Array.isArray(options.customProviderHeaders)) {
checkTypes.assert.array.of.string(options.customProviderHeaders);
} else {
checkTypes.assert.nonEmptyObject(options.customProviderHeaders);
}
}

return true;
};

export type ArgumentValidationRules<T> = {
[Key in keyof T]-?: ((options: T) => AssertFunction)[];
};
Expand All @@ -175,7 +189,7 @@ export const validationRules: ArgumentValidationRules<InternalPactVerifierOption
buildUrl: [wrapCheckType(checkTypes.assert.nonEmptyString)],
consumerVersionSelectors: [consumerVersionSelectorValidator],
consumerVersionTags: [consumerVersionTagsValidator],
customProviderHeaders: [wrapCheckType(checkTypes.assert.nonEmptyObject)],
customProviderHeaders: [customProviderHeadersValidator],
disableSslVerification: [wrapCheckType(checkTypes.assert.boolean)],
enablePending: [wrapCheckType(checkTypes.assert.boolean)],
format: [deprecatedFunction],
Expand Down

0 comments on commit 171cfd0

Please sign in to comment.