Skip to content

Commit

Permalink
Merge pull request #33 from irae/fix-matcher-with-json-values
Browse files Browse the repository at this point in the history
Fix encoding/decoding inconsistency when using values that need scaping
  • Loading branch information
mridgway committed Apr 4, 2016
2 parents de8796a + 152e4b9 commit c4e2e84
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# Change Log

## 2.0.0

### Breaking Changes

* [#33] `getRoute` will now `decodeURIComponent` route values, you might need to remove `decodeURIComponent` from your route actions if you were supporting extended characters manually in your routes.

## 1.0.0

### Breaking Changes

* [#29] `navigate` is no longer used as part of `router.getRoute` options
* [#29] `route.navigate` has been removed from the matched route object

### Features

* [#30] Route definitions should now be defined as an array of route objects
* [#30] Route definitions should now be defined as an array of route objects
rather than a map of routes. The old method of defining routes with a map
is still supported, but ordering can not be guaranteed (as per the JavaScript
engine's implementation).
* [#31] Added support for parsing and constructing urls with query strings.
* [#31] Added support for parsing and constructing urls with query strings.
Matched route objects now contain a `query` property containing the map of
query parameters. `router.makePath` now accepts a third `query` parameter
which is a map of query parameters to add to the resulting URL string. e.g.
Expand Down
4 changes: 3 additions & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ Route.prototype.match = function (url, options) {
// A route may legitimately have multiple instances of a parameter
// name if the path was an array.
if (pathMatches[i+1] !== undefined || routeParams[self.keys[i].name] === undefined){
routeParams[self.keys[i].name] = pathMatches[i+1];
// Because pathToRegexp encodeURIComponent params values, it is necessary
// to decode when reading from URL
routeParams[self.keys[i].name] = decodeURIComponent(pathMatches[i+1]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "routr",
"version": "1.0.0",
"version": "2.0.0",
"description": "A router for both server and client",
"main": "index.js",
"repository": {
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ var routesObject = {
},
invalid_path: {
path: 123
},
json_value: {
path: '/path/with/some/json_value/:json'
}
};
var routesArray = Object.keys(routesObject).map(function (routeName) {
return Object.assign({}, routesObject[routeName], {
name: routeName
});
});
var encodingConsistencyPath = '/path/with/some/json_value/%7B%22keyword%22%3A%22foo%22%7D';

describe('Router', function () {
[routesObject, routesArray].forEach(function (routes, key) {
Expand Down Expand Up @@ -284,6 +288,10 @@ describe('Router', function () {
var route = router.getRoute('/array/path/with/collision/bar/abc');
expect(route.params.key).to.equal('abc');
});
it('route with json string in param with consistency', function () {
var route = router.getRoute(encodingConsistencyPath);
expect(route.params.json).to.equal('{"keyword":"foo"}');
});

it('should allow route to match multiple methods', function () {
var route = 'multi_methods';
Expand Down Expand Up @@ -388,6 +396,12 @@ describe('Router', function () {
var path = router.makePath('invalid_path', {});
expect(path).to.equal(null);
});
it('path with some json value and consistency', function () {
var path = router.makePath('json_value', {
json: JSON.stringify({keyword: 'foo'})
});
expect(path).to.equal(encodingConsistencyPath);
});
});

it('should throw if route name is not defined', function () {
Expand Down

0 comments on commit c4e2e84

Please sign in to comment.