Skip to content

fix(audits): incorrect status code range #145

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions src/audits/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand Down
13 changes: 13 additions & 0 deletions src/audits/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/audits.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading