Search in sources :

Example 6 with GatewayFilter

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);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Ordered(org.springframework.core.Ordered) Route(org.springframework.cloud.gateway.route.Route) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) OrderedGatewayFilter(org.springframework.cloud.gateway.filter.OrderedGatewayFilter)

Example 7 with GatewayFilter

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));
}
Also used : Response(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response) DirtiesContext(org.springframework.test.annotation.DirtiesContext) TupleBuilder.tuple(org.springframework.tuple.TupleBuilder.tuple) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) ServerWebExchangeUtils(org.springframework.cloud.gateway.support.ServerWebExchangeUtils) Response(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response) RateLimiter(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) BaseWebClientTests(org.springframework.cloud.gateway.test.BaseWebClientTests) Autowired(org.springframework.beans.factory.annotation.Autowired) Qualifier(org.springframework.beans.factory.annotation.Qualifier) KeyResolver(org.springframework.cloud.gateway.filter.ratelimit.KeyResolver) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RANDOM_PORT(org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT) MockBean(org.springframework.boot.test.mock.mockito.MockBean) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Import(org.springframework.context.annotation.Import) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) ApplicationContext(org.springframework.context.ApplicationContext) HttpStatus(org.springframework.http.HttpStatus) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Tuple(org.springframework.tuple.Tuple) SpringBootConfiguration(org.springframework.boot.SpringBootConfiguration) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Bean(org.springframework.context.annotation.Bean) Route(org.springframework.cloud.gateway.route.Route) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Tuple(org.springframework.tuple.Tuple) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter)

Example 8 with GatewayFilter

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

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();
    };
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey) Arrays(java.util.Arrays) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) ServerWebExchangeUtils.containsEncodedParts(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts) Mono(reactor.core.publisher.Mono) DispatcherHandler(org.springframework.web.reactive.DispatcherHandler) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) Setter(com.netflix.hystrix.HystrixObservableCommand.Setter) TIMEOUT(com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.TIMEOUT) Function(java.util.function.Function) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Observable(rx.Observable) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) RxReactiveStreams(rx.RxReactiveStreams) ServerWebExchangeUtils.setResponseStatus(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus) GATEWAY_REQUEST_URL_ATTR(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR) HystrixObservableCommand(com.netflix.hystrix.HystrixObservableCommand) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) URI(java.net.URI) Subscription(rx.Subscription) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) Mono(reactor.core.publisher.Mono) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) Subscription(rx.Subscription) HystrixCommandGroupKey(com.netflix.hystrix.HystrixCommandGroupKey)

Aggregations

GatewayFilter (org.springframework.cloud.gateway.filter.GatewayFilter)9 GatewayFilterChain (org.springframework.cloud.gateway.filter.GatewayFilterChain)5 ServerWebExchange (org.springframework.web.server.ServerWebExchange)5 URI (java.net.URI)4 Route (org.springframework.cloud.gateway.route.Route)4 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)4 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)4 OrderedGatewayFilter (org.springframework.cloud.gateway.filter.OrderedGatewayFilter)3 HttpStatus (org.springframework.http.HttpStatus)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Tuple (org.springframework.tuple.Tuple)2 Mono (reactor.core.publisher.Mono)2 HystrixCommandGroupKey (com.netflix.hystrix.HystrixCommandGroupKey)1 HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)1 HystrixObservableCommand (com.netflix.hystrix.HystrixObservableCommand)1 Setter (com.netflix.hystrix.HystrixObservableCommand.Setter)1 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)1 TIMEOUT (com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.TIMEOUT)1