From b5e814421440d6c8b46b69fa02947260dae6ad0e Mon Sep 17 00:00:00 2001 From: mikemfleming Date: Tue, 16 Apr 2019 10:10:55 -0500 Subject: [PATCH] feat: configures redirectTo to accept regex --- README.md | 3 ++- lib/http.js | 12 ++++++++++-- test/http.js | 16 ++++++++++++++++ types/index.d.ts | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bf462d2..3c069d5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/http.js b/lib/http.js index 9b97eb9..4dc8cc5 100644 --- a/lib/http.js +++ b/lib/http.js @@ -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 */ @@ -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 ') ); diff --git a/test/http.js b/test/http.js index 995ed41..76ba716 100644 --- a/test/http.js +++ b/test/http.js @@ -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'); @@ -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 () { diff --git a/types/index.d.ts b/types/index.d.ts index c1fb8b3..0f79c09 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -23,7 +23,7 @@ declare global { } interface Assertion { - redirectTo(location: string): Assertion; + redirectTo(location: string|RegExp): Assertion; param(key: string, value?: string): Assertion;