diff --git a/modules/angular2/src/router/route_recognizer.ts b/modules/angular2/src/router/route_recognizer.ts index 972b705eea..fc0d1bd79e 100644 --- a/modules/angular2/src/router/route_recognizer.ts +++ b/modules/angular2/src/router/route_recognizer.ts @@ -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); } }); diff --git a/modules/angular2/test/router/route_recognizer_spec.ts b/modules/angular2/test/router/route_recognizer_spec.ts index 933f47fd41..c27a287211 100644 --- a/modules/angular2/test/router/route_recognizer_spec.ts +++ b/modules/angular2/test/router/route_recognizer_spec.ts @@ -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');