@@ -12,7 +12,7 @@ import {ChangeDetectionStrategy, Component, Injectable, NgModule, NgModuleFactor
12
12
import { ComponentFixture , TestBed , fakeAsync , inject , tick } from '@angular/core/testing' ;
13
13
import { By } from '@angular/platform-browser/src/dom/debug/by' ;
14
14
import { expect } from '@angular/platform-browser/testing/src/matchers' ;
15
- import { ActivatedRoute , ActivatedRouteSnapshot , ActivationEnd , ActivationStart , CanActivate , CanDeactivate , ChildActivationEnd , ChildActivationStart , DetachedRouteHandle , Event , GuardsCheckEnd , GuardsCheckStart , NavigationCancel , NavigationEnd , NavigationError , NavigationStart , PRIMARY_OUTLET , ParamMap , Params , PreloadAllModules , PreloadingStrategy , Resolve , ResolveEnd , ResolveStart , RouteConfigLoadEnd , RouteConfigLoadStart , RouteReuseStrategy , Router , RouterEvent , RouterModule , RouterPreloader , RouterStateSnapshot , RoutesRecognized , RunGuardsAndResolvers , UrlHandlingStrategy , UrlSegmentGroup , UrlTree } from '@angular/router' ;
15
+ import { ActivatedRoute , ActivatedRouteSnapshot , ActivationEnd , ActivationStart , CanActivate , CanDeactivate , ChildActivationEnd , ChildActivationStart , DetachedRouteHandle , Event , GuardsCheckEnd , GuardsCheckStart , NavigationCancel , NavigationEnd , NavigationError , NavigationStart , PRIMARY_OUTLET , ParamMap , Params , PreloadAllModules , PreloadingStrategy , Resolve , ResolveEnd , ResolveStart , RouteConfigLoadEnd , RouteConfigLoadStart , RouteReuseStrategy , Router , RouterEvent , RouterModule , RouterPreloader , RouterStateSnapshot , RoutesRecognized , RunGuardsAndResolvers , UrlHandlingStrategy , UrlSegmentGroup , UrlSerializer , UrlTree } from '@angular/router' ;
16
16
import { Observable , Observer , of } from 'rxjs' ;
17
17
import { map } from 'rxjs/operators' ;
18
18
@@ -1010,6 +1010,30 @@ describe('Integration', () => {
1010
1010
expectEvents ( recordedEvents , [ [ NavigationStart , '/invalid' ] , [ NavigationError , '/invalid' ] ] ) ;
1011
1011
} ) ) ) ;
1012
1012
1013
+ it ( 'should recover from malformed uri errors' ,
1014
+ fakeAsync ( inject ( [ Router , Location ] , ( router : Router , location : Location ) => {
1015
+ router . resetConfig ( [ { path : 'simple' , component : SimpleCmp } ] ) ;
1016
+ const fixture = createRoot ( router , RootCmp ) ;
1017
+ router . navigateByUrl ( '/invalid/url%with%percent' ) ;
1018
+ advance ( fixture ) ;
1019
+ expect ( location . path ( ) ) . toEqual ( '/' ) ;
1020
+ } ) ) ) ;
1021
+
1022
+ it ( 'should support custom malformed uri error handler' ,
1023
+ fakeAsync ( inject ( [ Router , Location ] , ( router : Router , location : Location ) => {
1024
+ const customMalformedUriErrorHandler =
1025
+ ( e : URIError , urlSerializer : UrlSerializer , url : string ) :
1026
+ UrlTree => { return urlSerializer . parse ( '/?error=The-URL-you-went-to-is-invalid' ) ; } ;
1027
+ router . malformedUriErrorHandler = customMalformedUriErrorHandler ;
1028
+
1029
+ router . resetConfig ( [ { path : 'simple' , component : SimpleCmp } ] ) ;
1030
+
1031
+ const fixture = createRoot ( router , RootCmp ) ;
1032
+ router . navigateByUrl ( '/invalid/url%with%percent' ) ;
1033
+ advance ( fixture ) ;
1034
+ expect ( location . path ( ) ) . toEqual ( '/?error=The-URL-you-went-to-is-invalid' ) ;
1035
+ } ) ) ) ;
1036
+
1013
1037
it ( 'should not swallow errors' , fakeAsync ( inject ( [ Router ] , ( router : Router ) => {
1014
1038
const fixture = createRoot ( router , RootCmp ) ;
1015
1039
@@ -3938,6 +3962,22 @@ describe('Testing router options', () => {
3938
3962
expect ( router . paramsInheritanceStrategy ) . toEqual ( 'always' ) ;
3939
3963
} ) ) ) ;
3940
3964
} ) ;
3965
+
3966
+ describe ( 'malformedUriErrorHandler' , ( ) => {
3967
+
3968
+ function malformedUriErrorHandler ( e : URIError , urlSerializer : UrlSerializer , url : string ) {
3969
+ return urlSerializer . parse ( '/error' ) ;
3970
+ }
3971
+
3972
+ beforeEach ( ( ) => {
3973
+ TestBed . configureTestingModule (
3974
+ { imports : [ RouterTestingModule . withRoutes ( [ ] , { malformedUriErrorHandler} ) ] } ) ;
3975
+ } ) ;
3976
+
3977
+ it ( 'should configure the router' , fakeAsync ( inject ( [ Router ] , ( router : Router ) => {
3978
+ expect ( router . malformedUriErrorHandler ) . toBe ( malformedUriErrorHandler ) ;
3979
+ } ) ) ) ;
3980
+ } ) ;
3941
3981
} ) ;
3942
3982
3943
3983
function expectEvents ( events : Event [ ] , pairs : any [ ] ) {
0 commit comments