Skip to content

refactor: use toBeBetween instead toBeGreaterThanOrEqual and toBeLessThanOrEqual #20

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

Merged
merged 4 commits into from
Nov 15, 2022
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
2 changes: 1 addition & 1 deletion implementations/thegraph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Status code 404 is not 200
```
4. MAY NOT allow executing mutations on GET requests<br />
```
Status code 200 is not greater than or equal to 400
Status code 200 is not between 400 and 499
```
5. SHOULD use 400 status code on missing {query} parameter when accepting application/graphql-response+json<br />
```
Expand Down
18 changes: 6 additions & 12 deletions src/audits/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
accept: 'application/graphql-response+json',
},
});
assert('Status code', res.status).toBeGreaterThanOrEqual(400);
assert('Status code', res.status).toBeLessThanOrEqual(499);
assert('Status code', res.status).toBeBetween(400, 499);
}),
// Request POST
audit(
Expand All @@ -221,8 +220,7 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
const res = await fetchFn(opts.url, {
method: 'POST',
});
assert('Status code', res.status).toBeGreaterThanOrEqual(400);
assert('Status code', res.status).toBeLessThanOrEqual(499);
assert('Status code', res.status).toBeBetween(400, 499);
},
),
audit('MUST accept application/json POST requests', async () => {
Expand Down Expand Up @@ -731,8 +729,7 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
},
body: '{ "not a JSON',
});
assert('Status code', res.status).toBeGreaterThanOrEqual(400);
assert('Status code', res.status).toBeLessThanOrEqual(599);
assert('Status code', res.status).toBeBetween(400, 499);
},
),
audit(
Expand Down Expand Up @@ -780,8 +777,7 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
qeury /* typo */: '{ __typename }',
}),
});
assert('Status code', res.status).toBeGreaterThanOrEqual(400);
assert('Status code', res.status).toBeLessThanOrEqual(599);
assert('Status code', res.status).toBeBetween(400, 599);
},
),
audit(
Expand Down Expand Up @@ -833,8 +829,7 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
query: '{',
}),
});
assert('Status code', res.status).toBeGreaterThanOrEqual(400);
assert('Status code', res.status).toBeLessThanOrEqual(599);
assert('Status code', res.status).toBeBetween(400, 599);
},
),
audit(
Expand Down Expand Up @@ -886,8 +881,7 @@ export function serverAudits(opts: ServerAuditOptions): Audit[] {
query: '{ 8f31403dfe404bccbb0e835f2629c6a7 }', // making sure the field doesnt exist
}),
});
assert('Status code', res.status).toBeGreaterThanOrEqual(400);
assert('Status code', res.status).toBeLessThanOrEqual(599);
assert('Status code', res.status).toBeBetween(400, 599);
},
),
audit(
Expand Down
14 changes: 6 additions & 8 deletions src/audits/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,12 @@ export function assert<T = unknown>(name: string, actual: T) {
throw `${name} ${actual} is not ${expected}`;
}
},
toBeLessThanOrEqual: (expected: T extends number ? T : never) => {
if (!(actual <= expected)) {
throw `${name} ${actual} is not less than or equal to ${expected}`;
}
},
toBeGreaterThanOrEqual: (expected: T extends number ? T : never) => {
if (!(actual >= expected)) {
throw `${name} ${actual} is not greater than or equal to ${expected}`;
toBeBetween: (
min: T extends number ? T : never,
max: T extends number ? T : never,
) => {
if (!(min <= actual && actual <= max)) {
throw `${name} ${actual} is not between ${min} and ${max}`;
}
},
toContain: (
Expand Down