Search in sources :

Example 1 with HttpInterceptor

use of com.hotels.styx.api.HttpInterceptor in project styx by ExpediaGroup.

the class BuiltInInterceptors method internalStyxInterceptors.

static List<HttpInterceptor> internalStyxInterceptors(StyxConfig config, HttpMessageFormatter httpMessageFormatter) {
    List<HttpInterceptor> builder = new ArrayList<>();
    boolean loggingEnabled = config.get("request-logging.inbound.enabled", Boolean.class).orElse(false);
    boolean longFormatEnabled = config.get("request-logging.inbound.longFormat", Boolean.class).orElse(false);
    if (loggingEnabled) {
        builder.add(new HttpMessageLoggingInterceptor(longFormatEnabled, httpMessageFormatter));
    }
    builder.addAll(asList(new TcpTunnelRequestRejector(), new ConfigurationContextResolverInterceptor(EMPTY_CONFIGURATION_CONTEXT_RESOLVER), new UnexpectedRequestContentLengthRemover(), config.proxyServerConfig().via().map(ViaHeaderAppendingInterceptor::new).orElseGet(ViaHeaderAppendingInterceptor::new), new HopByHopHeadersRemovingInterceptor(), new RequestEnrichingInterceptor(config.styxHeaderConfig())));
    return List.copyOf(builder);
}
Also used : ConfigurationContextResolverInterceptor(com.hotels.styx.proxy.interceptors.ConfigurationContextResolverInterceptor) TcpTunnelRequestRejector(com.hotels.styx.proxy.interceptors.TcpTunnelRequestRejector) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) ArrayList(java.util.ArrayList) UnexpectedRequestContentLengthRemover(com.hotels.styx.proxy.interceptors.UnexpectedRequestContentLengthRemover) HopByHopHeadersRemovingInterceptor(com.hotels.styx.proxy.interceptors.HopByHopHeadersRemovingInterceptor) ViaHeaderAppendingInterceptor(com.hotels.styx.proxy.interceptors.ViaHeaderAppendingInterceptor) HttpMessageLoggingInterceptor(com.hotels.styx.proxy.interceptors.HttpMessageLoggingInterceptor) RequestEnrichingInterceptor(com.hotels.styx.proxy.interceptors.RequestEnrichingInterceptor)

Example 2 with HttpInterceptor

use of com.hotels.styx.api.HttpInterceptor in project styx by ExpediaGroup.

the class StandardHttpPipelineTest method sendsExceptionUponExtraSubscriptionInsideInterceptor.

@ParameterizedTest
@MethodSource("multipleSubscriptionInterceptors")
public void sendsExceptionUponExtraSubscriptionInsideInterceptor(HttpInterceptor interceptor) throws Exception {
    HttpHandler handler = (request, context) -> Eventual.of(response(OK).build());
    List<HttpInterceptor> interceptors = singletonList(interceptor);
    StandardHttpPipeline pipeline = new StandardHttpPipeline(interceptors, handler, RequestTracker.NO_OP);
    Eventual<LiveHttpResponse> responseObservable = pipeline.handle(get("/").build(), requestContext());
    assertThrows(IllegalStateException.class, () -> Mono.from(responseObservable).block());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpHandler(com.hotels.styx.api.HttpHandler) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with HttpInterceptor

use of com.hotels.styx.api.HttpInterceptor in project styx by ExpediaGroup.

the class StyxProxyTest method startsServerWithHttpConnector.

@Test
public void startsServerWithHttpConnector() {
    HttpInterceptor echoInterceptor = (request, chain) -> textResponse("Response from http connector");
    StandardHttpRouter handler = new StandardHttpRouter();
    InetServer styxServer = newBuilder().setProtocolConnector(connector(0)).bossExecutor(NettyExecutor.create("Test-Server-Boss", 1)).workerExecutor(NettyExecutor.create("Test-Server-Worker", 0)).handler(new HttpInterceptorPipeline(List.of(echoInterceptor), (request, context) -> new HttpAggregator(new StandardHttpRouter()).handle(request, context), false)).build();
    Service server = StyxServers.toGuavaService(styxServer);
    server.startAsync().awaitRunning();
    assertThat("Server should be running", server.isRunning());
    HttpResponse secureResponse = get("http://localhost:" + styxServer.inetAddress().getPort());
    assertThat(secureResponse.bodyAs(UTF_8), containsString("Response from http connector"));
    server.stopAsync().awaitTerminated();
    assertThat("Server should not be running", !server.isRunning());
}
Also used : HttpResponse(com.hotels.styx.api.HttpResponse) LoggerFactory(org.slf4j.LoggerFactory) StyxFutures.await(com.hotels.styx.common.StyxFutures.await) StyxHttpClient(com.hotels.styx.client.StyxHttpClient) HttpClient(com.hotels.styx.client.HttpClient) Disabled(org.junit.jupiter.api.Disabled) HttpConnectorConfig(com.hotels.styx.server.HttpConnectorConfig) StyxServers(com.hotels.styx.StyxServers) WebServerConnectorFactory(com.hotels.styx.server.netty.WebServerConnectorFactory) NettyServerBuilder(com.hotels.styx.server.netty.NettyServerBuilder) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) StandardHttpRouter(com.hotels.styx.server.StandardHttpRouter) Eventual(com.hotels.styx.api.Eventual) Logger(org.slf4j.Logger) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) UTF_8(java.nio.charset.StandardCharsets.UTF_8) HttpAggregator(com.hotels.styx.common.http.handler.HttpAggregator) NettyServerBuilder.newBuilder(com.hotels.styx.server.netty.NettyServerBuilder.newBuilder) NettyExecutor(com.hotels.styx.NettyExecutor) IOException(java.io.IOException) HttpRequest(com.hotels.styx.api.HttpRequest) HttpInterceptorPipeline(com.hotels.styx.routing.handlers.HttpInterceptorPipeline) Service(com.google.common.util.concurrent.Service) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) List(java.util.List) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) OK(com.hotels.styx.api.HttpResponseStatus.OK) InetServer(com.hotels.styx.InetServer) ServerConnector(com.hotels.styx.server.netty.ServerConnector) StandardHttpRouter(com.hotels.styx.server.StandardHttpRouter) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) HttpInterceptorPipeline(com.hotels.styx.routing.handlers.HttpInterceptorPipeline) Service(com.google.common.util.concurrent.Service) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) HttpAggregator(com.hotels.styx.common.http.handler.HttpAggregator) InetServer(com.hotels.styx.InetServer) Test(org.junit.jupiter.api.Test)

Example 4 with HttpInterceptor

use of com.hotels.styx.api.HttpInterceptor in project styx by ExpediaGroup.

the class StandardHttpPipelineTest method interceptorsCanPassInformationThroughContextBeforeRequest.

@Test
public void interceptorsCanPassInformationThroughContextBeforeRequest() {
    HttpInterceptor addsToContext = (request, chain) -> {
        chain.context().add("contextValue", "expected");
        return chain.proceed(request);
    };
    AtomicReference<String> foundInContext = new AtomicReference<>();
    HttpInterceptor takesFromContext = (request, chain) -> {
        foundInContext.set(chain.context().get("contextValue", String.class));
        return chain.proceed(request);
    };
    StandardHttpPipeline pipeline = pipeline(addsToContext, takesFromContext);
    LiveHttpResponse response = sendRequestTo(pipeline);
    assertThat(response.status(), is(OK));
    assertThat(foundInContext.get(), is("expected"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with HttpInterceptor

use of com.hotels.styx.api.HttpInterceptor in project styx by ExpediaGroup.

the class StandardHttpPipelineTest method contextValuesAddedBeforeRequestCanBeRetrievedAfterward.

@Test
public void contextValuesAddedBeforeRequestCanBeRetrievedAfterward() {
    HttpInterceptor addsToContext = (request, chain) -> {
        chain.context().add("contextValue", "expected");
        return chain.proceed(request);
    };
    AtomicReference<String> foundInContext = new AtomicReference<>();
    HttpInterceptor takesFromContext = (request, chain) -> chain.proceed(request).map(response -> {
        foundInContext.set(chain.context().get("contextValue", String.class));
        return response;
    });
    StandardHttpPipeline pipeline = pipeline(addsToContext, takesFromContext);
    LiveHttpResponse response = sendRequestTo(pipeline);
    assertThat(response.status(), is(OK));
    assertThat(foundInContext.get(), is("expected"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HttpInterceptor (com.hotels.styx.api.HttpInterceptor)7 Eventual (com.hotels.styx.api.Eventual)6 OK (com.hotels.styx.api.HttpResponseStatus.OK)6 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)6 Test (org.junit.jupiter.api.Test)6 HttpHandler (com.hotels.styx.api.HttpHandler)5 LiveHttpRequest.get (com.hotels.styx.api.LiveHttpRequest.get)5 LiveHttpResponse.response (com.hotels.styx.api.LiveHttpResponse.response)5 HttpInterceptorContext (com.hotels.styx.server.HttpInterceptorContext)5 RequestTracker (com.hotels.styx.server.track.RequestTracker)5 Support.requestContext (com.hotels.styx.support.Support.requestContext)5 InetSocketAddress (java.net.InetSocketAddress)5 Arrays.asList (java.util.Arrays.asList)5 Collections.singletonList (java.util.Collections.singletonList)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Consumer (java.util.function.Consumer)5 Stream (java.util.stream.Stream)5