From a19e5b1bcddc5f809242bbd022d835edc6dc393f Mon Sep 17 00:00:00 2001 From: slackersoft Date: Fri, 17 Jan 2014 11:38:27 -0800 Subject: [PATCH] match form data in any order --- lib/mock-ajax.js | 10 +++++++--- spec/javascripts/mock-ajax-spec.js | 2 +- spec/javascripts/webmock-style-spec.js | 13 ++++++++++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index 05b0408..8625993 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -88,7 +88,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. this.findStub = function(url, data) { for (var i = stubs.length - 1; i >= 0; i--) { var stub = stubs[i]; - if (stub.url === url && (!stub.data || stub.data === data)) { + if (stub.matches(url, data)) { return stub; } } @@ -246,9 +246,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }; } - function RequestStub(url, data) { + function RequestStub(url, stubData) { this.url = url; - this.data = data; + this.data = stubData ? stubData.split('&').sort().join('&') : undefined; this.andReturn = function(options) { this.status = options.status || 200; @@ -256,6 +256,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. this.contentType = options.contentType; this.responseText = options.responseText; }; + + this.matches = function(url, data) { + return this.url === url && (!this.data || this.data === (data && data.split('&').sort().join('&'))); + }; } if (typeof window === "undefined" && typeof exports === "object") { diff --git a/spec/javascripts/mock-ajax-spec.js b/spec/javascripts/mock-ajax-spec.js index 6a869f4..7cef571 100644 --- a/spec/javascripts/mock-ajax-spec.js +++ b/spec/javascripts/mock-ajax-spec.js @@ -33,7 +33,7 @@ describe("mockAjax", function() { mockAjax.install(); mockAjax.requests.track({url: '/testurl'}); - mockAjax.stubs.addStub({url: '/bobcat'}); + mockAjax.stubRequest('/bobcat'); expect(mockAjax.requests.count()).toEqual(1); expect(mockAjax.stubs.findStub('/bobcat')).toBeDefined(); diff --git a/spec/javascripts/webmock-style-spec.js b/spec/javascripts/webmock-style-spec.js index a7b3d74..ce25963 100644 --- a/spec/javascripts/webmock-style-spec.js +++ b/spec/javascripts/webmock-style-spec.js @@ -66,7 +66,7 @@ describe("Webmock style mocking", function() { describe("stubbing with form data", function() { beforeEach(function() { - mockAjax.stubRequest("http://example.com/someApi", 'foo=bar').andReturn({responseText: "form", status: 201}); + mockAjax.stubRequest("http://example.com/someApi", 'foo=bar&baz=quux').andReturn({responseText: "form", status: 201}); }); var postRequest = function(data) { @@ -83,14 +83,21 @@ describe("Webmock style mocking", function() { }; it("uses the form data stub when the data matches", function() { - postRequest('foo=bar'); + postRequest('foo=bar&baz=quux'); + + expect(response.status).toEqual(201); + expect(response.responseText).toEqual('form'); + }); + + it("matches data params in any order", function() { + postRequest('baz=quux&foo=bar'); expect(response.status).toEqual(201); expect(response.responseText).toEqual('form'); }); it("falls back to the stub without data specified if the data doesn't match", function() { - postRequest('foo=baz'); + postRequest('foo=bar'); expect(response.status).toEqual(200); expect(response.responseText).toEqual('hi!');