@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2013 - 2015 the original author or authors .
* Copyright 2013 - 2017 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -16,7 +16,11 @@
@@ -16,7 +16,11 @@
package org.springframework.cloud.netflix.zuul.web ;
import static org.assertj.core.api.Assertions.assertThat ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.List ;
import org.junit.Before ;
import org.junit.Test ;
@ -28,11 +32,9 @@ import org.springframework.mock.web.MockHttpServletRequest;
@@ -28,11 +32,9 @@ import org.springframework.mock.web.MockHttpServletRequest;
import com.netflix.zuul.context.RequestContext ;
import static org.junit.Assert.assertNotNull ;
import static org.junit.Assert.assertNull ;
/ * *
* @author Dave Syer
* @author Biju Kunjummen
* /
public class ZuulHandlerMappingTests {
@ -58,7 +60,7 @@ public class ZuulHandlerMappingTests {
@@ -58,7 +60,7 @@ public class ZuulHandlerMappingTests {
. singletonList ( new Route ( "foo" , "/foo/**" , "foo" , "" , null , null ) ) ) ;
this . request . setServletPath ( "/foo/" ) ;
this . mapping . setDirty ( true ) ;
assertNotNull ( this . mapping . getHandler ( this . request ) ) ;
assertThat ( this . mapping . getHandler ( this . request ) ) . isNotNull ( ) ;
}
@Test
@ -68,7 +70,7 @@ public class ZuulHandlerMappingTests {
@@ -68,7 +70,7 @@ public class ZuulHandlerMappingTests {
;
this . request . setServletPath ( "/" ) ;
this . mapping . setDirty ( true ) ;
assertNotNull ( this . mapping . getHandler ( this . request ) ) ;
assertThat ( this . mapping . getHandler ( this . request ) ) . isNotNull ( ) ;
}
@Test
@ -77,7 +79,41 @@ public class ZuulHandlerMappingTests {
@@ -77,7 +79,41 @@ public class ZuulHandlerMappingTests {
. singletonList ( new Route ( "default" , "/**" , "foo" , "" , null , null ) ) ) ;
this . request . setServletPath ( "/error" ) ;
this . mapping . setDirty ( true ) ;
assertNull ( this . mapping . getHandler ( this . request ) ) ;
assertThat ( this . mapping . getHandler ( this . request ) ) . isNull ( ) ;
}
@Test
public void ignoredPathsShouldNotReturnAHandler ( ) throws Exception {
assertThat ( mappingWithIgnoredPathsAndRoutes ( Arrays . asList ( "/p1/**" ) ,
new Route ( "p1" , "/p1/**" , "p1" , "" , null , null ) )
. getHandler ( requestForAPath ( "/p1" ) ) ) . isNull ( ) ;
assertThat ( mappingWithIgnoredPathsAndRoutes ( Arrays . asList ( "/p1/**/p3/" ) ,
new Route ( "p1" , "/p1/**/p3" , "p1" , "" , null , null ) )
. getHandler ( requestForAPath ( "/p1/p2/p3" ) ) ) . isNull ( ) ;
assertThat ( mappingWithIgnoredPathsAndRoutes ( Arrays . asList ( "/p1/**/p3/**" ) ,
new Route ( "p1" , "/p1/**/p3" , "p1" , "" , null , null ) )
. getHandler ( requestForAPath ( "/p1/p2/p3" ) ) ) . isNull ( ) ;
assertThat ( mappingWithIgnoredPathsAndRoutes ( Arrays . asList ( "/p1/**/p4/" ) ,
new Route ( "p1" , "/p1/**/p4/" , "p1" , "" , null , null ) )
. getHandler ( requestForAPath ( "/p1/p2/p3/p4" ) ) ) . isNull ( ) ;
}
private ZuulHandlerMapping mappingWithIgnoredPathsAndRoutes ( List < String > ignoredPaths , Route route ) {
RouteLocator routeLocator = Mockito . mock ( RouteLocator . class ) ;
Mockito . when ( routeLocator . getIgnoredPaths ( ) )
. thenReturn ( ignoredPaths ) ;
Mockito . when ( routeLocator . getRoutes ( ) ) . thenReturn ( Collections . singletonList ( route ) ) ;
ZuulHandlerMapping zuulHandlerMapping = new ZuulHandlerMapping ( routeLocator , new ZuulController ( ) ) ;
return zuulHandlerMapping ;
}
private MockHttpServletRequest requestForAPath ( String path ) {
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . setServletPath ( path ) ;
return request ;
}
}