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: default to json if client accepts all media types #6950

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion packages/api/src/utils/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/utils/server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function createFastifyHandler<E extends Endpoint>(
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 {
Expand Down
10 changes: 9 additions & 1 deletion packages/api/test/unit/utils/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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
Expand Down
Loading