Skip to content

Commit

Permalink
Merge pull request #772 from wheresrhys/rhys/document-missingheaders
Browse files Browse the repository at this point in the history
docs: document and test behaviour with multiple missing headers
  • Loading branch information
wheresrhys committed Aug 3, 2024
2 parents 5594953 + 88d0440 commit 263c3bd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 36 deletions.
6 changes: 6 additions & 0 deletions docs/docs/@fetch-mock/core/route/matcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ Match only requests using this http method. Not case-sensitive, e.g. `{method: "

Match only requests that have these headers set, e.g. `{headers: {"Accepts": "text/html"}}`

#### missingHeaders

`{String[]}`

Matches any requests where **all** of a list of header names are missing on a request e.g. `{missingHeaders: ["Authorization"]}`.

### query

`{Object}`
Expand Down
103 changes: 67 additions & 36 deletions packages/core/src/__tests__/Matchers/headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,49 +44,80 @@ describe('header matching', () => {
}),
).toBe(true);
});
describe('missing headers', () => {
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('match missing headers', () => {
const route = new Route({
missingHeaders: ['a'],
response: 200,
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);
});
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,
it('match when multiple headers are missing', () => {
const route = new Route({
missingHeaders: ['a', 'b'],
response: 200,
});
expect(
route.matcher({
url: 'http://a.com/',
options: {
headers: { c: 'b' },
},
}),
).toBe(true);
});
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,
it('not match when only one header is missing', () => {
const route = new Route({
missingHeaders: ['a', 'b'],
response: 200,
});
expect(
route.matcher({
url: 'http://a.com/',
options: {
headers: { a: 'b' },
},
}),
).toBe(false);
});

expect(
route.matcher({
url: 'http://a.com/',
options: {},
}),
).toBe(true);
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', () => {
Expand Down

0 comments on commit 263c3bd

Please sign in to comment.