From 88d0440b814a0f3309f49c30d6c81d899ebc65a6 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Sat, 3 Aug 2024 14:33:54 +0100 Subject: [PATCH] docs: document and test behaviour with multiple missing headers --- docs/docs/@fetch-mock/core/route/matcher.md | 6 + .../src/__tests__/Matchers/headers.test.js | 103 ++++++++++++------ 2 files changed, 73 insertions(+), 36 deletions(-) diff --git a/docs/docs/@fetch-mock/core/route/matcher.md b/docs/docs/@fetch-mock/core/route/matcher.md index a7f60ac90..0fd58a2f3 100644 --- a/docs/docs/@fetch-mock/core/route/matcher.md +++ b/docs/docs/@fetch-mock/core/route/matcher.md @@ -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}` diff --git a/packages/core/src/__tests__/Matchers/headers.test.js b/packages/core/src/__tests__/Matchers/headers.test.js index 9d6218bd3..c0335ca1b 100644 --- a/packages/core/src/__tests__/Matchers/headers.test.js +++ b/packages/core/src/__tests__/Matchers/headers.test.js @@ -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', () => {