allow query string for stubbed requests to be in any order
This commit is contained in:
parent
a19e5b1bcd
commit
5ccd4827ee
@ -247,8 +247,15 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||||||
}
|
}
|
||||||
|
|
||||||
function RequestStub(url, stubData) {
|
function RequestStub(url, stubData) {
|
||||||
this.url = url;
|
var split = url.split('?');
|
||||||
this.data = stubData ? stubData.split('&').sort().join('&') : undefined;
|
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.andReturn = function(options) {
|
||||||
this.status = options.status || 200;
|
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.responseText = options.responseText;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.matches = function(url, data) {
|
this.matches = function(fullUrl, data) {
|
||||||
return this.url === url && (!this.data || this.data === (data && data.split('&').sort().join('&')));
|
var urlSplit = fullUrl.split('?'),
|
||||||
|
url = urlSplit[0],
|
||||||
|
query = urlSplit[1];
|
||||||
|
return this.url === url && this.query === normalizeQuery(query) && (!this.data || this.data === normalizeQuery(data));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
describe("Webmock style mocking", function() {
|
describe("Webmock style mocking", function() {
|
||||||
var successSpy, errorSpy, response, fakeGlobal, mockAjax;
|
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();
|
var xhr = new fakeGlobal.XMLHttpRequest();
|
||||||
xhr.onreadystatechange = function(arguments) {
|
xhr.onreadystatechange = function(arguments) {
|
||||||
if (this.readyState == this.DONE) {
|
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();
|
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() {
|
describe("stubbing with form data", function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
mockAjax.stubRequest("http://example.com/someApi", 'foo=bar&baz=quux').andReturn({responseText: "form", status: 201});
|
mockAjax.stubRequest("http://example.com/someApi", 'foo=bar&baz=quux').andReturn({responseText: "form", status: 201});
|
||||||
|
Loading…
Reference in New Issue
Block a user