diff --git a/adev/src/app/routing/redirections.spec.ts b/adev/src/app/routing/redirections.spec.ts new file mode 100644 index 00000000000..e750e1b5afd --- /dev/null +++ b/adev/src/app/routing/redirections.spec.ts @@ -0,0 +1,31 @@ +/*! + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.dev/license + */ + +import {REDIRECT_ROUTES} from './redirections'; +import {Route} from '@angular/router'; + +describe('REDIRECT_ROUTES', () => { + it('should have all redirectTo values starting with a "/"', () => { + const checkRoutes = (routes: Route[]) => { + for (const route of routes) { + if (route.redirectTo) { + if (typeof route.redirectTo === 'string') { + expect(route.redirectTo.startsWith('/')) + .withContext(`Invalid redirectTo: ${route.redirectTo}`) + .toBe(true); + } + } + if (route.children) { + checkRoutes(route.children); + } + } + }; + + checkRoutes(REDIRECT_ROUTES); + }); +}); diff --git a/adev/src/app/routing/redirections.ts b/adev/src/app/routing/redirections.ts index 1bb7ee2192f..1eeba7d49d4 100644 --- a/adev/src/app/routing/redirections.ts +++ b/adev/src/app/routing/redirections.ts @@ -10,20 +10,22 @@ import {Route} from '@angular/router'; /** * This file contains the redirections we keep to prevent breakages on existing links * that may exist on the internet and over which we have no control. + * + * The redirectTo should always that start with a "/" to avoid relative redirections. */ export const REDIRECT_ROUTES: Route[] = [ { path: 'guide/di/dependency-injection', - redirectTo: 'guide/di', + redirectTo: '/guide/di', }, { path: 'guide/di/creating-injectable-service', - redirectTo: 'guide/di/creating-and-using-services', + redirectTo: '/guide/di/creating-and-using-services', }, { path: 'guide/di/dependency-injection-providers', - redirectTo: 'guide/di/defining-dependency-providers', + redirectTo: '/guide/di/defining-dependency-providers', }, { path: 'guide/defer',