Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

allow query string for stubbed requests to be in any order

  • Loading branch information...
commit 5ccd4827ee7f1170d77a2543d2f160b90d4e00a5 1 parent a19e5b1
@slackersoft slackersoft authored
Showing with 29 additions and 6 deletions.
  1. +14 −4 lib/mock-ajax.js
  2. +15 −2 spec/javascripts/webmock-style-spec.js
View
18 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));
};
}
View
17 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});
Please sign in to comment.
Something went wrong with that request. Please try again.