Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 350d8ca

Browse files
fix: adds "normalizeHeaders"
1 parent 79448a3 commit 350d8ca

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { assert } = require('chai');
2+
const normalizeHeaders = require('../../../../units/normalize/normalizeHeaders');
3+
4+
describe('normalizeHeaders', () => {
5+
describe('when given nothing', () => {
6+
const headers = normalizeHeaders(undefined);
7+
8+
it('coerces to empty object', () => {
9+
assert.deepEqual(headers, {});
10+
});
11+
});
12+
13+
describe('when given headers object', () => {
14+
const headers = {
15+
'Accept-Language': 'en-US, en',
16+
'Content-Type': 'application/json',
17+
'Content-Length': 128
18+
};
19+
const normalizedHeaders = normalizeHeaders(headers);
20+
21+
it('lowercases the keys', () => {
22+
const expectedKeys = Object.keys(headers).map((key) => key.toLowerCase());
23+
assert.hasAllKeys(normalizedHeaders, expectedKeys);
24+
});
25+
26+
it('lowercases the values', () => {
27+
const expectedValues = Object.values(headers).map((value) =>
28+
typeof value === 'string' ? value.toLowerCase() : value
29+
);
30+
assert.deepEqual(Object.values(normalizedHeaders), expectedValues);
31+
});
32+
});
33+
34+
describe('when given non-object headers', () => {
35+
const unsupportedTypes = [['string', 'foo'], ['number', 2]];
36+
37+
unsupportedTypes.forEach(([typeName, dataType]) => {
38+
it(`when given ${typeName}`, () => {
39+
assert.throw(() => normalizeHeaders(dataType));
40+
});
41+
});
42+
});
43+
});

lib/api/units/normalize/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const evolve = require('../../utils/evolve');
2+
const normalizeHeaders = require('./normalizeHeaders');
3+
4+
const normalize = evolve({
5+
headers: normalizeHeaders
6+
});
7+
8+
module.exports = normalize;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const normalizeStringValue = (value) => {
2+
return value.toLowerCase().trim();
3+
};
4+
5+
/**
6+
* Normalizes the given headers.
7+
* @param {Object} headers
8+
* @returns {Object}
9+
*/
10+
const normalizeHeaders = (headers) => {
11+
if (!headers) {
12+
return {};
13+
}
14+
15+
const headersType = typeof headers;
16+
17+
if (headersType === null || headersType !== 'object') {
18+
throw new Error(
19+
`Can't validate: expected "headers" to be an Object, but got: ${headersType}.`
20+
);
21+
}
22+
23+
return Object.keys(headers).reduce(
24+
(acc, name) => ({
25+
...acc,
26+
[name.toLowerCase()]:
27+
typeof headers[name] === 'string'
28+
? normalizeStringValue(headers[name])
29+
: headers[name]
30+
}),
31+
{}
32+
);
33+
};
34+
35+
module.exports = normalizeHeaders;

0 commit comments

Comments
 (0)