Skip to content

Commit

Permalink
feat(core): Default Response status code to 200
Browse files Browse the repository at this point in the history
Resolved #6.
  • Loading branch information
offirgolan committed Jun 11, 2018
1 parent 1a86846 commit f42a281
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
16 changes: 10 additions & 6 deletions packages/@pollyjs/core/src/-private/response.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import stringify from 'json-stable-stringify';
import assert from '../utils/assert';

const { freeze } = Object;

function formatHeader(name) {
return (name || '').toLowerCase();
}

function status(statusCode) {
return parseInt(statusCode, 10) || null;
}

export default class PollyResponse {
constructor(statusCode, headers, body) {
this.statusCode = status(statusCode);
this.status(statusCode || 200);
this.headers = headers || {};
this.body = body;
}
Expand All @@ -22,7 +19,14 @@ export default class PollyResponse {
}

status(statusCode) {
this.statusCode = status(statusCode);
const status = parseInt(statusCode, 10);

assert(
`[Response] Invalid status code: ${status}`,
status >= 100 && status < 600
);

this.statusCode = status;

return this;
}
Expand Down
19 changes: 15 additions & 4 deletions packages/@pollyjs/core/tests/unit/-private/response-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@ describe('Unit | Response', function() {
expect(new PollyResponse()).to.exist;
});

it('should have a default status code of 200', function() {
expect(new PollyResponse().statusCode).to.equal(200);
});

describe('API', function() {
beforeEach(function() {
response = new PollyResponse();
});

it('.status()', function() {
response.status(200);
expect(response.statusCode).to.equal(200);
[100, '404', 500, '599'].forEach(statusCode => {
expect(response.status(statusCode).statusCode).to.equal(
Number(statusCode)
);
});

response.status('400');
expect(response.statusCode).to.equal(400);
[null, '', 0, 99, 600, 999].forEach(statusCode => {
expect(() => response.status(statusCode)).to.throw(
Error,
/Invalid status code/
);
});
});

it('.getHeader()', function() {
Expand Down

0 comments on commit f42a281

Please sign in to comment.