From 5ccd4827ee7f1170d77a2543d2f160b90d4e00a5 Mon Sep 17 00:00:00 2001 From: slackersoft Date: Tue, 21 Jan 2014 17:06:23 -1000 Subject: [PATCH] allow query string for stubbed requests to be in any order --- lib/mock-ajax.js | 18 ++++++++++++++---- spec/javascripts/webmock-style-spec.js | 17 +++++++++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index 8625993..9bd8c3f 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -247,8 +247,15 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. } function RequestStub(url, stubData) { - this.url = url; - this.data = stubData ? stubData.split('&').sort().join('&') : undefined; + var split = url.split('?'); + this.url = split[0]; + + var normalizeQuery = function(query) { + return query ? query.split('&').sort().join('&') : undefined; + }; + + this.query = normalizeQuery(split[1]); + this.data = normalizeQuery(stubData); this.andReturn = function(options) { this.status = options.status || 200; @@ -257,8 +264,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. this.responseText = options.responseText; }; - this.matches = function(url, data) { - return this.url === url && (!this.data || this.data === (data && data.split('&').sort().join('&'))); + 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)); }; } diff --git a/spec/javascripts/webmock-style-spec.js b/spec/javascripts/webmock-style-spec.js index ce25963..120c9e2 100644 --- a/spec/javascripts/webmock-style-spec.js +++ b/spec/javascripts/webmock-style-spec.js @@ -1,7 +1,8 @@ describe("Webmock style mocking", function() { var successSpy, errorSpy, response, fakeGlobal, mockAjax; - var sendRequest = function(fakeGlobal) { + var sendRequest = function(fakeGlobal, url) { + url = url || "http://example.com/someApi" var xhr = new fakeGlobal.XMLHttpRequest(); xhr.onreadystatechange = function(arguments) { if (this.readyState == this.DONE) { @@ -10,7 +11,7 @@ describe("Webmock style mocking", function() { } }; - xhr.open("GET", "http://example.com/someApi"); + xhr.open("GET", url); xhr.send(); }; @@ -64,6 +65,18 @@ describe("Webmock style mocking", function() { }); }); + describe("with a query string", function() { + beforeEach(function() { + mockAjax.stubRequest("http://example.com/someApi?foo=bar&baz=quux").andReturn({responseText: "greetings", status: 422}); + }); + + it("should match the query string in any order", function() { + sendRequest(fakeGlobal, "http://example.com/someApi?baz=quux&foo=bar"); + expect(response.status).toEqual(422); + expect(response.responseText).toEqual('greetings'); + }); + }); + describe("stubbing with form data", function() { beforeEach(function() { mockAjax.stubRequest("http://example.com/someApi", 'foo=bar&baz=quux').andReturn({responseText: "form", status: 201});