Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configures redirectTo to accept regex #243

Merged
merged 1 commit into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
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