From 02a476716d67225195842fe7ef92fac6dc383041 Mon Sep 17 00:00:00 2001 From: Offir Golan Date: Mon, 25 Jun 2018 13:52:54 -0700 Subject: [PATCH] feat(core): Normalize headers by lower-casing all keys (#42) BREAKING CHANGE: With this change, request ids will resolve to a different hash meaning that users will have to rerecord. --- .../core/src/utils/normalize-request.js | 30 +++++-- .../unit/utils/normalize-request-test.js | 27 ++++++ .../recording.json | 88 +++++++++---------- .../recording.json | 44 +++++----- 4 files changed, 115 insertions(+), 74 deletions(-) diff --git a/packages/@pollyjs/core/src/utils/normalize-request.js b/packages/@pollyjs/core/src/utils/normalize-request.js index 78182b36..f5c525a7 100644 --- a/packages/@pollyjs/core/src/utils/normalize-request.js +++ b/packages/@pollyjs/core/src/utils/normalize-request.js @@ -4,6 +4,7 @@ import isObjectLike from 'lodash-es/isObjectLike'; import isAbsoluteUrl from 'is-absolute-url'; const { keys } = Object; +const { isArray } = Array; export function method(method) { return (method || 'GET').toUpperCase(); @@ -39,18 +40,31 @@ export function url(url, config = {}) { } export function headers(headers, config) { - if (isObjectLike(config) && isObjectLike(headers) && config.exclude) { - // Filter out excluded headers - return keys(headers).reduce((h, k) => { - if (!config.exclude.includes(k)) { - h[k] = headers[k]; - } + let normalizedHeaders = headers; + + if (isObjectLike(normalizedHeaders)) { + // Lower case all headers so 'content-type' will be the same as 'Content-Type' + normalizedHeaders = keys(normalizedHeaders).reduce((accum, k) => { + accum[k.toLowerCase()] = normalizedHeaders[k]; - return h; + return accum; }, {}); + + // Filter out excluded headers + if (isObjectLike(config) && isArray(config.exclude)) { + const exclude = config.exclude.map(e => e.toLowerCase()); + + normalizedHeaders = keys(normalizedHeaders).reduce((accum, k) => { + if (!exclude.includes(k)) { + accum[k] = normalizedHeaders[k]; + } + + return accum; + }, {}); + } } - return headers; + return normalizedHeaders; } export function body(body) { diff --git a/packages/@pollyjs/core/tests/unit/utils/normalize-request-test.js b/packages/@pollyjs/core/tests/unit/utils/normalize-request-test.js index c8bf254b..25751466 100644 --- a/packages/@pollyjs/core/tests/unit/utils/normalize-request-test.js +++ b/packages/@pollyjs/core/tests/unit/utils/normalize-request-test.js @@ -28,6 +28,33 @@ describe('Unit | Utils | Normalize Request', function() { }); }); + describe('headers', function() { + it('should lower-case all header keys', function() { + expect( + headers({ + Accept: 'foo', + 'Content-Type': 'Bar' + }) + ).to.deep.equal({ + accept: 'foo', + 'content-type': 'Bar' + }); + }); + + it('should exclude specified headers', function() { + expect( + headers( + { + Accept: 'foo', + test: 'test', + 'Content-Type': 'Bar' + }, + { exclude: ['test', 'content-type'] } + ) + ).to.deep.equal({ accept: 'foo' }); + }); + }); + describe('url', function() { it('should sort query params', function() { expect(url('http://foo.com?b=1&c=1&a=1')).to.equal( diff --git a/test/recordings/-pollyjs_2160168770/core_3713949822/Integration-Adapters_529408267/Fetch-Adapter-Rest-Persister_112800326/should-respect-request-order_2914185093/recording.json b/test/recordings/-pollyjs_2160168770/core_3713949822/Integration-Adapters_529408267/Fetch-Adapter-Rest-Persister_112800326/should-respect-request-order_2914185093/recording.json index 398addb1..c5067d18 100644 --- a/test/recordings/-pollyjs_2160168770/core_3713949822/Integration-Adapters_529408267/Fetch-Adapter-Rest-Persister_112800326/should-respect-request-order_2914185093/recording.json +++ b/test/recordings/-pollyjs_2160168770/core_3713949822/Integration-Adapters_529408267/Fetch-Adapter-Rest-Persister_112800326/should-respect-request-order_2914185093/recording.json @@ -1,40 +1,13 @@ { - "created_at": "2018-06-22T18:07:44.447Z", + "created_at": "2018-06-25T20:32:33.710Z", "entries": { - "1ee5aa1bce525fe324cd2410440a9e87": [ - { - "created_at": "2018-06-22T18:07:44.447Z", - "request": { - "body": "{\"foo\":\"bar\"}", - "headers": { - "Content-Type": "application/json" - }, - "method": "POST", - "timestamp": "2018-06-22T18:07:44.383Z", - "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FFetch-Adapter-Rest-Persister_112800326%2Fshould-respect-request-order_2914185093" - }, - "response": { - "body": "{\"foo\":\"bar\"}", - "headers": { - "connection": "close", - "content-length": "13", - "content-type": "application/json; charset=utf-8", - "date": "Fri, 22 Jun 2018 18:07:44 GMT", - "etag": "W/\"d-pedE0BZFQNM7HX6mFsKPL6l+dUo\"", - "x-powered-by": "Express" - }, - "status": 200, - "timestamp": "2018-06-22T18:07:44.394Z" - } - } - ], "be0c42471e72afba776de36e2a1ff59f": [ { - "created_at": "2018-06-22T18:07:44.447Z", + "created_at": "2018-06-25T20:32:33.710Z", "request": { "headers": {}, "method": "GET", - "timestamp": "2018-06-22T18:07:44.372Z", + "timestamp": "2018-06-25T20:32:33.652Z", "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FFetch-Adapter-Rest-Persister_112800326%2Fshould-respect-request-order_2914185093" }, "response": { @@ -42,19 +15,19 @@ "headers": { "connection": "close", "content-length": "0", - "date": "Fri, 22 Jun 2018 18:07:44 GMT", + "date": "Mon, 25 Jun 2018 20:32:33 GMT", "x-powered-by": "Express" }, "status": 404, - "timestamp": "2018-06-22T18:07:44.382Z" + "timestamp": "2018-06-25T20:32:33.662Z" } }, { - "created_at": "2018-06-22T18:07:44.447Z", + "created_at": "2018-06-25T20:32:33.710Z", "request": { "headers": {}, "method": "GET", - "timestamp": "2018-06-22T18:07:44.396Z", + "timestamp": "2018-06-25T20:32:33.673Z", "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FFetch-Adapter-Rest-Persister_112800326%2Fshould-respect-request-order_2914185093" }, "response": { @@ -63,20 +36,20 @@ "connection": "close", "content-length": "13", "content-type": "application/json; charset=utf-8", - "date": "Fri, 22 Jun 2018 18:07:44 GMT", + "date": "Mon, 25 Jun 2018 20:32:33 GMT", "etag": "W/\"d-pedE0BZFQNM7HX6mFsKPL6l+dUo\"", "x-powered-by": "Express" }, "status": 200, - "timestamp": "2018-06-22T18:07:44.408Z" + "timestamp": "2018-06-25T20:32:33.681Z" } }, { - "created_at": "2018-06-22T18:07:44.447Z", + "created_at": "2018-06-25T20:32:33.710Z", "request": { "headers": {}, "method": "GET", - "timestamp": "2018-06-22T18:07:44.429Z", + "timestamp": "2018-06-25T20:32:33.690Z", "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FFetch-Adapter-Rest-Persister_112800326%2Fshould-respect-request-order_2914185093" }, "response": { @@ -84,21 +57,21 @@ "headers": { "connection": "close", "content-length": "0", - "date": "Fri, 22 Jun 2018 18:07:44 GMT", + "date": "Mon, 25 Jun 2018 20:32:33 GMT", "x-powered-by": "Express" }, "status": 404, - "timestamp": "2018-06-22T18:07:44.438Z" + "timestamp": "2018-06-25T20:32:33.700Z" } } ], "c15804732a62a96170bbda3629b07ba4": [ { - "created_at": "2018-06-22T18:07:44.447Z", + "created_at": "2018-06-25T20:32:33.710Z", "request": { "headers": {}, "method": "DELETE", - "timestamp": "2018-06-22T18:07:44.410Z", + "timestamp": "2018-06-25T20:32:33.682Z", "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FFetch-Adapter-Rest-Persister_112800326%2Fshould-respect-request-order_2914185093" }, "response": { @@ -106,11 +79,38 @@ "headers": { "connection": "close", "content-length": "0", - "date": "Fri, 22 Jun 2018 18:07:44 GMT", + "date": "Mon, 25 Jun 2018 20:32:33 GMT", + "x-powered-by": "Express" + }, + "status": 200, + "timestamp": "2018-06-25T20:32:33.690Z" + } + } + ], + "c5ebdc08e43d5bbac56563d702b3d9cf": [ + { + "created_at": "2018-06-25T20:32:33.710Z", + "request": { + "body": "{\"foo\":\"bar\"}", + "headers": { + "Content-Type": "application/json" + }, + "method": "POST", + "timestamp": "2018-06-25T20:32:33.663Z", + "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FFetch-Adapter-Rest-Persister_112800326%2Fshould-respect-request-order_2914185093" + }, + "response": { + "body": "{\"foo\":\"bar\"}", + "headers": { + "connection": "close", + "content-length": "13", + "content-type": "application/json; charset=utf-8", + "date": "Mon, 25 Jun 2018 20:32:33 GMT", + "etag": "W/\"d-pedE0BZFQNM7HX6mFsKPL6l+dUo\"", "x-powered-by": "Express" }, "status": 200, - "timestamp": "2018-06-22T18:07:44.427Z" + "timestamp": "2018-06-25T20:32:33.673Z" } } ] diff --git a/test/recordings/-pollyjs_2160168770/core_3713949822/Integration-Adapters_529408267/XHR-Adapter-Rest-Persister_3659812972/should-respect-request-order_2914185093/recording.json b/test/recordings/-pollyjs_2160168770/core_3713949822/Integration-Adapters_529408267/XHR-Adapter-Rest-Persister_3659812972/should-respect-request-order_2914185093/recording.json index c34c3e49..55b252c1 100644 --- a/test/recordings/-pollyjs_2160168770/core_3713949822/Integration-Adapters_529408267/XHR-Adapter-Rest-Persister_3659812972/should-respect-request-order_2914185093/recording.json +++ b/test/recordings/-pollyjs_2160168770/core_3713949822/Integration-Adapters_529408267/XHR-Adapter-Rest-Persister_3659812972/should-respect-request-order_2914185093/recording.json @@ -1,16 +1,16 @@ { - "created_at": "2018-06-22T18:07:44.277Z", + "created_at": "2018-06-25T20:32:33.577Z", "entries": { - "9208660a18c7a292e9534940083341b0": [ + "9d9ab11036c774a1876f2715ed671ed3": [ { - "created_at": "2018-06-22T18:07:44.286Z", + "created_at": "2018-06-25T20:32:33.577Z", "request": { "body": "{\"foo\":\"bar\"}", "headers": { "Content-Type": "application/json;charset=utf-8" }, "method": "POST", - "timestamp": "2018-06-22T18:07:44.210Z", + "timestamp": "2018-06-25T20:32:33.507Z", "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FXHR-Adapter-Rest-Persister_3659812972%2Fshould-respect-request-order_2914185093" }, "response": { @@ -19,24 +19,24 @@ "connection": "close", "content-length": "13", "content-type": "application/json; charset=utf-8", - "date": "Fri, 22 Jun 2018 18", + "date": "Mon, 25 Jun 2018 20", "etag": "W/\"d-pedE0BZFQNM7HX6mFsKPL6l+dUo\"", "x-powered-by": "Express" }, "status": 200, - "timestamp": "2018-06-22T18:07:44.224Z" + "timestamp": "2018-06-25T20:32:33.520Z" } } ], "e6f2c8956e4eedd0937533b32388c842": [ { - "created_at": "2018-06-22T18:07:44.286Z", + "created_at": "2018-06-25T20:32:33.577Z", "request": { "headers": { "Content-Type": "text/plain;charset=utf-8" }, "method": "DELETE", - "timestamp": "2018-06-22T18:07:44.250Z", + "timestamp": "2018-06-25T20:32:33.547Z", "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FXHR-Adapter-Rest-Persister_3659812972%2Fshould-respect-request-order_2914185093" }, "response": { @@ -44,23 +44,23 @@ "headers": { "connection": "close", "content-length": "0", - "date": "Fri, 22 Jun 2018 18", + "date": "Mon, 25 Jun 2018 20", "x-powered-by": "Express" }, "status": 200, - "timestamp": "2018-06-22T18:07:44.260Z" + "timestamp": "2018-06-25T20:32:33.556Z" } } ], "eb39d9b2d7c8db449ab804432be6291a": [ { - "created_at": "2018-06-22T18:07:44.286Z", + "created_at": "2018-06-25T20:32:33.577Z", "request": { "headers": { "Content-Type": "text/plain;charset=utf-8" }, "method": "GET", - "timestamp": "2018-06-22T18:07:44.188Z", + "timestamp": "2018-06-25T20:32:33.483Z", "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FXHR-Adapter-Rest-Persister_3659812972%2Fshould-respect-request-order_2914185093" }, "response": { @@ -68,21 +68,21 @@ "headers": { "connection": "close", "content-length": "0", - "date": "Fri, 22 Jun 2018 18", + "date": "Mon, 25 Jun 2018 20", "x-powered-by": "Express" }, "status": 404, - "timestamp": "2018-06-22T18:07:44.207Z" + "timestamp": "2018-06-25T20:32:33.502Z" } }, { - "created_at": "2018-06-22T18:07:44.286Z", + "created_at": "2018-06-25T20:32:33.577Z", "request": { "headers": { "Content-Type": "text/plain;charset=utf-8" }, "method": "GET", - "timestamp": "2018-06-22T18:07:44.226Z", + "timestamp": "2018-06-25T20:32:33.521Z", "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FXHR-Adapter-Rest-Persister_3659812972%2Fshould-respect-request-order_2914185093" }, "response": { @@ -91,22 +91,22 @@ "connection": "close", "content-length": "13", "content-type": "application/json; charset=utf-8", - "date": "Fri, 22 Jun 2018 18", + "date": "Mon, 25 Jun 2018 20", "etag": "W/\"d-pedE0BZFQNM7HX6mFsKPL6l+dUo\"", "x-powered-by": "Express" }, "status": 200, - "timestamp": "2018-06-22T18:07:44.248Z" + "timestamp": "2018-06-25T20:32:33.545Z" } }, { - "created_at": "2018-06-22T18:07:44.286Z", + "created_at": "2018-06-25T20:32:33.577Z", "request": { "headers": { "Content-Type": "text/plain;charset=utf-8" }, "method": "GET", - "timestamp": "2018-06-22T18:07:44.263Z", + "timestamp": "2018-06-25T20:32:33.557Z", "url": "/api/db/-pollyjs_2160168770%2Fcore_3713949822%2FIntegration-Adapters_529408267%2FXHR-Adapter-Rest-Persister_3659812972%2Fshould-respect-request-order_2914185093" }, "response": { @@ -114,11 +114,11 @@ "headers": { "connection": "close", "content-length": "0", - "date": "Fri, 22 Jun 2018 18", + "date": "Mon, 25 Jun 2018 20", "x-powered-by": "Express" }, "status": 404, - "timestamp": "2018-06-22T18:07:44.273Z" + "timestamp": "2018-06-25T20:32:33.568Z" } } ]