Skip to content

Commit 5ad1dc2

Browse files
authored
feat(fetchye): http headers casing does not affect cache key creation (#53)
1 parent 7d5b7d3 commit 5ad1dc2

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

packages/fetchye/__tests__/computeKey.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,19 @@ describe('computeKey', () => {
4343
it('should return false if key func returns false', () => {
4444
expect(computeKey(() => false, {})).toEqual(false);
4545
});
46+
it('should return the same hash value irrespective of header name casings', () => {
47+
const firstOptions = {
48+
headers: {
49+
Authorization: 'fake.jw.t',
50+
'Content-Type': 'application/json',
51+
},
52+
};
53+
const secondOptions = {
54+
headers: {
55+
AUTHoriZation: 'fake.jw.t',
56+
'content-type': 'application/json',
57+
},
58+
};
59+
expect(computeKey('uri', firstOptions).hash).toBe(computeKey('uri', secondOptions).hash);
60+
});
4661
});

packages/fetchye/src/computeKey.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,28 @@
1515
*/
1616

1717
import computeHash from 'object-hash';
18+
import mapHeaderNamesToLowerCase from './mapHeaderNamesToLowerCase';
1819

1920
export const computeKey = (key, options) => {
21+
const { headers, ...restOfOptions } = options;
22+
const nextOptions = { ...restOfOptions };
23+
if (headers) {
24+
nextOptions.headers = mapHeaderNamesToLowerCase(headers);
25+
}
26+
2027
if (typeof key === 'function') {
2128
let nextKey;
2229
try {
23-
nextKey = key(options);
30+
nextKey = key(nextOptions);
2431
} catch (error) {
2532
return false;
2633
}
2734
if (!nextKey) {
2835
return false;
2936
}
30-
return { key: nextKey, hash: computeHash([nextKey, options], { respectType: false }) };
37+
return { key: nextKey, hash: computeHash([nextKey, nextOptions], { respectType: false }) };
3138
}
32-
return { key, hash: computeHash([key, options], { respectType: false }) };
39+
return { key, hash: computeHash([key, nextOptions], { respectType: false }) };
3340
};
3441

3542
export default computeKey;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2021 American Express Travel Related Services Company, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+
* or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
17+
const mapHeaderNamesToLowerCase = (headers) => Object.entries(headers)
18+
.reduce((lowerCaseHeaders, [headerName, value]) => ({
19+
...lowerCaseHeaders,
20+
[headerName.toLowerCase()]: value,
21+
}), {});
22+
23+
export default mapHeaderNamesToLowerCase;

0 commit comments

Comments
 (0)