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

feat: add legacyRoutes option #223

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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