Skip to content

Commit

Permalink
feat: upgrade path-to-regexp (#216)
Browse files Browse the repository at this point in the history
This upgrades path-to-regexp to the latest version.

The behaviour has changed quite a lot since the previous version we were
using. Some examples of what has changed:

- Wildcards are now subpatterns, e.g. `/foo/(.*)` rather than `/foo/*`
- Delimeters are parsed everywhere (`http://foo` would think `://foo` or
something is a parameter)
- Characters need escaping, e.g. `foo?bar` must be `foo\\?bar` since you
can now have optional parameters like `/foo/:bar?`

To account for this in the most backwards compatible way possible, this
change does the following:

- Detects `://` in URLs and escapes it to `\\://`
- Detects `/*` in URLs and converts it to `/(.*)`
- **Breaks URLs with unescaped query strings** (i.e. `foo?bar` will no
longer behave the same. you _must_ escape, `foo\\?bar`

The breaking change with query strings is because we can't really
reliably replace `?` automatically with `\\?`, since consumers may
legitimately want to use the new optional parameters functionality.
  • Loading branch information
43081j committed Nov 26, 2023
1 parent e1a6f3d commit b800ff1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
20 changes: 15 additions & 5 deletions lib/fake-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var fakeXhr = require("../fake-xhr");
var push = [].push;
var log = require("./log");
var configureLogError = require("../configure-logger");
var pathToRegexp = require("path-to-regexp");
var pathToRegexp = require("path-to-regexp").pathToRegexp;

var supportsArrayBuffer = typeof ArrayBuffer !== "undefined";

Expand Down Expand Up @@ -236,10 +236,20 @@ var fakeServer = {
}

// Escape port number to prevent "named" parameters in 'path-to-regexp' module
if (typeof url === "string" && url !== "" && /:[0-9]+\//.test(url)) {
var m = url.match(/^(https?:\/\/.*?):([0-9]+\/.*)$/);
// eslint-disable-next-line no-param-reassign
url = `${m[1]}\\:${m[2]}`;
if (typeof url === "string" && url !== "") {
if (/:[0-9]+\//.test(url)) {
var m = url.match(/^(https?:\/\/.*?):([0-9]+\/.*)$/);
// eslint-disable-next-line no-param-reassign
url = `${m[1]}\\:${m[2]}`;
}
if (/:\/\//.test(url)) {
// eslint-disable-next-line no-param-reassign
url = url.replace("://", "\\://");
}
if (/\*/.test(url)) {
// eslint-disable-next-line no-param-reassign
url = url.replace(/\/\*/g, "/(.*)");
}
}

push.call(this.responses, {
Expand Down
2 changes: 1 addition & 1 deletion lib/fake-server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ describe("sinonFakeServer", function() {

it("yields response to request function handler when url contains RegExp characters", function() {
var handler = sinon.spy();
this.server.respondWith("GET", "/hello?world", handler);
this.server.respondWith("GET", "/hello\\?world", handler);
var xhr = new FakeXMLHttpRequest();
xhr.open("GET", "/hello?world");
xhr.send();
Expand Down
44 changes: 29 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@sinonjs/fake-timers": "^10.0.2",
"@sinonjs/text-encoding": "^0.7.1",
"just-extend": "^4.0.2",
"path-to-regexp": "^1.7.0"
"path-to-regexp": "^6.2.1"
},
"lint-staged": {
"*.{js,css,md}": "prettier --check",
Expand Down

0 comments on commit b800ff1

Please sign in to comment.