From c6c179cffb86db4768787bad4026558c9dfe40e6 Mon Sep 17 00:00:00 2001 From: John Zhang Date: Fri, 11 Dec 2020 18:38:56 +0800 Subject: [PATCH 1/3] New sinonFakeServer#respondWith(method, urlMatcherFunc, body) overload, where urlMatcherFunc(requestUrl) returns true/false for request matching. --- lib/fake-server/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/fake-server/index.js b/lib/fake-server/index.js index b15b9df..cc70df5 100644 --- a/lib/fake-server/index.js +++ b/lib/fake-server/index.js @@ -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; } From 0558a90b55a1575d782f089d9af53e3e8d142372 Mon Sep 17 00:00:00 2001 From: John Zhang Date: Fri, 11 Dec 2020 18:39:20 +0800 Subject: [PATCH 2/3] New unit test for sinonFakeServer#respondWith(method, urlMatcherFunc, body) --- lib/fake-server/index.test.js | 144 ++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/lib/fake-server/index.test.js b/lib/fake-server/index.test.js index 66c3b4b..e43c17d 100644 --- a/lib/fake-server/index.test.js +++ b/lib/fake-server/index.test.js @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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); @@ -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); @@ -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( From fe9ad0bf35f7163a80acf3bdc877bd510dbf01e8 Mon Sep 17 00:00:00 2001 From: John Zhang Date: Fri, 11 Dec 2020 19:02:05 +0800 Subject: [PATCH 3/3] Fix eslint errors --- lib/fake-server/index.test.js | 66 ++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/lib/fake-server/index.test.js b/lib/fake-server/index.test.js index e43c17d..466058a 100644 --- a/lib/fake-server/index.test.js +++ b/lib/fake-server/index.test.js @@ -370,7 +370,9 @@ describe("sinonFakeServer", function() { }); it("responds to URL matched by url matcher function", function() { - this.server.respondWith(() => true, "FuncMatcher"); + this.server.respondWith(function() { + return true; + }, "FuncMatcher"); this.server.respond(); @@ -390,7 +392,9 @@ describe("sinonFakeServer", function() { }); it("does not respond to URL not matched by function url matcher", function() { - this.server.respondWith(() => false, "No function match"); + this.server.respondWith(function() { + return false; + }, "No function match"); this.server.respond(); @@ -415,7 +419,9 @@ describe("sinonFakeServer", function() { }); it("responds to all URLs matched by function matcher", function() { - this.server.respondWith(() => true, "Match all URLs"); + this.server.respondWith(function() { + return true; + }, "Match all URLs"); this.server.respond(); @@ -449,20 +455,14 @@ describe("sinonFakeServer", function() { }); it("responds to no requests when function matcher is falsy", function() { - this.server.respondWith(() => false, "Falsy URL"); + this.server.respondWith(function() { + return false; + }, "Falsy URL"); this.server.respond(); - assert.equals(this.getRootAsync.respond.args[0], [ - 404, - {}, - "" - ]); - assert.equals(this.getPathAsync.respond.args[0], [ - 404, - {}, - "" - ]); + assert.equals(this.getRootAsync.respond.args[0], [404, {}, ""]); + assert.equals(this.getPathAsync.respond.args[0], [404, {}, ""]); }); it("responds to all GET requests", function() { @@ -918,7 +918,11 @@ describe("sinonFakeServer", function() { 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); + this.server.respondWith( + "GET", + equalMatcher("/hello?world"), + handler + ); var xhr = new FakeXMLHttpRequest(); xhr.open("GET", "/hello?world"); xhr.send(); @@ -928,6 +932,7 @@ describe("sinonFakeServer", function() { assert(handler.calledOnce); }); + // eslint-disable-next-line max-len 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); @@ -952,18 +957,7 @@ 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); - }); - + // eslint-disable-next-line max-len 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); @@ -1012,9 +1006,16 @@ describe("sinonFakeServer", function() { assert(!handler.called); }); + // eslint-disable-next-line max-len 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); + this.server.respondWith( + "GET", + function() { + return "truthy"; + }, + handler + ); var xhr = new FakeXMLHttpRequest(); xhr.open("GET", "/hello"); xhr.send(); @@ -1024,9 +1025,16 @@ describe("sinonFakeServer", function() { assert(!handler.called); }); + // eslint-disable-next-line max-len 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); + this.server.respondWith( + "GET", + function() { + return undefined; + }, + handler + ); var xhr = new FakeXMLHttpRequest(); xhr.open("GET", "/hello"); xhr.send();