Skip to content

Commit

Permalink
fix dynamic segments with URL-encoding
Browse files Browse the repository at this point in the history
```js
decodeURI('/foo%20bar') // '/foo bar'
decodeURI('/foo%23bar') // '/foo%23bar'
decodeURI('/foo%3fbar') // '/foo%3fbar'

decodeURIComponent('/foo%20bar') // '/foo bar'
decodeURIComponent('/foo%23bar') // '/foo#bar'
decodeURIComponent('/foo%3fbar') // '/foo?bar'
```

This fixes two separate, but related problems:

 1. `router.add([{ path: '/foo bar?baz', }])` did not previously
    match `/foo%20%3fbaz`
 2. `router.add([{ path: '/foo/:bar', ... }])`, when given
    `/foo/a%20b%23c%3fd`, would return params `{ bar: 'a b%23c%3fd' }`
  • Loading branch information
James A. Rosen committed Nov 26, 2015
1 parent 2a5a26c commit 29eb1b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/route-recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ RouteRecognizer.prototype = {
queryParams = this.parseQueryString(queryString);
}

path = decodeURI(path);
path = decodeURIComponent(path);

// DEBUG GROUP path

Expand Down
18 changes: 18 additions & 0 deletions tests/recognizer-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ test("A simple route recognizes", function() {
equal(router.recognize("/foo/baz"), null);
});

test("A route with URL-encoded parts recognizes", function() {
var handler = {};
var router = new RouteRecognizer();
router.add([{ path: "/foo bar?baz", handler: handler }]);

resultsMatch(router.recognize("/foo%20bar%3fbaz"), [{ handler: handler, params: {}, isDynamic: false }]);
});

test("A unicode route recognizes", function() {
var handler = {};
var router = new RouteRecognizer();
Expand Down Expand Up @@ -129,6 +137,16 @@ test("A dynamic route recognizes", function() {
equal(router.recognize("/zoo/baz"), null);
});

test("A dynamic route with encoded params", function() {
var handler = {};
var router = new RouteRecognizer();
router.add([{ path: "/foo/:bar", handler: handler }]);

equal(router.recognize("/foo/bar%20baz")[0].params.bar, "bar baz");
equal(router.recognize("/foo/bar%23baz")[0].params.bar, "bar#baz");
equal(router.recognize("/foo/bar%3fbaz")[0].params.bar, "bar?baz");
});

test("Multiple routes recognize", function() {
var handler1 = { handler: 1 };
var handler2 = { handler: 2 };
Expand Down

0 comments on commit 29eb1b2

Please sign in to comment.