Skip to content

Commit 73d1525

Browse files
committed
fix(router): ensure that root URL redirect doesn't redirect non-root URLs
Closes angular#2221
1 parent 3154cea commit 73d1525

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

modules/angular2/src/router/route_recognizer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ export class RouteRecognizer {
5757
var solutions = ListWrapper.create();
5858

5959
MapWrapper.forEach(this.redirects, (target, path) => {
60-
// TODO: "/" redirect case
61-
if (StringWrapper.startsWith(url, path)) {
60+
// "/" redirect case
61+
if (path == '/' || path == '') {
62+
if (path == url) {
63+
url = target;
64+
}
65+
} else if (StringWrapper.startsWith(url, path)) {
6266
url = target + StringWrapper.substring(url, path.length);
6367
}
6468
});

modules/angular2/test/router/route_recognizer_spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,32 @@ export function main() {
7676
expect(solution.matchedUrl).toEqual('/b');
7777
});
7878

79+
it('should not perform root URL redirect on a non-root route', () => {
80+
recognizer.addRedirect('/', '/foo');
81+
recognizer.addConfig('/bar', handler);
82+
var solutions = recognizer.recognize('/bar');
83+
expect(solutions.length).toBe(1);
84+
85+
var solution = solutions[0];
86+
expect(solution.handler).toEqual(handler);
87+
expect(solution.matchedUrl).toEqual('/bar');
88+
});
89+
90+
it('should perform a valid redirect when a slash or an empty string is being processed', () => {
91+
recognizer.addRedirect('/', '/matias');
92+
recognizer.addRedirect('', '/fatias');
93+
94+
recognizer.addConfig('/matias', handler);
95+
recognizer.addConfig('/fatias', handler);
96+
97+
var solutions;
98+
99+
solutions = recognizer.recognize('/');
100+
expect(solutions[0].matchedUrl).toBe('/matias');
101+
102+
solutions = recognizer.recognize('');
103+
expect(solutions[0].matchedUrl).toBe('/fatias');
104+
});
79105

80106
it('should generate URLs', () => {
81107
recognizer.addConfig('/app/user/:name', handler, 'user');

0 commit comments

Comments
 (0)