Search in sources :

Example 1 with GatewayFilterChain

use of org.springframework.cloud.gateway.filter.GatewayFilterChain in project spring-cloud-gateway by spring-cloud.

the class RewritePathGatewayFilterFactoryTests method testRewriteFilter.

private ServerWebExchange testRewriteFilter(String regex, String replacement, String actualPath, String expectedPath) {
    GatewayFilter filter = new RewritePathGatewayFilterFactory().apply(c -> c.setRegexp(regex).setReplacement(replacement));
    URI url = UriComponentsBuilder.fromUriString("http://localhost" + actualPath).build(true).toUri();
    MockServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, url).build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
    ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
    when(filterChain.filter(captor.capture())).thenReturn(Mono.empty());
    filter.filter(exchange, filterChain);
    ServerWebExchange webExchange = captor.getValue();
    assertThat(webExchange.getRequest().getURI()).hasPath(expectedPath);
    URI requestUrl = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(requestUrl).hasScheme("http").hasHost("localhost").hasNoPort().hasPath(expectedPath);
    LinkedHashSet<URI> uris = webExchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
    assertThat(uris).contains(request.getURI());
    return webExchange;
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) URI(java.net.URI) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter)

Example 2 with GatewayFilterChain

use of org.springframework.cloud.gateway.filter.GatewayFilterChain in project spring-cloud-gateway by spring-cloud.

the class SetPathGatewayFilterFactoryTests method testFilter.

private void testFilter(String template, String expectedPath, HashMap<String, String> variables) {
    GatewayFilter filter = new SetPathGatewayFilterFactory().apply(c -> c.setTemplate(template));
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost").build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    try {
        Constructor<PathMatchInfo> constructor = ReflectionUtils.accessibleConstructor(PathMatchInfo.class, Map.class, Map.class);
        constructor.setAccessible(true);
        PathMatchInfo pathMatchInfo = constructor.newInstance(variables, Collections.emptyMap());
        exchange.getAttributes().put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, pathMatchInfo);
    } catch (Exception e) {
        ReflectionUtils.rethrowRuntimeException(e);
    }
    GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
    ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
    when(filterChain.filter(captor.capture())).thenReturn(Mono.empty());
    filter.filter(exchange, filterChain);
    ServerWebExchange webExchange = captor.getValue();
    assertThat(webExchange.getRequest().getURI()).hasPath(expectedPath);
    LinkedHashSet<URI> uris = webExchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
    assertThat(uris).contains(request.getURI());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) PathMatchInfo(org.springframework.web.util.pattern.PathPattern.PathMatchInfo) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) URI(java.net.URI) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter)

Example 3 with GatewayFilterChain

use of org.springframework.cloud.gateway.filter.GatewayFilterChain in project spring-cloud-gateway by spring-cloud.

the class StripPrefixGatewayFilterFactoryTests method testStripPrefixFilter.

private void testStripPrefixFilter(String actualPath, String expectedPath, int parts) {
    GatewayFilter filter = new StripPrefixGatewayFilterFactory().apply(c -> c.setParts(parts));
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost" + actualPath).build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
    ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
    when(filterChain.filter(captor.capture())).thenReturn(Mono.empty());
    filter.filter(exchange, filterChain);
    ServerWebExchange webExchange = captor.getValue();
    assertThat(webExchange.getRequest().getURI()).hasPath(expectedPath);
    URI requestUrl = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(requestUrl).hasScheme("http").hasHost("localhost").hasNoPort().hasPath(expectedPath);
    LinkedHashSet<URI> uris = webExchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
    assertThat(uris).contains(request.getURI());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) URI(java.net.URI) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter)

Aggregations

URI (java.net.URI)3 GatewayFilter (org.springframework.cloud.gateway.filter.GatewayFilter)3 GatewayFilterChain (org.springframework.cloud.gateway.filter.GatewayFilterChain)3 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)3 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)3 ServerWebExchange (org.springframework.web.server.ServerWebExchange)3 PathMatchInfo (org.springframework.web.util.pattern.PathPattern.PathMatchInfo)1