Skip to content

Commit

Permalink
feat(matchers): add missingHeaders
Browse files Browse the repository at this point in the history
Close #716
  • Loading branch information
posva committed Aug 1, 2024
1 parent 32e529a commit fcecfe4
Show file tree
Hide file tree
Showing 4 changed files with 50 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
30 changes: 30 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,36 @@ 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('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 UserRouteConfig = {
headers?: {
[key: string]: string | number;
};
missingHeaders?: string[];
query?: {
[key: string]: string;
};
Expand Down

0 comments on commit fcecfe4

Please sign in to comment.