Allow stubs to match only a particular http method (GET, POST, PUT, etc.)

Fixes #36
This commit is contained in:
slackersoft 2014-07-10 12:44:56 -07:00
parent b6a0020b84
commit 199324e720
2 changed files with 29 additions and 10 deletions

View File

@ -69,8 +69,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
paramParser.reset(); paramParser.reset();
}; };
this.stubRequest = function(url, data) { this.stubRequest = function(url, data, method) {
var stub = new RequestStub(url, data); var stub = new RequestStub(url, data, method);
stubTracker.addStub(stub); stubTracker.addStub(stub);
return stub; return stub;
}; };
@ -103,10 +103,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
stubs = []; stubs = [];
}; };
this.findStub = function(url, data) { this.findStub = function(url, data, method) {
for (var i = stubs.length - 1; i >= 0; i--) { for (var i = stubs.length - 1; i >= 0; i--) {
var stub = stubs[i]; var stub = stubs[i];
if (stub.matches(url, data)) { if (stub.matches(url, data, method)) {
return stub; return stub;
} }
} }
@ -210,7 +210,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
this.readyState = 2; this.readyState = 2;
this.onreadystatechange(); this.onreadystatechange();
var stub = stubTracker.findStub(this.url, data); var stub = stubTracker.findStub(this.url, data, this.method);
if (stub) { if (stub) {
this.response(stub); this.response(stub);
} }
@ -319,7 +319,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}; };
} }
function RequestStub(url, stubData) { function RequestStub(url, stubData, method) {
var normalizeQuery = function(query) { var normalizeQuery = function(query) {
return query ? query.split('&').sort().join('&') : undefined; return query ? query.split('&').sort().join('&') : undefined;
}; };
@ -334,6 +334,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
} }
this.data = normalizeQuery(stubData); this.data = normalizeQuery(stubData);
this.method = method;
this.andReturn = function(options) { this.andReturn = function(options) {
this.status = options.status || 200; this.status = options.status || 200;
@ -342,7 +343,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
this.responseText = options.responseText; this.responseText = options.responseText;
}; };
this.matches = function(fullUrl, data) { this.matches = function(fullUrl, data, method) {
var matches = false; var matches = false;
fullUrl = fullUrl.toString(); fullUrl = fullUrl.toString();
if (this.url instanceof RegExp) { if (this.url instanceof RegExp) {
@ -353,7 +354,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
query = urlSplit[1]; query = urlSplit[1];
matches = this.url === url && this.query === normalizeQuery(query); matches = this.url === url && this.query === normalizeQuery(query);
} }
return matches && (!this.data || this.data === normalizeQuery(data)); return matches && (!this.data || this.data === normalizeQuery(data)) && (!this.method || this.method === method);
}; };
} }

View File

@ -1,8 +1,9 @@
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, url) { var sendRequest = function(fakeGlobal, url, method) {
url = url || "http://example.com/someApi" url = url || "http://example.com/someApi"
method = method || 'GET';
var xhr = new fakeGlobal.XMLHttpRequest(); var xhr = new fakeGlobal.XMLHttpRequest();
xhr.onreadystatechange = function(arguments) { xhr.onreadystatechange = function(arguments) {
if (this.readyState == (this.DONE || 4)) { // IE 8 doesn't support DONE if (this.readyState == (this.DONE || 4)) { // IE 8 doesn't support DONE
@ -11,7 +12,7 @@ describe("Webmock style mocking", function() {
} }
}; };
xhr.open("GET", url); xhr.open(method, url);
xhr.send(); xhr.send();
}; };
@ -65,6 +66,23 @@ describe("Webmock style mocking", function() {
}); });
}); });
describe('stubs with method specified', function() {
beforeEach(function() {
mockAjax.stubRequest('http://example.com/myApi', null, 'POST').andReturn({responseText: 'post', status: '201'});
mockAjax.stubRequest('http://example.com/myApi', null, 'PUT').andReturn({responseText: 'put', status: '200'});
});
it("does not match a different method", function() {
sendRequest(fakeGlobal, 'http://example.com/myApi', 'GET');
expect(successSpy).not.toHaveBeenCalled();
});
it("matches with the right method", function() {
sendRequest(fakeGlobal, 'http://example.com/myApi', 'POST');
expect(response.responseText).toEqual('post');
});
});
describe("with a query string", function() { describe("with a query string", function() {
beforeEach(function() { beforeEach(function() {
mockAjax.stubRequest("http://example.com/someApi?foo=bar&baz=quux").andReturn({responseText: "greetings", status: 422}); mockAjax.stubRequest("http://example.com/someApi?foo=bar&baz=quux").andReturn({responseText: "greetings", status: 422});