diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index d622aad..7939669 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -259,14 +259,19 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. } function RequestStub(url, stubData) { - var split = url.split('?'); - this.url = split[0]; - var normalizeQuery = function(query) { return query ? query.split('&').sort().join('&') : undefined; }; - this.query = normalizeQuery(split[1]); + if (url instanceof RegExp) { + this.url = url; + this.query = undefined; + } else { + var split = url.split('?'); + this.url = split[0]; + this.query = split.length > 1 ? normalizeQuery(split[1]) : undefined; + } + this.data = normalizeQuery(stubData); this.andReturn = function(options) { @@ -277,10 +282,17 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }; this.matches = function(fullUrl, data) { - var urlSplit = fullUrl.split('?'), - url = urlSplit[0], - query = urlSplit[1]; - return this.url === url && this.query === normalizeQuery(query) && (!this.data || this.data === normalizeQuery(data)); + var matches = false; + fullUrl = fullUrl.toString(); + if (this.url instanceof RegExp) { + matches = this.url.test(fullUrl); + } else { + var urlSplit = fullUrl.split('?'), + url = urlSplit[0], + query = urlSplit[1]; + matches = this.url === url && this.query === normalizeQuery(query); + } + return matches && (!this.data || this.data === normalizeQuery(data)); }; } @@ -292,4 +304,3 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. jasmine.Ajax = new MockAjax(window); } }()); - diff --git a/spec/javascripts/webmock-style-spec.js b/spec/javascripts/webmock-style-spec.js index 425d72e..f99830e 100644 --- a/spec/javascripts/webmock-style-spec.js +++ b/spec/javascripts/webmock-style-spec.js @@ -77,6 +77,14 @@ describe("Webmock style mocking", function() { }); }); + it("allows stubs to use RegExp", function () { + mockAjax.stubRequest(/thisGuy/).andReturn({responseText: 'regex', status: 200}); + + sendRequest(fakeGlobal, 'http://www.example.com/thisGuy/thatGuy'); + expect(successSpy).toHaveBeenCalled(); + expect(response.responseText).toBe('regex'); + }); + describe("stubbing with form data", function() { beforeEach(function() { mockAjax.stubRequest("http://example.com/someApi", 'foo=bar&baz=quux').andReturn({responseText: "form", status: 201});