Skip to content

Commit

Permalink
feat: configures redirectTo to accept regex
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemfleming authored and keithamus committed Apr 17, 2019
1 parent 24ef17b commit 9b8fea3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -332,12 +332,13 @@ expect(res).to.not.redirect;

### .redirectTo

* **@param** _{String}_ location url
* **@param** _{String|RegExp}_ location url

Assert that a `Response` object redirects to the supplied location.

```js
expect(res).to.redirectTo('http://example.com');
expect(res).to.redirectTo(/^\/search\/results\?orderBy=desc$/);
```

### .param
Expand Down
12 changes: 10 additions & 2 deletions lib/http.js
Expand Up @@ -273,7 +273,7 @@ module.exports = function (chai, _) {
* expect(res).to.redirectTo('http://example.com');
* ```
*
* @param {String} location url
* @param {String|RegExp} location url
* @name redirectTo
* @api public
*/
Expand All @@ -284,8 +284,16 @@ 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.some(redirect => destination.test(redirect));

} 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
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
2 changes: 1 addition & 1 deletion types/index.d.ts
Expand Up @@ -23,7 +23,7 @@ declare global {
}

interface Assertion {
redirectTo(location: string): Assertion;
redirectTo(location: string|RegExp): Assertion;

param(key: string, value?: string): Assertion;

Expand Down

0 comments on commit 9b8fea3

Please sign in to comment.