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
Enforce a router's root
as its mount point
#1198
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
03f5d53
Enforce a router's `root` as its mount point
ericf ad48d08
Update user guide and add HISTORY entry for Router mount point changes
ericf 106c45f
Update hash-based routing test to check for the full path in `req.path`
ericf c781d92
Fix syntax highlighting in App's HISTORY.md
ericf ebe52cb
Add note about `req.path` being the full path in App's HISTORY.md
ericf 5a9933e
Flip arguments of Router#_pathHasRoot() method and fix unit test
ericf File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -387,6 +387,32 @@ routerSuite.add(new Y.Test.Case({ | |
Assert.areSame(two, routes[1].callbacks[0]); | ||
}, | ||
|
||
'match() should return an array of routes that match the given path under a `root`': function () { | ||
var router = this.router = new Y.Router(), | ||
routes; | ||
|
||
function one() {} | ||
function two() {} | ||
function three() {} | ||
|
||
router.set('root', '/app/'); | ||
|
||
router.route('/:foo', one); | ||
router.route(/foo/, two); | ||
router.route('/bar', three); | ||
|
||
Assert.areSame(0, router.match('/').length); | ||
Assert.areSame(0, router.match('/foo').length); | ||
Assert.areSame(0, router.match('/foo/').length); | ||
Assert.areSame(0, router.match('/bar').length); | ||
|
||
routes = router.match('/app/foo'); | ||
|
||
Assert.areSame(2, routes.length); | ||
Assert.areSame(one, routes[0].callbacks[0]); | ||
Assert.areSame(two, routes[1].callbacks[0]); | ||
}, | ||
|
||
'hasRoute() should return `true` if one or more routes match the given path': function () { | ||
var router = this.router = new Y.Router(), | ||
router2 = new Y.Router(); | ||
|
@@ -417,6 +443,53 @@ routerSuite.add(new Y.Test.Case({ | |
router2.destroy(); | ||
}, | ||
|
||
'hasRoute() should always return `false` for paths not under the `root`': function () { | ||
var location = Y.getLocation(), | ||
router1 = new Y.Router({root: '/app'}), | ||
router2 = new Y.Router({root: '/app/'}); | ||
|
||
function noop () {} | ||
|
||
router1.route('/', noop); | ||
router1.route('/:foo', noop); | ||
router1.route(/foo/, noop); | ||
router1.route('/bar', noop); | ||
|
||
router2.route('/', noop); | ||
router2.route('/:foo', noop); | ||
router2.route(/foo/, noop); | ||
router2.route('/bar', noop); | ||
|
||
Assert.isFalse(router1.hasRoute('/')); | ||
Assert.isFalse(router1.hasRoute('/app')); | ||
Assert.isFalse(router1.hasRoute('/foo/')); | ||
Assert.isFalse(router1.hasRoute('/bar')); | ||
Assert.isFalse(router1.hasRoute('/baz')); | ||
Assert.isFalse(router1.hasRoute(router1._getOrigin() + '/bar')); | ||
|
||
Assert.isFalse(router2.hasRoute('/')); | ||
Assert.isFalse(router2.hasRoute('/app')); | ||
Assert.isFalse(router2.hasRoute('/foo/')); | ||
Assert.isFalse(router2.hasRoute('/bar')); | ||
Assert.isFalse(router2.hasRoute('/baz')); | ||
Assert.isFalse(router2.hasRoute(router2._getOrigin() + '/bar')); | ||
|
||
Assert.isTrue(router1.hasRoute('/app/')); | ||
Assert.isTrue(router1.hasRoute('/app/foo/')); | ||
Assert.isTrue(router1.hasRoute('/app/bar')); | ||
Assert.isTrue(router1.hasRoute('/app/baz')); | ||
Assert.isTrue(router1.hasRoute(router1._getOrigin() + '/app/bar')); | ||
|
||
Assert.isTrue(router2.hasRoute('/app/')); | ||
Assert.isTrue(router2.hasRoute('/app/foo/')); | ||
Assert.isTrue(router2.hasRoute('/app/bar')); | ||
Assert.isTrue(router2.hasRoute('/app/baz')); | ||
Assert.isTrue(router2.hasRoute(router2._getOrigin() + '/app/bar')); | ||
|
||
router1.destroy(); | ||
router2.destroy(); | ||
}, | ||
|
||
'hasRoute() should support full URLs': function () { | ||
var router = this.router = new Y.Router(), | ||
loc = win && win.location, | ||
|
@@ -482,7 +555,7 @@ routerSuite.add(new Y.Test.Case({ | |
|
||
router.route('/hashpath', function (req) { | ||
test.resume(function () { | ||
Assert.areSame('/hashpath', req.path); | ||
Assert.areSame('/foo/hashpath', req.path); | ||
Assert.areSame(Y.getLocation().pathname, root + 'hashpath'); | ||
}); | ||
}); | ||
|
@@ -497,15 +570,28 @@ routerSuite.add(new Y.Test.Case({ | |
router.set('root', '/'); | ||
Assert.areSame('/bar', router.removeRoot('/bar')); | ||
Assert.areSame('/bar', router.removeRoot('bar')); | ||
Assert.areSame('/bar/', router.removeRoot('/bar/')); | ||
Assert.areSame('/bar/', router.removeRoot('bar/')); | ||
|
||
router.set('root', '/foo'); | ||
Assert.areSame('/', router.removeRoot('/foo')); | ||
Assert.areSame('/', router.removeRoot('/foo/')); | ||
Assert.areSame('/bar', router.removeRoot('/foo/bar')); | ||
Assert.areSame('/bar/', router.removeRoot('/foo/bar/')); | ||
Assert.areSame('/foobar', router.removeRoot('/foobar')); | ||
Assert.areSame('/foobar/', router.removeRoot('/foobar/')); | ||
|
||
router.set('root', '/foo/'); | ||
Assert.areSame('/foo', router.removeRoot('/foo')); | ||
Assert.areSame('/', router.removeRoot('/foo/')); | ||
Assert.areSame('/bar', router.removeRoot('/foo/bar')); | ||
Assert.areSame('/bar/', router.removeRoot('/foo/bar/')); | ||
Assert.areSame('/foobar', router.removeRoot('/foobar')); | ||
Assert.areSame('/foobar/', router.removeRoot('/foobar/')); | ||
|
||
router.set('root', '/moo'); | ||
Assert.areSame('/foo/bar', router.removeRoot('/foo/bar')); | ||
Assert.areSame('/foo/moo', router.removeRoot('/foo/moo')); | ||
}, | ||
|
||
'removeRoot() should strip the origin ("http://foo.com") portion of the URL, if any': function () { | ||
|
@@ -653,7 +739,7 @@ routerSuite.add(new Y.Test.Case({ | |
router.set('root', pathRoot); | ||
router.route('/save', function (req) { | ||
test.resume(function () { | ||
Assert.areSame('/save', req.path); | ||
Assert.areSame(router._joinURL('/save'), req.path); | ||
Assert.areSame('/save', Y.HistoryHash.getHash()); | ||
}); | ||
}); | ||
|
@@ -674,7 +760,7 @@ routerSuite.add(new Y.Test.Case({ | |
router.set('root', '/app'); | ||
router.route('/save', function (req) { | ||
test.resume(function () { | ||
Assert.areSame('/save', req.path); | ||
Assert.areSame('/app/save', req.path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed test's expected value because |
||
Assert.areSame('/app/save', Y.HistoryHash.getHash()); | ||
}); | ||
}); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed test's expected value because
req.path
now contains the full URL, and is no longer relative to the router'sroot
.