From adc251ca5a8be2ebd5cb31d1b2dc1617acd6d0c6 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Tue, 21 Feb 2017 17:30:58 -0700 Subject: [PATCH] Accept array in Headers constructor --- fetch.js | 5 ++++- test/test.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/fetch.js b/fetch.js index dfb74be..a5b8548 100644 --- a/fetch.js +++ b/fetch.js @@ -84,7 +84,10 @@ headers.forEach(function(value, name) { this.append(name, value) }, this) - + } else if (Array.isArray(headers)) { + headers.forEach(function(header) { + this.append(header[0], header[1]); + }, this); } else if (headers) { Object.getOwnPropertyNames(headers).forEach(function(name) { this.append(name, headers[name]) diff --git a/test/test.js b/test/test.js index 280f0fe..ee315cf 100644 --- a/test/test.js +++ b/test/test.js @@ -170,6 +170,16 @@ suite('Headers', function() { assert.equal(headers.get('Accept'), 'application/json,text/plain') assert.equal(headers.get('Content-type'), 'text/html') }) + test('constructor works with arrays', function() { + var array = [ + ['Content-Type', 'text/xml'], + ['Breaking-Bad', '<3'] + ]; + var headers = new Headers(array) + + assert.equal(headers.get('Content-Type'), 'text/xml') + assert.equal(headers.get('Breaking-Bad'), '<3') + }) test('headers are case insensitive', function() { var headers = new Headers({'Accept': 'application/json'}) assert.equal(headers.get('ACCEPT'), 'application/json') -- GitLab