Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure req.params is correctly populated for Routers with a non-default root path. #1207

Merged
merged 1 commit into from
Sep 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/js/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ Y.Router = Y.extend(Router, Y.Base, {

// Decode each of the path matches so that the any URL-encoded
// path segments are decoded in the `req.params` object.
matches = YArray.map(route.regex.exec(path) || [], function (match) {
matches = YArray.map(route.regex.exec(self.removeRoot(path)) || [], function (match) {
// Decode matches, or coerce `undefined` matches to an empty
// string to match expectations of working with `req.params`
// in the content of route dispatching, and normalize
Expand Down
51 changes: 51 additions & 0 deletions src/app/tests/unit/assets/router-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,57 @@ routerSuite.add(new Y.Test.Case({
Assert.areSame(5, calls);
},

'request object should contain captured route parameters for Router with non-default root': function () {
var calls = 0,
router = this.router = new Y.Router({
root: '/root/'
});

router.route('/foo/:bar/:baz', function (req) {
calls += 1;

ArrayAssert.itemsAreSame(['bar', 'baz'], Y.Object.keys(req.params));
ArrayAssert.itemsAreSame(['one', 'two'], Y.Object.values(req.params));
});

router.route('/bar/*path', function (req) {
calls += 1;

Assert.isObject(req.params);
ArrayAssert.itemsAreSame(['path'], Y.Object.keys(req.params));
ArrayAssert.itemsAreSame(['one/two'], Y.Object.values(req.params));
});

router.route(/^\/(baz)\/(quux)$/, function (req) {
calls += 1;

Assert.isArray(req.params);
ArrayAssert.itemsAreSame(['/baz/quux', 'baz', 'quux'], req.params);
});

router.route(/^\/((fnord)|(fnarf))\/(quux)$/, function (req) {
calls += 1;

Assert.isArray(req.params);
ArrayAssert.itemsAreSame(['/fnord/quux', 'fnord', 'fnord', '', 'quux'], req.params);
});

router.route(/^\/((blorp)|(blerf))\/(quux)$/, function (req) {
calls += 1;

Assert.isArray(req.params);
ArrayAssert.itemsAreSame(['/blerf/quux', 'blerf', '', 'blerf', 'quux'], req.params);
});

router._dispatch('/root/foo/one/two', {});
router._dispatch('/root/bar/one/two', {});
router._dispatch('/root/baz/quux', {});
router._dispatch('/root/fnord/quux', {});
router._dispatch('/root/blerf/quux', {});

Assert.areSame(5, calls);
},

'route parameters should be decoded': function () {
var calls = 0,
router = this.router = new Y.Router(),
Expand Down