Search in sources :

Example 6 with HttpInterceptor

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

the class StandardHttpPipelineTest method interceptorsCanPassInformationThroughContextAfterRequest.

@Test
public void interceptorsCanPassInformationThroughContextAfterRequest() {
    HttpInterceptor addsToContext = (request, chain) -> chain.proceed(request).map(response -> {
        chain.context().add("contextValue", "expected");
        return response;
    });
    AtomicReference<String> foundInContext = new AtomicReference<>();
    HttpInterceptor takesFromContext = (request, chain) -> chain.proceed(request).map(response -> {
        foundInContext.set(chain.context().get("contextValue", String.class));
        return response;
    });
    // add + take happens on the way back, so order must be reserved
    StandardHttpPipeline pipeline = pipeline(takesFromContext, addsToContext);
    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 7 with HttpInterceptor

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

the class StandardHttpPipelineTest method interceptorReceivesNewContext.

@Test
public void interceptorReceivesNewContext() {
    HttpInterceptor expectNewContext = (request, chain) -> {
        Object seen = chain.context().get("seen", Object.class);
        assertThat("Old context reused" + seen, seen, is(nullValue()));
        chain.context().add("seen", true);
        return chain.proceed(request);
    };
    StandardHttpPipeline pipeline = pipeline(expectNewContext);
    assertThat(sendRequestTo(pipeline).status(), is(OK));
    // make the same request again to ensure a new context is used
    assertThat(sendRequestTo(pipeline).status(), is(OK));
}
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) 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