fix(router): ensure that root URL redirect doesn't redirect non-root URLs

Closes #2221
This commit is contained in:
Matias Niemelä 2015-06-15 00:45:02 -07:00
parent 3154cea0bf
commit 73d152506b
2 changed files with 32 additions and 2 deletions

View File

@ -57,8 +57,12 @@ export class RouteRecognizer {
var solutions = ListWrapper.create(); var solutions = ListWrapper.create();
MapWrapper.forEach(this.redirects, (target, path) => { MapWrapper.forEach(this.redirects, (target, path) => {
// TODO: "/" redirect case // "/" redirect case
if (StringWrapper.startsWith(url, path)) { if (path == '/' || path == '') {
if (path == url) {
url = target;
}
} else if (StringWrapper.startsWith(url, path)) {
url = target + StringWrapper.substring(url, path.length); url = target + StringWrapper.substring(url, path.length);
} }
}); });

View File

@ -76,6 +76,32 @@ export function main() {
expect(solution.matchedUrl).toEqual('/b'); 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', () => { it('should generate URLs', () => {
recognizer.addConfig('/app/user/:name', handler, 'user'); recognizer.addConfig('/app/user/:name', handler, 'user');