Skip to content

Commit

Permalink
Encode dynamic url segments when generating
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Apr 20, 2016
1 parent 7ffd1de commit 18012da
Show file tree
Hide file tree
Showing 2 changed files with 9 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 @@ -61,7 +61,7 @@ DynamicSegment.prototype = {
},

generate: function(params) {
return params[this.name];
return encodeURIComponent(params[this.name]);
}
};

Expand Down
8 changes: 8 additions & 0 deletions tests/recognizer-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ test("Generation works", function() {
equal( router.generate("postIndex"), "/posts" );
});

test("Generation encodes dynamic segments", function() {
var postId = "abc/def";
var encodedPostId = encodeURIComponent(postId);
ok(postId !== encodedPostId, "precondition - encoded segment does not equal orginal segment");
equal( router.generate("post", { id: postId }), "/posts/" + encodedPostId );
equal( router.generate("edit_post", { id: postId }), "/posts/" + encodedPostId + "/edit" );
});

test("Parsing and generation results into the same input string", function() {
var query = "filter%20data=date";
equal(router.generateQueryString(router.parseQueryString(query)), '?' + query);
Expand Down

0 comments on commit 18012da

Please sign in to comment.