From 73d152506b6f5e598e3d1e527e5bb4897895fec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matias=20Niemel=C3=A4?= Date: Mon, 15 Jun 2015 00:45:02 -0700 Subject: [PATCH] fix(router): ensure that root URL redirect doesn't redirect non-root URLs Closes #2221 --- .../angular2/src/router/route_recognizer.ts | 8 ++++-- .../test/router/route_recognizer_spec.ts | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) 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');