Skip to content

Commit

Permalink
Merge pull request #124 from tightenco/encode-uri-component
Browse files Browse the repository at this point in the history
Add encodeURIComponent to path and query params.
  • Loading branch information
DanielCoulbourne authored Jan 26, 2018
2 parents dd35479 + 15e5d3e commit f7b0377
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/js/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Router extends String {
paramsArrayKey++;
if (typeof tags[key] !== 'undefined') {
delete this.queryParams[key];
return tags[key].id || tags[key];
return tags[key].id || encodeURIComponent(tags[key]);
}
if (tag.indexOf('?') === -1) {
throw new Error(`Ziggy Error: '${keyName}' key is required for route '${this.name}'`);
Expand Down Expand Up @@ -91,7 +91,7 @@ class Router extends String {

Object.keys(this.queryParams).forEach(function(key, i) {
queryString = i === 0 ? queryString : queryString + '&';
queryString += key + '=' + this.queryParams[key];
queryString += key + '=' + encodeURIComponent(this.queryParams[key]);
}.bind(this));

return queryString;
Expand Down
14 changes: 14 additions & 0 deletions tests/js/test.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,18 @@ describe('route()', function() {
route('events.venues.index', 1)
);
});

it('Should URL encode path params.', function() {
assert.equal(
'http://test.thing/ab/cd/events/Fun%26Games/venues',
route('events.venues.index', {event: "Fun&Games"}).url()
);
});

it('Should URL encode query params.', function() {
assert.equal(
'http://test.thing/ab/cd/events/Fun%26Games/venues?location=Brews%26Clues',
route('events.venues.index', {event: "Fun&Games", location: "Brews&Clues"}).url()
);
});
});

0 comments on commit f7b0377

Please sign in to comment.