Skip to content

Commit

Permalink
Modified assertions to work for both Node and web browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMessinger committed Feb 1, 2016
1 parent 644a37e commit 1c5194f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
18 changes: 9 additions & 9 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ module.exports = function (chai, _) {
*/

Assertion.addMethod('status', function (code) {
new Assertion(this._obj).to.have.property('statusCode');
var statusCode = this._obj.statusCode;
new Assertion(this._obj).to.have.property('status');
var status = this._obj.status;

this.assert(
statusCode == code
status == code
, 'expected #{this} to have status code #{exp} but got #{act}'
, 'expected #{this} to not have status code #{act}'
, code
, statusCode
, status
);
});

Expand Down Expand Up @@ -232,13 +232,13 @@ module.exports = function (chai, _) {

Assertion.addProperty('redirect', function() {
var redirectCodes = [301, 302, 303]
, statusCode = this._obj.statusCode
, status = this._obj.status
, redirects = this._obj.redirects;

this.assert(
redirectCodes.indexOf(statusCode) >= 0 || redirects && redirects.length
, "expected redirect with 30{1-3} status code but got " + statusCode
, "expected not to redirect but got " + statusCode + " status"
redirectCodes.indexOf(status) >= 0 || redirects && redirects.length
, "expected redirect with 30{1-3} status code but got " + status
, "expected not to redirect but got " + status + " status"
);
});

Expand Down Expand Up @@ -325,7 +325,7 @@ module.exports = function (chai, _) {
, cookie;

if (!header) {
header = getHeader(this._obj, 'cookie').split(';');
header = (getHeader(this._obj, 'cookie') || '').split(';');
}

cookie = Cookie.CookieJar();
Expand Down
36 changes: 18 additions & 18 deletions test/http.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
describe('assertions', function () {

it('#status', function () {
var res = { statusCode: 200 };
var res = { status: 200 };
res.should.to.have.status(200);

(function () {
res.should.not.have.status(200);
}).should.throw('expected { statusCode: 200 } to not have status code 200');
}).should.throw('expected { status: 200 } to not have status code 200');

(function () {
({}).should.not.to.have.status(200);
}).should.throw("expected {} to have a property 'statusCode'");
}).should.throw("expected {} to have a property 'status'");
});

it('#ip', function () {
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('assertions', function () {

(function () {
res.should.not.have.headers;
}).should.throw('expected { getHeader: [Function] } to not have headers or getHeader method');
}).should.throw(/expected .*getHeader.* to not have headers or getHeader method/);
});

it('#json', function() {
Expand Down Expand Up @@ -169,60 +169,60 @@ describe('assertions', function () {
});

it('#redirect', function () {
var res = { statusCode: 200 };
var res = { status: 200 };
res.should.not.redirect;

[301, 302, 303].forEach(function (statusCode) {
var res = { statusCode: statusCode };
[301, 302, 303].forEach(function (status) {
var res = { status: status };
res.should.redirect;
});

({
statusCode: 200,
status: 200,
redirects: ['http://example.com']
}).should.redirect;

({
statusCode: 200,
status: 200,
redirects: []
}).should.not.redirect;

(function () {
var res = { statusCode: 200 };
var res = { status: 200 };
res.should.redirect;
}).should.throw('expected redirect with 30{1-3} status code but got 200');

(function () {
var res = { statusCode: 301 };
var res = { status: 301 };
res.should.not.redirect;
}).should.throw('expected not to redirect but got 301 status');
});

it('#redirectTo', function () {
var res = { statusCode: 301, headers: { location: 'foo' } };
var res = { status: 301, headers: { location: 'foo' } };
res.should.redirectTo('foo');

res = { statusCode: 301, headers: { location: 'bar' } };
res = { status: 301, headers: { location: 'bar' } };
res.should.not.redirectTo('foo');

res = { statusCode: 200, redirects: ['bar'] };
res = { status: 200, redirects: ['bar'] };
res.should.redirectTo('bar');

res = { statusCode: 200, redirects: ['bar'] };
res = { status: 200, redirects: ['bar'] };
res.should.not.redirectTo('foo');

(function () {
var res = { statusCode: 301, headers: { location: 'foo' } };
var res = { status: 301, headers: { location: 'foo' } };
res.should.not.redirectTo('foo');
}).should.throw('expected header \'location\' to not have value foo');

(function () {
var res = { statusCode: 301, headers: { location: 'bar' } };
var res = { status: 301, headers: { location: 'bar' } };
res.should.redirectTo('foo');
}).should.throw('expected header \'location\' to have value foo');

(function () {
var res = { statusCode: 200, redirects: ['bar', 'baz'] };
var res = { status: 200, redirects: ['bar', 'baz'] };
res.should.redirectTo('foo');
}).should.throw('expected redirect to foo but got bar then baz');
});
Expand Down

0 comments on commit 1c5194f

Please sign in to comment.