Skip to content

Commit

Permalink
Decode encoded path segments in urls
Browse files Browse the repository at this point in the history
This allows handlers like "/foo/:bar" to match a path with a url encoded
segment such as "/foo/abc%Fdef" -> { params: { bar: "abc/def" } }

See related issue emberjs/ember.js#11497
  • Loading branch information
bantic committed Apr 20, 2016
1 parent 20f90b5 commit 7ffd1de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/route-recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ function findHandler(state, path, queryParams) {
for (var i=0; i<handlers.length; i++) {
var handler = handlers[i], names = handler.names, params = {};

for (var j=0; j<names.length; j++) {
params[names[j]] = captures[currentCapture++];
for (var j=0, m=names.length; j<m; j++) {
params[names[j]] = decodeURIComponent(captures[currentCapture++]);
}

result[i] = { handler: handler.handler, params: params, isDynamic: !!names.length };
Expand Down
20 changes: 20 additions & 0 deletions tests/recognizer-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ test("A dynamic route recognizes", function() {
equal(router.recognize("/zoo/baz"), null);
});

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

var bar = "abc/def";
var encodedBar = encodeURIComponent(bar);
resultsMatch(router.recognize("/foo/" + encodedBar), [{ handler: handler, params: { bar: bar }, isDynamic: true }]);
});

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

var bar = "abc/def";
var encodedBar = encodeURIComponent(bar);
resultsMatch(router.recognize("/foo/" + encodedBar + "/baz"), [{ handler: handler, params: { bar: bar }, isDynamic: true }]);
});

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

0 comments on commit 7ffd1de

Please sign in to comment.