use of org.springframework.web.util.pattern.PathPattern.PathMatchInfo 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());
}
Aggregations