1616
1717package org .springframework .cloud .gateway .handler .predicate ;
1818
19+ import java .net .URI ;
1920import java .util .ArrayList ;
21+ import java .util .LinkedHashSet ;
22+ import java .util .List ;
2023import java .util .function .Predicate ;
2124
2225import org .junit .jupiter .api .Test ;
26+ import org .mockito .ArgumentCaptor ;
27+ import reactor .core .publisher .Mono ;
2328
29+ import org .springframework .boot .autoconfigure .web .reactive .WebFluxProperties ;
30+ import org .springframework .cloud .gateway .filter .GatewayFilter ;
31+ import org .springframework .cloud .gateway .filter .GatewayFilterChain ;
32+ import org .springframework .cloud .gateway .filter .factory .StripPrefixGatewayFilterFactory ;
2433import org .springframework .cloud .gateway .handler .AsyncPredicate ;
2534import org .springframework .cloud .gateway .route .Route ;
35+ import org .springframework .mock .http .server .reactive .MockServerHttpRequest ;
36+ import org .springframework .mock .web .server .MockServerWebExchange ;
2637import org .springframework .web .server .ServerWebExchange ;
2738
2839import static org .assertj .core .api .Assertions .assertThat ;
40+ import static org .mockito .Mockito .mock ;
41+ import static org .mockito .Mockito .when ;
42+ import static org .springframework .cloud .gateway .support .ServerWebExchangeUtils .GATEWAY_ORIGINAL_REQUEST_URL_ATTR ;
43+ import static org .springframework .cloud .gateway .support .ServerWebExchangeUtils .GATEWAY_REQUEST_URL_ATTR ;
2944
3045/**
3146 * @author Spencer Gibb
47+ * @author FuYiNan Guo
3248 */
3349public class GatewayPredicateVisitorTests {
3450
3551 @ Test
3652 public void asyncPredicateVisitVisitsEachNode () {
37- PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory ();
53+ PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory (new WebFluxProperties () );
3854 HostRoutePredicateFactory hostRoutePredicateFactory = new HostRoutePredicateFactory ();
3955 ReadBodyRoutePredicateFactory readBodyRoutePredicateFactory1 = new ReadBodyRoutePredicateFactory ();
4056 ReadBodyRoutePredicateFactory readBodyRoutePredicateFactory2 = new ReadBodyRoutePredicateFactory ();
@@ -55,7 +71,7 @@ public void asyncPredicateVisitVisitsEachNode() {
5571
5672 @ Test
5773 public void predicateVisitVisitsEachNode () {
58- PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory ();
74+ PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory (new WebFluxProperties () );
5975 HostRoutePredicateFactory hostRoutePredicateFactory = new HostRoutePredicateFactory ();
6076 Predicate <ServerWebExchange > predicate = pathRoutePredicateFactory .apply (pathRoutePredicateFactory .newConfig ())
6177 .and (hostRoutePredicateFactory .apply (hostRoutePredicateFactory .newConfig ()));
@@ -68,4 +84,59 @@ public void predicateVisitVisitsEachNode() {
6884 .hasExactlyElementsOfTypes (PathRoutePredicateFactory .Config .class , HostRoutePredicateFactory .Config .class );
6985 }
7086
87+ @ Test
88+ public void pathRoutePredicateVisitWithSetWebfluxBasePath () {
89+ WebFluxProperties webFluxProperties = new WebFluxProperties ();
90+ webFluxProperties .setBasePath ("/gw/api/v1" );
91+
92+ PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory (webFluxProperties );
93+ PathRoutePredicateFactory .Config config = new PathRoutePredicateFactory .Config ()
94+ .setPatterns (List .of ("/temp/**" ))
95+ .setMatchTrailingSlash (true );
96+
97+ Predicate <ServerWebExchange > predicate = pathRoutePredicateFactory .apply (config );
98+
99+ ServerWebExchange exchange = MockServerWebExchange .from (MockServerHttpRequest .get ("http://127.0.0.1:8080/gw/api/v1/temp/test" )
100+ .build ());
101+
102+ assertThat (predicate .test (exchange )).isEqualTo (true );
103+ }
104+
105+ @ Test
106+ public void pathRoutePredicateVisitWithSetWebfluxBasePathStripPrefix () {
107+ WebFluxProperties webFluxProperties = new WebFluxProperties ();
108+ webFluxProperties .setBasePath ("/gw/api/v1" );
109+
110+ PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory (webFluxProperties );
111+ PathRoutePredicateFactory .Config config = new PathRoutePredicateFactory .Config ()
112+ .setPatterns (List .of ("/temp/**" ))
113+ .setMatchTrailingSlash (true );
114+
115+ Predicate <ServerWebExchange > predicate = pathRoutePredicateFactory .apply (config );
116+
117+ ServerWebExchange exchange = MockServerWebExchange .from (MockServerHttpRequest .get ("http://127.0.0.1:8080/gw/api/v1/temp/test" )
118+ .build ());
119+
120+ assertThat (predicate .test (exchange )).isEqualTo (true );
121+
122+ // webflux base path strips prefix is 3
123+ GatewayFilter filter = new StripPrefixGatewayFilterFactory ().apply (c -> c .setParts (3 ));
124+
125+ GatewayFilterChain filterChain = mock (GatewayFilterChain .class );
126+
127+ ArgumentCaptor <ServerWebExchange > captor = ArgumentCaptor .forClass (ServerWebExchange .class );
128+ when (filterChain .filter (captor .capture ())).thenReturn (Mono .empty ());
129+
130+ filter .filter (exchange , filterChain );
131+
132+ ServerWebExchange webExchange = captor .getValue ();
133+
134+ assertThat (webExchange .getRequest ().getURI ()).hasPath ("/temp/test" );
135+
136+ URI requestUrl = webExchange .getRequiredAttribute (GATEWAY_REQUEST_URL_ATTR );
137+ assertThat (requestUrl ).hasScheme ("http" ).hasHost ("127.0.0.1" ).hasPort (8080 ).hasPath ("/temp/test" );
138+
139+ LinkedHashSet <URI > uris = webExchange .getRequiredAttribute (GATEWAY_ORIGINAL_REQUEST_URL_ATTR );
140+ assertThat (uris ).contains (exchange .getRequest ().getURI ());
141+ }
71142}
0 commit comments