Skip to content
Browse files

Allow functions to handle requests through server.respondWith

  • Loading branch information...
1 parent e1b66fd commit 824d7e3ac54d52964495c2960f12f49bf2bdd060 @cjohansen cjohansen committed Jan 21, 2011
Showing with 125 additions and 16 deletions.
  1. +23 −16 lib/sinon/util/fake_server.js
  2. +102 −0 test/sinon/util/fake_server_test.js
View
39 lib/sinon/util/fake_server.js
@@ -26,11 +26,11 @@ sinon.fakeServer = (function () {
return new F();
}
- function responseArray(strOrArray) {
- var response = strOrArray;
+ function responseArray(handler) {
+ var response = handler;
- if (Object.prototype.toString.call(strOrArray) != "[object Array]") {
- response = [200, {}, strOrArray];
+ if (Object.prototype.toString.call(handler) != "[object Array]") {
+ response = [200, {}, handler];
}
if (typeof response[2] != "string") {
@@ -44,24 +44,29 @@ sinon.fakeServer = (function () {
var wloc = window.location;
var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host);
- function matchOne(response, requestMethod, requestUrl) {
+ function matchOne(response, reqMethod, reqUrl) {
var rmeth = response.method;
- var matchMethod = !rmeth || rmeth.toLowerCase() == requestMethod.toLowerCase();
+ var matchMethod = !rmeth || rmeth.toLowerCase() == reqMethod.toLowerCase();
var url = response.url;
- var matchUrl = !url || url == requestUrl || (typeof url.test == "function" && url.test(requestUrl));
+ var matchUrl = !url || url == reqUrl || (typeof url.test == "function" && url.test(reqUrl));
return matchMethod && matchUrl;
}
- function match(response, requestMethod, requestUrl) {
- if (matchOne(response, requestMethod, requestUrl)) {
- return true;
- }
+ function match(response, request) {
+ var requestMethod = this.getHTTPMethod(request);
+ var requestUrl = request.url;
if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
- var strippedUrl = requestUrl.replace(rCurrLoc, "");
+ requestUrl = requestUrl.replace(rCurrLoc, "");
+ }
- return matchOne(response, requestMethod, strippedUrl);
+ if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
+ if (typeof response.response == "function") {
+ return response.response(request);
+ }
+
+ return true;
}
return false;
@@ -136,7 +141,7 @@ sinon.fakeServer = (function () {
this.responses.push({
method: method,
url: url,
- response: responseArray(body)
+ response: typeof body == "function" ? body : responseArray(body)
});
}
},
@@ -161,14 +166,16 @@ sinon.fakeServer = (function () {
if (this.responses) {
for (var i = 0, l = this.responses.length; i < l; i++) {
- if (match(this.responses[i], this.getHTTPMethod(request), request.url)) {
+ if (match.call(this, this.responses[i], request)) {
response = this.responses[i].response;
break;
}
}
}
- request.respond(response[0], response[1], response[2]);
+ if (request.readyState != 4) {
+ request.respond(response[0], response[1], response[2]);
+ }
} catch (e) {}
},
View
102 test/sinon/util/fake_server_test.js
@@ -399,6 +399,108 @@ testCase("ServerRespondWithTest", {
}
});
+testCase("ServerRespondWithFunctionHandlerTest", {
+ setUp: function () {
+ this.server = sinon.fakeServer.create();
+ },
+
+ tearDown: function () {
+ this.server.restore();
+ },
+
+ "should yield response to request function handler": function () {
+ var handler = sinon.spy();
+ this.server.respondWith("/hello", handler);
+ var xhr = new sinon.FakeXMLHttpRequest();
+ xhr.open("GET", "/hello");
+ xhr.send();
+
+ this.server.respond();
+
+ assert(handler.calledOnce);
+ assert(handler.calledWith(xhr));
+ },
+
+ "should respond to request from function handler": function () {
+ this.server.respondWith("/hello", function (xhr) {
+ xhr.respond(200, { "Content-Type": "application/json" }, '{"id":42}');
+ });
+
+ var request = new sinon.FakeXMLHttpRequest();
+ request.open("GET", "/hello");
+ request.send();
+
+ this.server.respond();
+
+ assertEquals(200, request.status);
+ assertEquals({ "Content-Type": "application/json" }, request.responseHeaders);
+ assertEquals('{"id":42}', request.responseText);
+ },
+
+ "should yield response to request function handler when method matches": function () {
+ var handler = sinon.spy();
+ this.server.respondWith("GET", "/hello", handler);
+ var xhr = new sinon.FakeXMLHttpRequest();
+ xhr.open("GET", "/hello");
+ xhr.send();
+
+ this.server.respond();
+
+ assert(handler.calledOnce);
+ },
+
+ "should not yield response to request function handler when method does not match": function () {
+ var handler = sinon.spy();
+ this.server.respondWith("GET", "/hello", handler);
+ var xhr = new sinon.FakeXMLHttpRequest();
+ xhr.open("POST", "/hello");
+ xhr.send();
+
+ this.server.respond();
+
+ assert(!handler.called);
+ },
+
+ "should yield response to request function handler when regexp url matches": function () {
+ var handler = sinon.spy();
+ this.server.respondWith("GET", /\/.*/, handler);
+ var xhr = new sinon.FakeXMLHttpRequest();
+ xhr.open("GET", "/hello");
+ xhr.send();
+
+ this.server.respond();
+
+ assert(handler.calledOnce);
+ },
+
+ "should not yield response to request function handler when regexp url does not match": function () {
+ var handler = sinon.spy();
+ this.server.respondWith("GET", /\/a.*/, handler);
+ var xhr = new sinon.FakeXMLHttpRequest();
+ xhr.open("GET", "/hello");
+ xhr.send();
+
+ this.server.respond();
+
+ assert(!handler.called);
+ },
+
+ "should not process request further if processed by function": function () {
+ var handler = sinon.spy();
+ this.server.respondWith("GET", /\/a.*/, handler);
+ this.server.respondWith("GET", "/aloha", [200, {}, "Oh hi"]);
+ var xhr = new sinon.FakeXMLHttpRequest();
+ xhr.respond = sinon.spy();
+ xhr.open("GET", "/aloha");
+ xhr.send();
+
+ this.server.respond();
+
+ assert(handler.called);
+ assert(xhr.respond.calledOnce);
+ }
+});
+
testCase("ServerRespondFakeHTTPVerbTest", {
setUp: function () {
this.server = sinon.fakeServer.create();

0 comments on commit 824d7e3

Please sign in to comment.
Something went wrong with that request. Please try again.