Repository: wheresrhys/fetch-mock
Pull request #772 (2024-08-03 13:34:07)

docs: document and test behaviour with multiple missing headers


Patch:
diff --git a/docs/docs/@fetch-mock/core/route/matcher.md b/docs/docs/@fetch-mock/core/route/matcher.md
index a7f60ac..0fd58a2 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}`


Test patch:
diff --git a/packages/core/src/__tests__/Matchers/headers.test.js b/packages/core/src/__tests__/Matchers/headers.test.js
index 9d6218b..c0335ca 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', () => {