Search in sources :

Example 61 with LiveHttpResponse

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

the class StyxBackendServiceClientFactoryTest method usesTheOriginSpecifiedInTheStickySessionCookie.

@Test
public void usesTheOriginSpecifiedInTheStickySessionCookie() {
    BackendService backendService = newBackendServiceBuilder().origins(newOriginBuilder("localhost", 9091).id("x").build(), newOriginBuilder("localhost", 9092).id("y").build(), newOriginBuilder("localhost", 9093).id("z").build()).stickySessionConfig(newStickySessionConfigBuilder().enabled(true).build()).build();
    BackendServiceClient styxBackendServiceClient = new StyxBackendServiceClientFactory(environment).createClient(backendService, newOriginsInventoryBuilder(environment.centralisedMetrics(), backendService).hostClientFactory((pool) -> {
        if (pool.getOrigin().id().equals(id("x"))) {
            return hostClient(response(OK).header("X-Origin-Id", "x").build());
        } else if (pool.getOrigin().id().equals(id("y"))) {
            return hostClient(response(OK).header("X-Origin-Id", "y").build());
        } else {
            return hostClient(response(OK).header("X-Origin-Id", "z").build());
        }
    }).build(), new CachingOriginStatsFactory(environment.centralisedMetrics()));
    LiveHttpRequest requestz = get("/some-req").cookies(requestCookie(STICKY_COOKIE, id("z").toString())).build();
    LiveHttpRequest requestx = get("/some-req").cookies(requestCookie(STICKY_COOKIE, id("x").toString())).build();
    LiveHttpRequest requesty = get("/some-req").cookies(requestCookie(STICKY_COOKIE, id("y").toString())).build();
    LiveHttpResponse responsez = Mono.from(styxBackendServiceClient.sendRequest(requestz, requestContext())).block();
    LiveHttpResponse responsex = Mono.from(styxBackendServiceClient.sendRequest(requestx, requestContext())).block();
    LiveHttpResponse responsey = Mono.from(styxBackendServiceClient.sendRequest(requesty, requestContext())).block();
    assertThat(responsex.header("X-Origin-Id").get(), is("x"));
    assertThat(responsey.header("X-Origin-Id").get(), is("y"));
    assertThat(responsez.header("X-Origin-Id").get(), is("z"));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) StyxBackendServiceClient(com.hotels.styx.client.StyxBackendServiceClient) BackendServiceClient(com.hotels.styx.client.BackendServiceClient) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 62 with LiveHttpResponse

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

the class ConfigurationContextResolverInterceptorTest method resolvesConfigurationContext.

@Test
public void resolvesConfigurationContext() {
    LiveHttpRequest request = get("/").build();
    Configuration.Context context = context(Map.of("key1", "value1", "key2", "value2"));
    ConfigurationContextResolver configurationContextResolver = configurationContextResolver(request, context);
    ConfigurationContextResolverInterceptor interceptor = new ConfigurationContextResolverInterceptor(configurationContextResolver);
    TestChain chain = new TestChain();
    Eventual<LiveHttpResponse> responseObservable = interceptor.intercept(request, chain);
    assertThat(Mono.from(responseObservable).block(), hasStatus(OK));
    assertThat(chain.proceedWasCalled, is(true));
    assertThat(chain.context().get("config.context", Configuration.Context.class), is(context));
}
Also used : ConfigurationContextResolver(com.hotels.styx.api.configuration.ConfigurationContextResolver) Support.requestContext(com.hotels.styx.support.Support.requestContext) Configuration(com.hotels.styx.api.configuration.Configuration) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 63 with LiveHttpResponse

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

the class HopByHopHeadersRemovingInterceptorTest method removesConnectionHeadersFromResponse.

@Test
public void removesConnectionHeadersFromResponse() throws Exception {
    LiveHttpResponse response = Mono.from(interceptor.intercept(get("/foo").build(), returnsResponse(response().header(CONNECTION, "Foo, Bar, Baz").header("Foo", "abc").header("Foo", "def").header("Bar", "one, two, three").build()))).block();
    assertThat(response.header(CONNECTION), isAbsent());
    assertThat(response.header("Foo"), isAbsent());
    assertThat(response.header("Bar"), isAbsent());
    assertThat(response.header("Baz"), isAbsent());
}
Also used : LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 64 with LiveHttpResponse

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

the class RouteHandlerAdapterTest method injectsToPipelineWhenRouteFound.

@Test
public void injectsToPipelineWhenRouteFound() {
    HttpHandler pipeline = mock(HttpHandler.class);
    when(pipeline.handle(any(LiveHttpRequest.class), any(HttpInterceptor.Context.class))).thenReturn(Eventual.of(respOk));
    HttpRouter router = mock(HttpRouter.class);
    when(router.route(any(LiveHttpRequest.class), any(HttpInterceptor.Context.class))).thenReturn(Optional.of(pipeline));
    LiveHttpResponse response = Mono.from(new RouteHandlerAdapter(router).handle(request, requestContext())).block();
    assertThat(response.status(), is(OK));
}
Also used : Support.requestContext(com.hotels.styx.support.Support.requestContext) HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) HttpRouter(com.hotels.styx.server.HttpRouter) Test(org.junit.jupiter.api.Test)

Example 65 with LiveHttpResponse

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

the class StyxServerTest method serverDoesNotStartUntilPluginsAreStarted.

@Test
public void serverDoesNotStartUntilPluginsAreStarted() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    StyxServer styxServer = styxServerWithPlugins(Map.of("slowlyStartingPlugin", new Plugin() {

        @Override
        public void styxStarting() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request1, Chain chain) {
            return Eventual.of(response(OK).build());
        }
    }));
    try {
        styxServer.startAsync();
        Thread.sleep(10);
        assertThat(styxServer.proxyHttpAddress(), nullValue());
        latch.countDown();
        eventually(() -> assertThat(styxServer.proxyHttpAddress().getPort(), is(greaterThan(0))));
    } finally {
        stopIfRunning(styxServer);
    }
}
Also used : LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) NamedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Test(org.junit.jupiter.api.Test)

Aggregations

LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)80 Test (org.junit.jupiter.api.Test)69 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)25 Support.requestContext (com.hotels.styx.support.Support.requestContext)21 Eventual (com.hotels.styx.api.Eventual)15 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)15 HttpHandler (com.hotels.styx.api.HttpHandler)14 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)14 Mono (reactor.core.publisher.Mono)14 ByteStream (com.hotels.styx.api.ByteStream)13 Context (com.hotels.styx.api.HttpInterceptor.Context)13 OK (com.hotels.styx.api.HttpResponseStatus.OK)13 LiveHttpResponse.response (com.hotels.styx.api.LiveHttpResponse.response)13 TransportLostException (com.hotels.styx.api.exceptions.TransportLostException)12 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 Matchers.is (org.hamcrest.Matchers.is)12 Buffer (com.hotels.styx.api.Buffer)11 LiveHttpRequest.get (com.hotels.styx.api.LiveHttpRequest.get)11 HttpInterceptorContext (com.hotels.styx.server.HttpInterceptorContext)11