diff --git a/src/audits/server.ts b/src/audits/server.ts index d7b4f37..1074d2e 100644 --- a/src/audits/server.ts +++ b/src/audits/server.ts @@ -560,26 +560,32 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] { ), audit( 'B6DC', - 'MAY use 4xx or 5xx status codes on JSON parsing failure', + 'MAY use 2xx, 4xx, or 5xx status codes on JSON parsing failure when accepting application/json', async () => { const res = await fetchFn(await getUrl(opts.url), { method: 'POST', headers: { 'content-type': 'application/json', + accept: 'application/json', }, body: '{ "not a JSON', }); - ressert(res).status.toBeBetween(400, 499); + ressert(res).status.toBeBetweenMultiple([ + [200, 299], + [400, 499], + [500, 599], + ]); }, ), audit( 'BCF8', - 'MAY use 400 status code on JSON parsing failure', + 'SHOULD use 400 status code on JSON parsing failure when accepting application/json', async () => { const res = await fetchFn(await getUrl(opts.url), { method: 'POST', headers: { 'content-type': 'application/json', + accept: 'application/json', }, body: '{ "not a JSON', }); diff --git a/src/audits/utils.ts b/src/audits/utils.ts index ed6f70b..d5f47e3 100644 --- a/src/audits/utils.ts +++ b/src/audits/utils.ts @@ -99,6 +99,19 @@ export function ressert(res: Response) { ); } }, + toBeBetweenMultiple: (ranges: Array<[number, number]>) => { + const isInRange = ranges.some( + ([min, max]) => min <= res.status && res.status <= max, + ); + if (!isInRange) { + throw new AuditError( + res, + `Response status is not between any of the provided ranges: ${ranges + .map(([min, max]) => `[${min}, ${max}]`) + .join(', ')}`, + ); + } + }, }, header(key: 'content-type') { return { diff --git a/tests/__snapshots__/audits.test.ts.snap b/tests/__snapshots__/audits.test.ts.snap index d8e9c5e..551b761 100644 --- a/tests/__snapshots__/audits.test.ts.snap +++ b/tests/__snapshots__/audits.test.ts.snap @@ -188,11 +188,11 @@ exports[`should not change globally unique audit ids 1`] = ` }, { "id": "B6DC", - "name": "MAY use 4xx or 5xx status codes on JSON parsing failure", + "name": "MAY use 2xx, 4xx, or 5xx status codes on JSON parsing failure when accepting application/json", }, { "id": "BCF8", - "name": "MAY use 400 status code on JSON parsing failure", + "name": "SHOULD use 400 status code on JSON parsing failure when accepting application/json", }, { "id": "8764",