Skip to content

Commit

Permalink
Merge 69ca903 into 24ef17b
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike M. Fleming committed Apr 16, 2019
2 parents 24ef17b + 69ca903 commit a1a4467
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,15 @@ module.exports = function (chai, _) {
new Assertion(this._obj).to.redirect;

if(redirects && redirects.length) {
var hasRedirected;

if (Object.prototype.toString.call(destination) === '[object RegExp]') {
hasRedirected = redirects.reduce((flag, redirect) => flag || destination.test(redirect), false);
} else {
hasRedirected = redirects.indexOf(destination) > -1;
}
this.assert(
redirects.indexOf(destination) > -1
hasRedirected
, 'expected redirect to ' + destination + ' but got ' + redirects.join(' then ')
, 'expected not to redirect to ' + destination + ' but got ' + redirects.join(' then ')
);
Expand Down
16 changes: 16 additions & 0 deletions test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ describe('assertions', function () {
res = { status: 200, redirects: ['bar'] };
res.should.not.redirectTo('foo');

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

res = { status: 200, redirects: ['foo/bar?baz=qux'] };
res.should.redirectTo(/^foo\/bar/);

(function () {
var res = { status: 301, headers: { location: 'foo' } };
res.should.not.redirectTo('foo');
Expand All @@ -237,6 +243,16 @@ describe('assertions', function () {
var res = { status: 200, redirects: ['bar', 'baz'] };
res.should.redirectTo('foo');
}).should.throw('expected redirect to foo but got bar then baz');

(function () {
var res = { status: 301, headers: { location: 'foo' } };
res.should.not.redirectTo(/foo/);
}).should.throw('expected header \'location\' not to match /foo/ but got \'foo\'');

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

it('#param', function () {
Expand Down

0 comments on commit a1a4467

Please sign in to comment.