Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): headers set by the sdk must respect case insensitive keys #627

Merged
merged 7 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2020 Cognite AS

// @ts-ignore
import {
headersWithDefaultField,
HttpHeaders,
} from '../../httpClient/basicHttpClient';

function lengthOfHttpHeaders(headers?: HttpHeaders): number {
let counter = 0;
for (const _ in headers) {
counter += 1;
}
return counter;
}

describe('basicHttpClient', () => {
describe('headersWithDefaultField', () => {
test('add missing', () => {
const emptyHeaders: HttpHeaders = {};
const alteredEmptyHeaders = headersWithDefaultField(
emptyHeaders,
'Accept',
'application/json'
);
expect(lengthOfHttpHeaders(alteredEmptyHeaders)).toEqual(1);
expect('Accept' in alteredEmptyHeaders).toBeTruthy();
});
test('not overwrite existing', () => {
const mediaType = 'image/png';
const emptyHeaders: HttpHeaders = {
Accept: mediaType,
};
const alteredEmptyHeaders = headersWithDefaultField(
emptyHeaders,
'Accept',
'application/json'
);
expect(lengthOfHttpHeaders(alteredEmptyHeaders)).toEqual(1);
expect('Accept' in alteredEmptyHeaders).toBeTruthy();
expect(alteredEmptyHeaders['Accept']).toEqual(mediaType);
});
test('to be case insensitive', () => {
const mediaType = 'image/png';
const emptyHeaders: HttpHeaders = {
accept: mediaType,
};
const alteredEmptyHeaders = headersWithDefaultField(
emptyHeaders,
'Accept',
'application/json'
);
expect(lengthOfHttpHeaders(alteredEmptyHeaders)).toEqual(1);
expect('accept' in alteredEmptyHeaders).toBeTruthy();
expect(alteredEmptyHeaders['accept']).toEqual(mediaType);
});
});
});
30 changes: 26 additions & 4 deletions packages/core/src/httpClient/basicHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ export class BasicHttpClient {
request: HttpRequest
): Promise<HttpResponse<ResponseType>> {
const url = this.constructUrl(request.path, request.params);
const headers: HttpHeaders = {
Accept: 'application/json',
...request.headers,
};
const headers = headersWithDefaultField(
request.headers,
'Accept',
'application/json'
);
let body = request.data;
if (isJson(body)) {
body = BasicHttpClient.transformRequestBody(body);
Expand Down Expand Up @@ -224,6 +225,27 @@ export class BasicHttpClient {
}
}

function lowercaseHeadersKeys(headers: HttpHeaders): string[] {
const keys: string[] = [];
for (const key in headers) {
keys.push(key.toLowerCase());
}
return keys;
}

export function headersWithDefaultField(
headers: HttpHeaders = {},
fieldName: string,
fieldValue: string
): HttpHeaders {
const lowercaseHeaders = lowercaseHeadersKeys(headers);
const lowercaseKey = fieldName.toLowerCase();
if (!lowercaseHeaders.includes(lowercaseKey)) {
headers[fieldName] = fieldValue;
}
return headers;
}

export interface HttpRequest extends HttpRequestOptions {
path: string;
method: HttpMethod;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as Constants from './constants';
import * as GraphUtils from './graphUtils';
import * as TestUtils from './testUtils';

export * from './types';

export { MetadataMap } from './metadata';
Expand Down