Skip to content

Commit f42a281

Browse files
committed
feat(core): Default Response status code to 200
Resolved #6.
1 parent 1a86846 commit f42a281

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

packages/@pollyjs/core/src/-private/response.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import stringify from 'json-stable-stringify';
2+
import assert from '../utils/assert';
23

34
const { freeze } = Object;
45

56
function formatHeader(name) {
67
return (name || '').toLowerCase();
78
}
89

9-
function status(statusCode) {
10-
return parseInt(statusCode, 10) || null;
11-
}
12-
1310
export default class PollyResponse {
1411
constructor(statusCode, headers, body) {
15-
this.statusCode = status(statusCode);
12+
this.status(statusCode || 200);
1613
this.headers = headers || {};
1714
this.body = body;
1815
}
@@ -22,7 +19,14 @@ export default class PollyResponse {
2219
}
2320

2421
status(statusCode) {
25-
this.statusCode = status(statusCode);
22+
const status = parseInt(statusCode, 10);
23+
24+
assert(
25+
`[Response] Invalid status code: ${status}`,
26+
status >= 100 && status < 600
27+
);
28+
29+
this.statusCode = status;
2630

2731
return this;
2832
}

packages/@pollyjs/core/tests/unit/-private/response-test.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,28 @@ describe('Unit | Response', function() {
99
expect(new PollyResponse()).to.exist;
1010
});
1111

12+
it('should have a default status code of 200', function() {
13+
expect(new PollyResponse().statusCode).to.equal(200);
14+
});
15+
1216
describe('API', function() {
1317
beforeEach(function() {
1418
response = new PollyResponse();
1519
});
1620

1721
it('.status()', function() {
18-
response.status(200);
19-
expect(response.statusCode).to.equal(200);
22+
[100, '404', 500, '599'].forEach(statusCode => {
23+
expect(response.status(statusCode).statusCode).to.equal(
24+
Number(statusCode)
25+
);
26+
});
2027

21-
response.status('400');
22-
expect(response.statusCode).to.equal(400);
28+
[null, '', 0, 99, 600, 999].forEach(statusCode => {
29+
expect(() => response.status(statusCode)).to.throw(
30+
Error,
31+
/Invalid status code/
32+
);
33+
});
2334
});
2435

2536
it('.getHeader()', function() {

0 commit comments

Comments
 (0)