Skip to content

Commit

Permalink
feat: add legacyRoutes option (#223)
Browse files Browse the repository at this point in the history
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);
```
  • Loading branch information
43081j committed May 15, 2024
1 parent c7b6ce7 commit da6f09c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/fake-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ var fakeServer = {
fakeHTTPMethods: true,
logger: true,
unsafeHeadersEnabled: true,
legacyRoutes: true,
};

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

log: log,

legacyRoutes: true,

respondWith: function respondWith(method, url, body) {
if (arguments.length === 1 && typeof method !== "function") {
this.response = responseArray(method);
Expand Down Expand Up @@ -250,6 +253,13 @@ var fakeServer = {
// eslint-disable-next-line no-param-reassign
url = url.replace(/\/\*/g, "/(.*)");
}

if (this.legacyRoutes) {
if (url.includes("?")) {
// eslint-disable-next-line no-param-reassign
url = url.replace("?", "\\?");
}
}
}

push.call(this.responses, {
Expand Down
24 changes: 23 additions & 1 deletion lib/fake-server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ describe("sinonFakeServer", function () {
"fakeServer.create should not accept 'foo' settings",
);
});
it("allows the 'legacyRoutes' setting", function () {
var server = sinonFakeServer.create({
legacyRoutes: false,
});
assert(
server.legacyRoutes === false,
"fakeServer.create should accept 'legacyRoutes' setting",
);
});
});

it("fakes XMLHttpRequest", function () {
Expand Down Expand Up @@ -898,8 +907,9 @@ describe("sinonFakeServer", function () {
assert(handler.calledOnce);
});

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

it("yields response to handler when url contains unescaped RegExp characters in legacy mode", function () {
var handler = sinon.spy();
this.server.respondWith("GET", "/hello?world", handler);
var xhr = new FakeXMLHttpRequest();
xhr.open("GET", "/hello?world");
xhr.send();

this.server.respond();

assert(handler.calledOnce);
});

function equalMatcher(expected) {
return function (test) {
return expected === test;
Expand Down

0 comments on commit da6f09c

Please sign in to comment.