From eb9e4e0489dc23ae99abd5429c3c0c9e7d83f033 Mon Sep 17 00:00:00 2001 From: Varun Chandrasekar Date: Thu, 27 Jan 2022 23:54:14 -0800 Subject: [PATCH 1/3] Made the 'Content-type' response header detection case-insensitive --- src/index.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 46398fd3e..43249c7a8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -535,14 +535,20 @@ export default request * todo */ function getResult(response: Dom.Response): Promise { - const contentType = response.headers.get('Content-Type') - if (contentType && contentType.startsWith('application/json')) { + let contentType: string | undefined + + response.headers.forEach((value, key) => { + if (key.toLowerCase() === 'Content-Type'.toLowerCase()) { + contentType = value + } + }) + + if (contentType && contentType.toLowerCase().startsWith('application/json')) { return response.json() } else { return response.text() } } - /** * helpers */ From 0cfa26ae5fb73fae3e848727ac18ccffcc61ea32 Mon Sep 17 00:00:00 2001 From: Varun Chandrasekar Date: Fri, 28 Jan 2022 00:48:14 -0800 Subject: [PATCH 2/3] fix: Made content-type literal in check lower case --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 43249c7a8..903a2902b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -538,7 +538,7 @@ function getResult(response: Dom.Response): Promise { let contentType: string | undefined response.headers.forEach((value, key) => { - if (key.toLowerCase() === 'Content-Type'.toLowerCase()) { + if (key.toLowerCase() === 'content-type') { contentType = value } }) From 6675b55a5c576dd74d79d9b4ced34555ec0a6e0e Mon Sep 17 00:00:00 2001 From: Varun Chandrasekar Date: Fri, 28 Jan 2022 14:19:19 -0800 Subject: [PATCH 3/3] Added test --- tests/general.test.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/general.test.ts b/tests/general.test.ts index 8277a5c70..acc701bcd 100644 --- a/tests/general.test.ts +++ b/tests/general.test.ts @@ -1,5 +1,6 @@ import { GraphQLClient, rawRequest, request } from '../src' import { setupTestServer } from './__helpers' +import * as Dom from '../src/types.dom' const ctx = setupTestServer() @@ -149,3 +150,32 @@ test.skip('extra fetch options', async () => { ] `) }) + +test('case-insensitive content-type header for custom fetch', async () => { + const testData = { data: { test: 'test' } } + const testResponseHeaders = new Map() + testResponseHeaders.set('ConTENT-type', 'apPliCatiON/JSON') + + const options: Dom.RequestInit = { + fetch: function (url: string) { + return Promise.resolve({ + headers: testResponseHeaders, + data: testData, + json: function () { + return testData + }, + text: function () { + return JSON.stringify(testData) + }, + ok: true, + status: 200, + url, + }) + }, + } + + const client = new GraphQLClient(ctx.url, options) + const result = await client.request('{ test }') + + expect(result).toBe(testData.data) +})