match form data in any order

This commit is contained in:
slackersoft 2014-01-17 11:38:27 -08:00
parent 75d0747e70
commit a19e5b1bcd
3 changed files with 18 additions and 7 deletions

View File

@ -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") {

View File

@ -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();

View File

@ -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!');