Skip to content

Commit

Permalink
Merge pull request #764 from posva/feat/missing-headers
Browse files Browse the repository at this point in the history
feat(matchers): add missingHeaders
  • Loading branch information
wheresrhys authored Aug 3, 2024
2 parents 8d2a595 + bb7ef68 commit 5594953
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/core/src/Matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ const getHeaderMatcher = ({ headers: expectedHeaders }) => {
);
};
};
/**
* @type {MatcherGenerator}
*/
const getMissingHeaderMatcher = ({
missingHeaders: expectedMissingHeaders,
}) => {
if (!expectedMissingHeaders) {
return;
}
const expectation = expectedMissingHeaders.map((header) =>
header.toLowerCase(),
);
return ({ options: { headers = {} } }) => {
const lowerCaseHeaders = normalizeHeaders(headers);
return expectation.every((headerName) => !(headerName in lowerCaseHeaders));
};
};
/**
* @type {MatcherGenerator}
*/
Expand Down Expand Up @@ -306,6 +323,7 @@ export const builtInMatchers = [
{ name: 'query', matcher: getQueryParamsMatcher },
{ name: 'method', matcher: getMethodMatcher },
{ name: 'headers', matcher: getHeaderMatcher },
{ name: 'missingHeaders', matcher: getMissingHeaderMatcher },
{ name: 'params', matcher: getExpressParamsMatcher },
{ name: 'body', matcher: getBodyMatcher, usesBody: true },
{ name: 'matcherFunction', matcher: getFunctionMatcher },
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import statusTextMap from './StatusTextMap.js';
* @property {RouteName} [name]
* @property {string} [method]
* @property {{ [key: string]: string | number }} [headers]
* @property {string[]} [missingHeaders]
* @property {{ [key: string]: string }} [query]
* @property {{ [key: string]: string }} [params]
* @property {object} [body]
Expand Down
44 changes: 44 additions & 0 deletions packages/core/src/__tests__/Matchers/headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,50 @@ describe('header matching', () => {
).toBe(true);
});

it('match missing headers', () => {
const route = new Route({
missingHeaders: ['a'],
response: 200,
});
expect(
route.matcher({
url: 'http://a.com/',
options: {
headers: { b: 'c' },
},
}),
).toBe(true);
});

it('not match present missing header', () => {
const route = new Route({
missingHeaders: ['a'],
response: 200,
});
expect(
route.matcher({
url: 'http://a.com/',
options: {
headers: { a: 'b' },
},
}),
).toBe(false);
});

it('not error when request sent without headers', () => {
const route = new Route({
missingHeaders: ['a'],
response: 200,
});

expect(
route.matcher({
url: 'http://a.com/',
options: {},
}),
).toBe(true);
});

it('be case insensitive', () => {
const route = new Route({
headers: { a: 'b' },
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/Route.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type UserRouteSpecificConfig = {
headers?: {
[key: string]: string | number;
};
missingHeaders?: string[];
query?: {
[key: string]: string;
};
Expand Down

0 comments on commit 5594953

Please sign in to comment.