From 9103f5655b05d5f74d036cec9760dc87b566163d Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Mon, 15 Jul 2024 16:19:52 +0100 Subject: [PATCH] fix: default to json if client accepts all media types (#6950) --- packages/api/src/utils/headers.ts | 7 ++++++- packages/api/src/utils/server/handler.ts | 2 +- packages/api/test/unit/utils/headers.test.ts | 10 +++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/api/src/utils/headers.ts b/packages/api/src/utils/headers.ts index 0646bb109fbb..5ef5346612fa 100644 --- a/packages/api/src/utils/headers.ts +++ b/packages/api/src/utils/headers.ts @@ -44,7 +44,12 @@ export function parseAcceptHeader(accept?: string, supported = SUPPORTED_MEDIA_T // Normalize here, using 1 as the default qvalue const quality = current.includes(";") ? current.split(";") : [current, "q=1"]; - const mediaType = quality[0].trim(); + let mediaType = quality[0].trim(); + + if (mediaType === "*/*") { + // Default to json if all media types are accepted + mediaType = MediaType.json; + } // If the mime type isn't acceptable, move on to the next entry if (!isSupportedMediaType(mediaType, supported)) { diff --git a/packages/api/src/utils/server/handler.ts b/packages/api/src/utils/server/handler.ts index 479212493885..a3cd9a43a56a 100644 --- a/packages/api/src/utils/server/handler.ts +++ b/packages/api/src/utils/server/handler.ts @@ -44,7 +44,7 @@ export function createFastifyHandler( if (definition.resp.isEmpty) { // Ignore Accept header, the response will be sent without body responseMediaType = null; - } else if (acceptHeader === undefined || acceptHeader === "*/*") { + } else if (acceptHeader === undefined) { // Default to json to not force user to set header, e.g. when using curl responseMediaType = MediaType.json; } else { diff --git a/packages/api/test/unit/utils/headers.test.ts b/packages/api/test/unit/utils/headers.test.ts index c3bcf6bc79c2..3416335b0dcd 100644 --- a/packages/api/test/unit/utils/headers.test.ts +++ b/packages/api/test/unit/utils/headers.test.ts @@ -5,13 +5,15 @@ describe("utils / headers", () => { describe("parseAcceptHeader", () => { const testCases: {header: string | undefined; expected: MediaType | null}[] = [ {header: undefined, expected: null}, - {header: "*/*", expected: null}, + {header: "*/*", expected: MediaType.json}, {header: "application/json", expected: MediaType.json}, {header: "application/octet-stream", expected: MediaType.ssz}, {header: "application/invalid", expected: null}, {header: "application/invalid;q=1,application/octet-stream;q=0.1", expected: MediaType.ssz}, {header: "application/octet-stream;q=0.5,application/json;q=1", expected: MediaType.json}, {header: "application/octet-stream;q=1,application/json;q=0.1", expected: MediaType.ssz}, + {header: "application/octet-stream;q=1,application/json;q=0.9", expected: MediaType.ssz}, + {header: "application/octet-stream;q=1,*/*;q=0.9", expected: MediaType.ssz}, {header: "application/octet-stream,application/json;q=0.1", expected: MediaType.ssz}, {header: "application/octet-stream;,application/json;q=0.1", expected: MediaType.json}, {header: "application/octet-stream;q=2,application/json;q=0.1", expected: MediaType.json}, @@ -20,6 +22,12 @@ describe("utils / headers", () => { {header: "application/octet-stream ; q=0.5 , application/json ; q=1", expected: MediaType.json}, {header: "application/octet-stream ; q=1 , application/json ; q=0.1", expected: MediaType.ssz}, {header: "application/octet-stream;q=1,application/json;q=0.1", expected: MediaType.ssz}, + { + // Default Accept header set by chrome browser + header: + "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", + expected: MediaType.json, + }, // The implementation is order dependent, however, RFC-9110 doesn't specify a preference. // The following tests serve to document the behavior at the time of implementation- not a