fix(router): ensure that root URL redirect doesn't redirect non-root URLs
Closes #2221
This commit is contained in:
parent
3154cea0bf
commit
73d152506b
|
@ -57,8 +57,12 @@ export class RouteRecognizer {
|
|||
var solutions = ListWrapper.create();
|
||||
|
||||
MapWrapper.forEach(this.redirects, (target, path) => {
|
||||
// TODO: "/" redirect case
|
||||
if (StringWrapper.startsWith(url, path)) {
|
||||
// "/" redirect case
|
||||
if (path == '/' || path == '') {
|
||||
if (path == url) {
|
||||
url = target;
|
||||
}
|
||||
} else if (StringWrapper.startsWith(url, path)) {
|
||||
url = target + StringWrapper.substring(url, path.length);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -76,6 +76,32 @@ export function main() {
|
|||
expect(solution.matchedUrl).toEqual('/b');
|
||||
});
|
||||
|
||||
it('should not perform root URL redirect on a non-root route', () => {
|
||||
recognizer.addRedirect('/', '/foo');
|
||||
recognizer.addConfig('/bar', handler);
|
||||
var solutions = recognizer.recognize('/bar');
|
||||
expect(solutions.length).toBe(1);
|
||||
|
||||
var solution = solutions[0];
|
||||
expect(solution.handler).toEqual(handler);
|
||||
expect(solution.matchedUrl).toEqual('/bar');
|
||||
});
|
||||
|
||||
it('should perform a valid redirect when a slash or an empty string is being processed', () => {
|
||||
recognizer.addRedirect('/', '/matias');
|
||||
recognizer.addRedirect('', '/fatias');
|
||||
|
||||
recognizer.addConfig('/matias', handler);
|
||||
recognizer.addConfig('/fatias', handler);
|
||||
|
||||
var solutions;
|
||||
|
||||
solutions = recognizer.recognize('/');
|
||||
expect(solutions[0].matchedUrl).toBe('/matias');
|
||||
|
||||
solutions = recognizer.recognize('');
|
||||
expect(solutions[0].matchedUrl).toBe('/fatias');
|
||||
});
|
||||
|
||||
it('should generate URLs', () => {
|
||||
recognizer.addConfig('/app/user/:name', handler, 'user');
|
||||
|
|
Loading…
Reference in New Issue