Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: sinonFakeServer#respondWith(method, urlMatcher, body) overload #171

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/fake-server/index.js
Expand Up @@ -73,7 +73,8 @@ function matchOne(response, reqMethod, reqUrl) {
var matchUrl =
!url ||
url === reqUrl ||
(typeof url.test === "function" && url.test(reqUrl));
(typeof url.test === "function" && url.test(reqUrl)) ||
(typeof url === "function" && url(reqUrl) === true);

return matchMethod && matchUrl;
}
Expand Down
144 changes: 144 additions & 0 deletions lib/fake-server/index.test.js
Expand Up @@ -369,6 +369,18 @@ describe("sinonFakeServer", function() {
]);
});

it("responds to URL matched by url matcher function", function() {
this.server.respondWith(() => true, "FuncMatcher");

this.server.respond();

assert.equals(this.getPathAsync.respond.args[0], [
200,
{},
"FuncMatcher"
]);
});

it("does not respond to URL not matched by regexp", function() {
this.server.respondWith(/^\/p.*/, "No regexp match");

Expand All @@ -377,6 +389,14 @@ describe("sinonFakeServer", function() {
assert.equals(this.getRootAsync.respond.args[0], [404, {}, ""]);
});

it("does not respond to URL not matched by function url matcher", function() {
this.server.respondWith(() => false, "No function match");

this.server.respond();

assert.equals(this.getRootAsync.respond.args[0], [404, {}, ""]);
});

it("responds to all URLs matched by regexp", function() {
this.server.respondWith(/^\/.*/, "Match all URLs");

Expand All @@ -394,6 +414,23 @@ describe("sinonFakeServer", function() {
]);
});

it("responds to all URLs matched by function matcher", function() {
this.server.respondWith(() => true, "Match all URLs");

this.server.respond();

assert.equals(this.getRootAsync.respond.args[0], [
200,
{},
"Match all URLs"
]);
assert.equals(this.getPathAsync.respond.args[0], [
200,
{},
"Match all URLs"
]);
});

it("responds to all requests when match URL is falsy", function() {
this.server.respondWith("", "Falsy URL");

Expand All @@ -411,6 +448,23 @@ describe("sinonFakeServer", function() {
]);
});

it("responds to no requests when function matcher is falsy", function() {
this.server.respondWith(() => false, "Falsy URL");

this.server.respond();

assert.equals(this.getRootAsync.respond.args[0], [
404,
{},
""
]);
assert.equals(this.getPathAsync.respond.args[0], [
404,
{},
""
]);
});

it("responds to all GET requests", function() {
this.server.respondWith("GET", "", "All GETs");

Expand Down Expand Up @@ -856,6 +910,36 @@ describe("sinonFakeServer", function() {
assert(handler.calledOnce);
});

function equalMatcher(expected) {
return function(test) {
return expected === test;
};
}

it("yields response to request function handler when url is a function that returns true", function() {
var handler = sinon.spy();
this.server.respondWith("GET", equalMatcher("/hello?world"), handler);
var xhr = new FakeXMLHttpRequest();
xhr.open("GET", "/hello?world");
xhr.send();

this.server.respond();

assert(handler.calledOnce);
});

it("yields response to request function handler when url is a function that returns true with no Http Method specified", function() {
var handler = sinon.spy();
this.server.respondWith(equalMatcher("/hello?world"), handler);
var xhr = new FakeXMLHttpRequest();
xhr.open("GET", "/hello?world");
xhr.send();

this.server.respond();

assert(handler.calledOnce);
});

it("does not yield response to request function handler when method does not match", function() {
var handler = sinon.spy();
this.server.respondWith("GET", "/hello", handler);
Expand All @@ -868,6 +952,30 @@ describe("sinonFakeServer", function() {
assert(!handler.called);
});

it("does not yield response to request function handler when method does not match (using url mather function)", function() {
var handler = sinon.spy();
this.server.respondWith("GET", equalMatcher("/hello"), handler);
var xhr = new FakeXMLHttpRequest();
xhr.open("POST", "/hello");
xhr.send();

this.server.respond();

assert(!handler.called);
});

it("does not yield response to request function handler when method does not match (using url mather function)", function() {
var handler = sinon.spy();
this.server.respondWith("GET", equalMatcher("/hello"), handler);
var xhr = new FakeXMLHttpRequest();
xhr.open("POST", "/hello");
xhr.send();

this.server.respond();

assert(!handler.called);
});

it("yields response to request function handler when regexp url matches", function() {
var handler = sinon.spy();
this.server.respondWith("GET", /\/.*/, handler);
Expand All @@ -892,6 +1000,42 @@ describe("sinonFakeServer", function() {
assert(!handler.called);
});

it("does not yield response to request function handler when urlMatcher function returns false", function() {
var handler = sinon.spy();
this.server.respondWith("GET", equalMatcher("/goodbye"), handler);
var xhr = new FakeXMLHttpRequest();
xhr.open("GET", "/hello");
xhr.send();

this.server.respond();

assert(!handler.called);
});

it("does not yield response to request function handler when urlMatcher function returns non Boolean truthy value", function() {
var handler = sinon.spy();
this.server.respondWith("GET", () => "truthy", handler);
var xhr = new FakeXMLHttpRequest();
xhr.open("GET", "/hello");
xhr.send();

this.server.respond();

assert(!handler.called);
});

it("does not yield response to request function handler when urlMatcher function returns non Boolean falsey value", function() {
var handler = sinon.spy();
this.server.respondWith("GET", () => undefined, handler);
var xhr = new FakeXMLHttpRequest();
xhr.open("GET", "/hello");
xhr.send();

this.server.respond();

assert(!handler.called);
});

it("adds function handler without method or url filter", function() {
this.server.respondWith(function(xhr) {
xhr.respond(
Expand Down