Skip to content

Commit da6f09c

Browse files
authored
feat: add legacyRoutes option (#223)
This adds the `legacyRoutes` option, defaulted to `true`. When `legacyRoutes` is true, the `?` character will automatically be escaped: ```ts server.respondWith('GET', '/hello?world', handler); ``` When it is false (future default), `?` has a special meaning in that it denotes which parameters in a path are optional. Due to this, it will not be escaped automatically: ```ts server.respondWith('GET', '/hello\\?world', handler); // so we can have optional params server.respondWith('GET', '/hello/:param?', handler); ```
1 parent c7b6ce7 commit da6f09c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/fake-server/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ var fakeServer = {
150150
fakeHTTPMethods: true,
151151
logger: true,
152152
unsafeHeadersEnabled: true,
153+
legacyRoutes: true,
153154
};
154155

155156
// eslint-disable-next-line no-param-reassign
@@ -213,6 +214,8 @@ var fakeServer = {
213214

214215
log: log,
215216

217+
legacyRoutes: true,
218+
216219
respondWith: function respondWith(method, url, body) {
217220
if (arguments.length === 1 && typeof method !== "function") {
218221
this.response = responseArray(method);
@@ -250,6 +253,13 @@ var fakeServer = {
250253
// eslint-disable-next-line no-param-reassign
251254
url = url.replace(/\/\*/g, "/(.*)");
252255
}
256+
257+
if (this.legacyRoutes) {
258+
if (url.includes("?")) {
259+
// eslint-disable-next-line no-param-reassign
260+
url = url.replace("?", "\\?");
261+
}
262+
}
253263
}
254264

255265
push.call(this.responses, {

lib/fake-server/index.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ describe("sinonFakeServer", function () {
103103
"fakeServer.create should not accept 'foo' settings",
104104
);
105105
});
106+
it("allows the 'legacyRoutes' setting", function () {
107+
var server = sinonFakeServer.create({
108+
legacyRoutes: false,
109+
});
110+
assert(
111+
server.legacyRoutes === false,
112+
"fakeServer.create should accept 'legacyRoutes' setting",
113+
);
114+
});
106115
});
107116

108117
it("fakes XMLHttpRequest", function () {
@@ -898,8 +907,9 @@ describe("sinonFakeServer", function () {
898907
assert(handler.calledOnce);
899908
});
900909

901-
it("yields response to request function handler when url contains RegExp characters", function () {
910+
it("yields response to handler when url contains escaped RegExp characters in non-legacy mode", function () {
902911
var handler = sinon.spy();
912+
this.server.legacyRoutes = false;
903913
this.server.respondWith("GET", "/hello\\?world", handler);
904914
var xhr = new FakeXMLHttpRequest();
905915
xhr.open("GET", "/hello?world");
@@ -910,6 +920,18 @@ describe("sinonFakeServer", function () {
910920
assert(handler.calledOnce);
911921
});
912922

923+
it("yields response to handler when url contains unescaped RegExp characters in legacy mode", function () {
924+
var handler = sinon.spy();
925+
this.server.respondWith("GET", "/hello?world", handler);
926+
var xhr = new FakeXMLHttpRequest();
927+
xhr.open("GET", "/hello?world");
928+
xhr.send();
929+
930+
this.server.respond();
931+
932+
assert(handler.calledOnce);
933+
});
934+
913935
function equalMatcher(expected) {
914936
return function (test) {
915937
return expected === test;

0 commit comments

Comments
 (0)