Skip to content

Commit

Permalink
feat(core): Normalize headers by lower-casing all keys (#42)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: With this change, request ids will resolve to a different hash meaning that users will have to rerecord.
  • Loading branch information
offirgolan authored and jasonmit committed Jun 25, 2018
1 parent d4be431 commit 02a4767
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 74 deletions.
30 changes: 22 additions & 8 deletions packages/@pollyjs/core/src/utils/normalize-request.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions packages/@pollyjs/core/tests/unit/utils/normalize-request-test.js
Expand Up @@ -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(
Expand Down
@@ -1,60 +1,33 @@
{
"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": {
"body": "",
"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": {
Expand All @@ -63,54 +36,81 @@
"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": {
"body": "",
"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": {
"body": "",
"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"
}
}
]
Expand Down
@@ -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": {
Expand All @@ -19,70 +19,70 @@
"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": {
"body": "",
"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": {
"body": "",
"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": {
Expand All @@ -91,34 +91,34 @@
"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": {
"body": "",
"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"
}
}
]
Expand Down

0 comments on commit 02a4767

Please sign in to comment.