Skip to content

Commit f688127

Browse files
committed
fix(audits): outdated B6DC status code ranges
Relevant section for status codes 2xx and 5xx: 6.4.1 application/json https://graphql.github.io/graphql-over-http/draft/#sel-FANNLVCCBCIsB7xT Relevant section for Status code 400: 6.4.1.1.1 JSON parsing failure https://graphql.github.io/graphql-over-http/draft/#sec-application-json.Examples.JSON-parsing-failure
1 parent 5587c03 commit f688127

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/audits/server.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,16 +560,21 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
560560
),
561561
audit(
562562
'B6DC',
563-
'MAY use 4xx or 5xx status codes on JSON parsing failure',
563+
'MAY use 2xx, 400, or 5xx status codes on JSON parsing failure when accepting application/json',
564564
async () => {
565565
const res = await fetchFn(await getUrl(opts.url), {
566566
method: 'POST',
567567
headers: {
568568
'content-type': 'application/json',
569+
accept: 'application/json',
569570
},
570571
body: '{ "not a JSON',
571572
});
572-
ressert(res).status.toBeBetween(400, 499);
573+
ressert(res).status.toBeBetweenMultiple([
574+
[200, 299],
575+
[400, 400],
576+
[500, 599],
577+
]);
573578
},
574579
),
575580
audit(

src/audits/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ export function ressert(res: Response) {
9999
);
100100
}
101101
},
102+
toBeBetweenMultiple: (ranges: Array<[number, number]>) => {
103+
const isInRange = ranges.some(
104+
([min, max]) => min <= res.status && res.status <= max,
105+
);
106+
if (!isInRange) {
107+
throw new AuditError(
108+
res,
109+
`Response status is not between any of the provided ranges: ${ranges
110+
.map(([min, max]) => `[${min}, ${max}]`)
111+
.join(', ')}`,
112+
);
113+
}
114+
},
102115
},
103116
header(key: 'content-type') {
104117
return {

tests/__snapshots__/audits.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ exports[`should not change globally unique audit ids 1`] = `
188188
},
189189
{
190190
"id": "B6DC",
191-
"name": "MAY use 4xx or 5xx status codes on JSON parsing failure",
191+
"name": "MAY use 2xx, 400, or 5xx status codes on JSON parsing failure when accepting application/json",
192192
},
193193
{
194194
"id": "BCF8",

0 commit comments

Comments
 (0)