Automated cherry pick of #3790 (#3791)

* Handle lower case headers

* Change var name
This commit is contained in:
Mattermost Build 2020-01-09 09:35:08 +01:00 committed by Saturnino Abril
parent 759fa9305b
commit 531ffd6d58
2 changed files with 74 additions and 14 deletions

View file

@ -17,8 +17,8 @@ import {t} from 'app/utils/i18n';
/* eslint-disable no-throw-literal */
const HEADER_X_CLUSTER_ID = 'X-Cluster-Id';
const HEADER_TOKEN = 'Token';
export const HEADER_X_CLUSTER_ID = 'X-Cluster-Id';
export const HEADER_TOKEN = 'Token';
let managedConfig;
@ -99,23 +99,19 @@ Client4.doFetchWithResponse = async (url, options) => {
});
}
if (headers[HEADER_X_CLUSTER_ID] || headers[HEADER_X_CLUSTER_ID.toLowerCase()]) {
const clusterId = headers[HEADER_X_CLUSTER_ID] || headers[HEADER_X_CLUSTER_ID.toLowerCase()];
if (clusterId && Client4.clusterId !== clusterId) {
Client4.clusterId = clusterId; /* eslint-disable-line require-atomic-updates */
}
const clusterId = headers[HEADER_X_CLUSTER_ID] || headers[HEADER_X_CLUSTER_ID.toLowerCase()];
if (clusterId && Client4.clusterId !== clusterId) {
Client4.clusterId = clusterId; /* eslint-disable-line require-atomic-updates */
}
if (headers[HEADER_TOKEN] || headers[HEADER_TOKEN.toLowerCase()]) {
const token = headers[HEADER_TOKEN] || headers[HEADER_TOKEN.toLowerCase()];
const token = headers[HEADER_TOKEN] || headers[HEADER_TOKEN.toLowerCase()];
if (token) {
Client4.setToken(token);
}
if (headers[HEADER_X_VERSION_ID] && !headers['Cache-Control']) {
const serverVersion = headers[HEADER_X_VERSION_ID];
if (serverVersion && Client4.serverVersion !== serverVersion) {
Client4.serverVersion = serverVersion; /* eslint-disable-line require-atomic-updates */
}
const serverVersion = headers[HEADER_X_VERSION_ID] || headers[HEADER_X_VERSION_ID.toLowerCase()];
if (serverVersion && !headers['Cache-Control'] && Client4.serverVersion !== serverVersion) {
Client4.serverVersion = serverVersion; /* eslint-disable-line require-atomic-updates */
}
if (response.ok) {

64
app/init/fetch.test.js Normal file
View file

@ -0,0 +1,64 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Client4} from 'mattermost-redux/client';
import {HEADER_X_VERSION_ID} from 'mattermost-redux/client/client4';
import {
HEADER_X_CLUSTER_ID,
HEADER_TOKEN,
} from 'app/init/fetch';
describe('Fetch', () => {
test('doFetchWithResponse handles empty headers', async () => {
const setToken = jest.spyOn(Client4, 'setToken');
const response = {
json: () => Promise.resolve('data'),
headers: {},
ok: true,
};
global.fetch.mockReturnValueOnce(response);
await Client4.doFetchWithResponse('https://mattermost.com', {method: 'GET'});
expect(Client4.serverVersion).toEqual('');
expect(Client4.clusterId).toEqual('');
expect(setToken).not.toHaveBeenCalled();
});
test('doFetchWithResponse handles title case headers', async () => {
const setToken = jest.spyOn(Client4, 'setToken');
const headers = {
[HEADER_X_VERSION_ID]: 'VersionId',
[HEADER_X_CLUSTER_ID]: 'ClusterId',
[HEADER_TOKEN]: 'Token',
};
const response = {
json: () => Promise.resolve('data'),
ok: true,
headers,
};
global.fetch.mockReturnValueOnce(response);
await Client4.doFetchWithResponse('https://mattermost.com', {method: 'GET'});
expect(Client4.serverVersion).toEqual(headers[HEADER_X_VERSION_ID]);
expect(Client4.clusterId).toEqual(headers[HEADER_X_CLUSTER_ID]);
expect(setToken).toHaveBeenCalledWith(headers[HEADER_TOKEN]);
});
test('doFetchWithResponse handles lower case headers', async () => {
const setToken = jest.spyOn(Client4, 'setToken');
const headers = {
[HEADER_X_VERSION_ID.toLowerCase()]: 'versionid',
[HEADER_X_CLUSTER_ID.toLowerCase()]: 'clusterid',
[HEADER_TOKEN.toLowerCase()]: 'token',
};
const response = {
json: () => Promise.resolve('data'),
ok: true,
headers,
};
global.fetch.mockReturnValueOnce(response);
await Client4.doFetchWithResponse('https://mattermost.com', {method: 'GET'});
expect(Client4.serverVersion).toEqual(headers[HEADER_X_VERSION_ID.toLowerCase()]);
expect(Client4.clusterId).toEqual(headers[HEADER_X_CLUSTER_ID.toLowerCase()]);
expect(setToken).toHaveBeenCalledWith(headers[HEADER_TOKEN.toLowerCase()]);
});
});