Skip to content

Commit

Permalink
Normalize user-provided HttpLink headers by lower-casing their names. (
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jul 2, 2021
1 parent f9f8bef commit ae0df47
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
- Prefer `existing.pageInfo.startCursor` and `endCursor` (if defined) in `read` function of `relayStylePagination` policies. <br/>
[@benjamn](https://github.com/benjamn) in [#8438](https://github.com/apollographql/apollo-client/pull/8438)

### Improvements

- Normalize user-provided `HttpLink` headers by lower-casing their names. <br/>
[@benjamn](https://github.com/benjamn) in [#8449](https://github.com/apollographql/apollo-client/pull/8449)

## Apollo Client 3.3.20

### Bug fixes
Expand Down
25 changes: 25 additions & 0 deletions src/link/http/__tests__/selectHttpOptionsAndBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,29 @@ describe('selectHttpOptionsAndBody', () => {
expect(options.opt).toEqual('hi');
expect(options.method).toEqual('POST'); //from default
});

it('normalizes HTTP header names to lower case', () => {
const headers = {
accept: 'application/json',
Accept: 'application/octet-stream',
'content-type': 'application/graphql',
'Content-Type': 'application/javascript',
'CONTENT-type': 'application/json',
};

const config = { headers };
const { options, body } = selectHttpOptionsAndBody(
createOperation({}, { query }),
fallbackHttpConfig,
config,
);

expect(body).toHaveProperty('query');
expect(body).not.toHaveProperty('extensions');

expect(options.headers).toEqual({
accept: 'application/octet-stream',
'content-type': 'application/json',
});
});
});
15 changes: 14 additions & 1 deletion src/link/http/selectHttpOptionsAndBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const selectHttpOptionsAndBody = (
...config.options,
headers: {
...options.headers,
...config.headers,
...headersToLowerCase(config.headers),
},
};
if (config.credentials) options.credentials = config.credentials;
Expand All @@ -147,3 +147,16 @@ export const selectHttpOptionsAndBody = (
body,
};
};

function headersToLowerCase(
headers: Record<string, string> | undefined
): typeof headers {
if (headers) {
const normalized = Object.create(null);
Object.keys(Object(headers)).forEach(name => {
normalized[name.toLowerCase()] = headers[name];
});
return normalized;
}
return headers;
}

0 comments on commit ae0df47

Please sign in to comment.