From c36e5ea075115c8ae0d9fa783242724955257152 Mon Sep 17 00:00:00 2001 From: kellyselden Date: Fri, 13 Jan 2017 15:27:15 -0800 Subject: [PATCH 1/2] header value coerce to array instead of split --- src/fastboot-headers.js | 12 +++++++++++- test/fastboot-headers-test.js | 20 +++++--------------- test/fastboot-response-test.js | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/fastboot-headers.js b/src/fastboot-headers.js index 1fe2549..77609c1 100644 --- a/src/fastboot-headers.js +++ b/src/fastboot-headers.js @@ -5,7 +5,17 @@ function FastBootHeaders(headers) { this.headers = {}; for (var header in headers) { - this.headers[header] = headers[header].split(', '); + let value = headers[header]; + + // Express gives us either a string + // or an array of strings if there are multiple values. + // We want to support the Header spec + // so we will coerce to an array always. + if (typeof value === 'string') { + value = [value]; + } + + this.headers[header] = value; } } diff --git a/test/fastboot-headers-test.js b/test/fastboot-headers-test.js index 041ee25..8985994 100644 --- a/test/fastboot-headers-test.js +++ b/test/fastboot-headers-test.js @@ -8,9 +8,7 @@ var FastBootHeaders = alchemistRequire('fastboot-headers.js'); describe('FastBootHeaders', function() { it('returns an array of header values from getAll, regardless of header name casing', function() { var headers = { - // Express concatenates repeated keys with ', ' - // and also lowercases the keys - 'x-test-header': 'value1, value2' + 'x-test-header': ['value1', 'value2'] }; headers = new FastBootHeaders(headers); @@ -20,9 +18,7 @@ describe('FastBootHeaders', function() { it('returns an emtpy array when a header is not present', function() { var headers = { - // Express concatenates repeated keys with ', ' - // and also lowercases the keys - 'x-test-header': 'value1, value2' + 'x-test-header': ['value1', 'value2'] }; headers = new FastBootHeaders(headers); @@ -32,9 +28,7 @@ describe('FastBootHeaders', function() { it('returns the first value when using get, regardless of case', function() { var headers = { - // Express concatenates repeated keys with ', ' - // and also lowercases the keys - 'x-test-header': 'value1, value2' + 'x-test-header': ['value1', 'value2'] }; headers = new FastBootHeaders(headers); @@ -44,9 +38,7 @@ describe('FastBootHeaders', function() { it('returns null when using get when a header is not present', function() { var headers = { - // Express concatenates repeated keys with ', ' - // and also lowercases the keys - 'x-test-header': 'value1, value2' + 'x-test-header': ['value1', 'value2'] }; headers = new FastBootHeaders(headers); @@ -56,9 +48,7 @@ describe('FastBootHeaders', function() { it('returns whether or not a header is present via has, regardless of casing', function() { var headers = { - // Express concatenates repeated keys with ', ' - // and also lowercases the keys - 'x-test-header': 'value1, value2' + 'x-test-header': ['value1', 'value2'] }; headers = new FastBootHeaders(headers); diff --git a/test/fastboot-response-test.js b/test/fastboot-response-test.js index 304b26e..d01cd51 100644 --- a/test/fastboot-response-test.js +++ b/test/fastboot-response-test.js @@ -9,7 +9,7 @@ describe("FastBootResponse", function() { beforeEach(function () { var mockResponse = { _headers: { - "i-am-a": "mock header, me too", + "i-am-a": ["mock header", "me too"], "cookie": "" } }; From 44195c8e3fd483e871be3d091b478457cbe2f857 Mon Sep 17 00:00:00 2001 From: kellyselden Date: Fri, 13 Jan 2017 15:36:04 -0800 Subject: [PATCH 2/2] add test for single header value (string, not array) --- test/fastboot-headers-test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/fastboot-headers-test.js b/test/fastboot-headers-test.js index 8985994..014c8a5 100644 --- a/test/fastboot-headers-test.js +++ b/test/fastboot-headers-test.js @@ -6,6 +6,15 @@ var alchemistRequire = require('broccoli-module-alchemist/require'); var FastBootHeaders = alchemistRequire('fastboot-headers.js'); describe('FastBootHeaders', function() { + it('returns an array from getAll when header value is string', function() { + var headers = { + 'x-test-header': 'value1, value2' + }; + headers = new FastBootHeaders(headers); + + expect(headers.getAll('x-test-header')).to.deep.equal(['value1, value2']); + }); + it('returns an array of header values from getAll, regardless of header name casing', function() { var headers = { 'x-test-header': ['value1', 'value2']