Skip to content

Commit

Permalink
Fixed incoming request header casing issue
Browse files Browse the repository at this point in the history
refs https://github.com/TryGhost/Toolbox/issues/209
refs antongolub/reqresnext#33

- All request headers should be lowercased to match the default behavior in express and node's http module. These patches should be removed once the upstream issue is solved
  • Loading branch information
naz committed Feb 15, 2022
1 parent 1975323 commit 952bf98
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
8 changes: 5 additions & 3 deletions packages/express-test/lib/request.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const {default: reqresnext} = require('reqresnext');
const {CookieAccessInfo} = require('cookiejar');
const {parse} = require('url');
const {isJSON} = require('./utils');
const {isJSON, convertKeysToLowerCase} = require('./utils');

class RequestOptions {
constructor({method, url, headers, body} = {}) {
this.method = method || 'GET';
this.url = url || '/';
this.headers = headers || {};
// TODO: remove convertKeysToLowerCase utility once https://github.com/antongolub/reqresnext/issues/33 is fixed
this.headers = convertKeysToLowerCase(headers) || {};
this.body = body;
}

Expand All @@ -29,7 +30,8 @@ class Request {
}

header(name, value) {
this.reqOptions.headers[name] = value;
// TODO: remove toLowerCase once https://github.com/antongolub/reqresnext/issues/33 is fixed
this.reqOptions.headers[name.toLowerCase()] = value;
return this;
}

Expand Down
10 changes: 10 additions & 0 deletions packages/express-test/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ module.exports.makeMessageFromMatchMessage = function makeMessageFromMatchMessag
messageLines.splice(0, 1, errorMessage);
return messageLines.join('\n');
};

module.exports.convertKeysToLowerCase = function (map) {
const newMap = {};

for (const key in map) {
newMap[key.toLowerCase()] = map[key];
}

return newMap;
};
18 changes: 17 additions & 1 deletion packages/express-test/test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
const {assert} = require('./utils');

const {isJSON} = require('../lib/utils');
const {isJSON, convertKeysToLowerCase} = require('../lib/utils');

describe('Utils', function () {
it('isJSON', function () {
assert.equal(isJSON('application/json'), true);
assert.equal(isJSON('application/ld+json'), true);
assert.equal(isJSON('text/html'), false);
});

it('convertKeysToLowerCase', function () {
const map = {
prop: 'value',
standard_Notation: 42,
'BaZ-Header': 'qux'
};

const lowerCasedMap = convertKeysToLowerCase(map);

assert.equal(map.standard_Notation, 42, 'original map is not modified');

assert.equal(lowerCasedMap.prop, 'value');
assert.equal(lowerCasedMap.standard_notation, 42);
assert.equal(lowerCasedMap['baz-header'], 'qux');
});
});

0 comments on commit 952bf98

Please sign in to comment.