Skip to content

Commit ba7c111

Browse files
committed
feat: add validation for HTTP status code
1 parent e662488 commit ba7c111

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as isCreated } from './is-created';
22
export { default as isOk } from './is-ok';
33
export { default as findHttpStatus } from './find-http-status';
4+
export { default as validateHttpStatus } from './validate-http-status';

src/validate-http-status.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function validateHttpStatus(status, expectedStatus) {
2+
try {
3+
const isValid = status === expectedStatus;
4+
5+
if (!isValid) {
6+
throw new Error(`Expected a ${expectedStatus} response.`);
7+
}
8+
return isValid;
9+
} catch (e) {
10+
return e;
11+
}
12+
}
13+
14+
export default validateHttpStatus;

tests/validate-http-status.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { validateHttpStatus } from '../src';
2+
3+
const STATUS_EXPECTED = 200;
4+
5+
describe('validateHttpStatus', () => {
6+
test('it should validate http status 200', () => {
7+
const received = validateHttpStatus(200, STATUS_EXPECTED);
8+
expect(received).toBeTruthy();
9+
});
10+
11+
test('it should throw Error when wrong status be passed', () => {
12+
const received = validateHttpStatus(400, STATUS_EXPECTED);
13+
expect(received.message).toBe('Expected a 200 response.');
14+
});
15+
});

0 commit comments

Comments
 (0)