From 531ffd6d58e9517da27a9d00a935a5bc805f6de1 Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Thu, 9 Jan 2020 09:35:08 +0100 Subject: [PATCH] Automated cherry pick of #3790 (#3791) * Handle lower case headers * Change var name --- app/init/fetch.js | 24 +++++++--------- app/init/fetch.test.js | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 app/init/fetch.test.js diff --git a/app/init/fetch.js b/app/init/fetch.js index bb8f2fb14..edd05091e 100644 --- a/app/init/fetch.js +++ b/app/init/fetch.js @@ -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) { diff --git a/app/init/fetch.test.js b/app/init/fetch.test.js new file mode 100644 index 000000000..316fc53c6 --- /dev/null +++ b/app/init/fetch.test.js @@ -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()]); + }); +}); \ No newline at end of file