use of org.springframework.cloud.gateway.filter.GatewayFilter in project spring-cloud-gateway by spring-cloud.
the class GatewayFilterSpecTests method testFilter.
private void testFilter(Class<? extends GatewayFilter> type, GatewayFilter gatewayFilter, int order) {
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
Route.Builder routeBuilder = Route.builder().id("123").uri("abc:123").predicate(exchange -> true);
RouteLocatorBuilder.Builder routes = new RouteLocatorBuilder(context).routes();
GatewayFilterSpec spec = new GatewayFilterSpec(routeBuilder, routes);
spec.filter(gatewayFilter);
Route route = routeBuilder.build();
assertThat(route.getFilters()).hasSize(1);
GatewayFilter filter = route.getFilters().get(0);
assertThat(filter).isInstanceOf(type);
Ordered ordered = (Ordered) filter;
assertThat(ordered.getOrder()).isEqualTo(order);
}
use of org.springframework.cloud.gateway.filter.GatewayFilter in project spring-cloud-gateway by spring-cloud.
the class RequestRateLimiterGatewayFilterFactoryTests method assertFilterFactory.
private void assertFilterFactory(KeyResolver keyResolver, String key, boolean allowed, HttpStatus expectedStatus) {
Tuple args = tuple().build();
when(rateLimiter.isAllowed("myroute", key)).thenReturn(Mono.just(new Response(allowed, 1)));
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
exchange.getResponse().setStatusCode(HttpStatus.OK);
exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR, Route.builder().id("myroute").predicate(ex -> true).uri("http://localhost").build());
when(this.filterChain.filter(exchange)).thenReturn(Mono.empty());
RequestRateLimiterGatewayFilterFactory factory = this.context.getBean(RequestRateLimiterGatewayFilterFactory.class);
GatewayFilter filter = factory.apply(config -> config.setKeyResolver(keyResolver));
Mono<Void> response = filter.filter(exchange, this.filterChain);
response.subscribe(aVoid -> assertThat(exchange.getResponse().getStatusCode()).isEqualTo(expectedStatus));
}
use of org.springframework.cloud.gateway.filter.GatewayFilter 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());
}
use of org.springframework.cloud.gateway.filter.GatewayFilter in project spring-cloud-gateway by spring-cloud.
the class HystrixGatewayFilterFactory method apply.
@Override
public GatewayFilter apply(Config config) {
// TODO: if no name is supplied, generate one from command id (useful for default filter)
if (config.setter == null) {
HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey(getClass().getSimpleName());
HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey(config.name);
config.setter = Setter.withGroupKey(groupKey).andCommandKey(commandKey);
}
return (exchange, chain) -> {
RouteHystrixCommand command = new RouteHystrixCommand(config.setter, config.fallbackUri, exchange, chain);
return Mono.create(s -> {
Subscription sub = command.toObservable().subscribe(s::success, s::error, s::success);
s.onCancel(sub::unsubscribe);
}).onErrorResume((Function<Throwable, Mono<Void>>) throwable -> {
if (throwable instanceof HystrixRuntimeException) {
HystrixRuntimeException e = (HystrixRuntimeException) throwable;
if (e.getFailureType() == TIMEOUT) {
setResponseStatus(exchange, HttpStatus.GATEWAY_TIMEOUT);
return exchange.getResponse().setComplete();
}
}
return Mono.empty();
}).then();
};
}
Aggregations