Java Code Examples for org.springframework.cloud.gateway.support.ServerWebExchangeUtils#putUriTemplateVariables()

The following examples show how to use org.springframework.cloud.gateway.support.ServerWebExchangeUtils#putUriTemplateVariables() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HostRoutePredicateFactory.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Override
public Predicate<ServerWebExchange> apply(Config config) {
	return new GatewayPredicate() {
		@Override
		public boolean test(ServerWebExchange exchange) {
			String host = exchange.getRequest().getHeaders().getFirst("Host");
			Optional<String> optionalPattern = config.getPatterns().stream()
					.filter(pattern -> pathMatcher.match(pattern, host)).findFirst();

			if (optionalPattern.isPresent()) {
				Map<String, String> variables = pathMatcher
						.extractUriTemplateVariables(optionalPattern.get(), host);
				ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);
				return true;
			}

			return false;
		}

		@Override
		public String toString() {
			return String.format("Hosts: %s", config.getPatterns());
		}
	};
}
 
Example 2
Source File: SetPathGatewayFilterFactoryTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
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);
	ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);

	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());
}