From f7e3e92058415278338d8eb15dc6f107da8dffd7 Mon Sep 17 00:00:00 2001 From: Jake Champion Date: Tue, 18 Jul 2023 01:25:02 +0100 Subject: [PATCH] fix: Headers only accepts array which have nested array of length 2 fixes https://github.com/JakeChampion/fetch/issues/1235 --- fetch.js | 3 +++ test/test.js | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/fetch.js b/fetch.js index c7775af..9c198c3 100644 --- a/fetch.js +++ b/fetch.js @@ -92,6 +92,9 @@ export function Headers(headers) { }, this) } else if (Array.isArray(headers)) { headers.forEach(function(header) { + if (header.length != 2) { + throw new TypeError('Headers constructor: expected name/value pair to be length 2, found' + header.length) + } this.append(header[0], header[1]) }, this) } else if (headers) { diff --git a/test/test.js b/test/test.js index 8508610..e19a984 100644 --- a/test/test.js +++ b/test/test.js @@ -203,6 +203,16 @@ exercise.forEach(function(exerciseMode) { assert.equal(headers.get('Content-Type'), 'text/xml') assert.equal(headers.get('Breaking-Bad'), '<3') + assert.throws(function() { + new Headers([ + ['Content-Type'], + ]) + }, TypeError) + assert.throws(function() { + new Headers([ + ['Content-Type', 'a', 'b'], + ]) + }, TypeError) }) test('headers are case insensitive', function() { var headers = new Headers({Accept: 'application/json'}) -- GitLab