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

Commit a0e3229

Browse files
refactor: removes explicit normalization from "validateHeaders"
1 parent 5589c2f commit a0e3229

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

lib/api/units/normalize/normalizeHeaders.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ const normalizeHeaders = (headers) => {
1313
}
1414

1515
const headersType = typeof headers;
16+
const isHeadersNull = headers == null;
1617

17-
if (headersType === null || headersType !== 'object') {
18+
if (isHeadersNull || headersType !== 'object') {
1819
throw new Error(
19-
`Can't validate: expected "headers" to be an Object, but got: ${headersType}.`
20+
`Can't validate: expected "headers" to be an Object, but got: ${
21+
isHeadersNull ? 'null' : headersType
22+
}.`
2023
);
2124
}
2225

2326
return Object.keys(headers).reduce(
24-
(acc, name) => ({
25-
...acc,
27+
(normalizedHeaders, name) => ({
28+
...normalizedHeaders,
2629
[name.toLowerCase()]:
2730
typeof headers[name] === 'string'
2831
? normalizeStringValue(headers[name])

lib/api/units/validateHeaders.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@ const { HeadersJsonExample } = require('../../validators/headers-json-example');
22

33
const APIARY_JSON_HEADER_TYPE = 'application/vnd.apiary.http-headers+json';
44

5-
function normalizeHeaders(headers) {
6-
return headers instanceof Object
7-
? Object.keys(headers).reduce(
8-
(acc, headerName) =>
9-
Object.assign({}, acc, {
10-
[headerName.toLowerCase()]: headers[headerName]
11-
}),
12-
{}
13-
)
14-
: null;
15-
}
16-
175
function getHeadersType(headers) {
186
return headers instanceof Object && !Array.isArray(headers)
197
? APIARY_JSON_HEADER_TYPE
@@ -26,18 +14,16 @@ function getHeadersType(headers) {
2614
* @param {Object} expected
2715
*/
2816
function validateHeaders(real, expected) {
29-
const realHeaders = normalizeHeaders(real.headers);
30-
const expectedHeaders = normalizeHeaders(expected.headers);
31-
const realType = getHeadersType(realHeaders);
32-
const expectedType = getHeadersType(expectedHeaders);
17+
const realType = getHeadersType(real.headers);
18+
const expectedType = getHeadersType(expected.headers);
3319
const results = [];
3420

3521
const hasJsonHeaders =
3622
realType === APIARY_JSON_HEADER_TYPE &&
3723
expectedType === APIARY_JSON_HEADER_TYPE;
3824

3925
const validator = hasJsonHeaders
40-
? new HeadersJsonExample(realHeaders, expectedHeaders)
26+
? new HeadersJsonExample(real.headers, expected.headers)
4127
: null;
4228
const rawData = validator && validator.validate();
4329

0 commit comments

Comments
 (0)