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"));
}
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));
}
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());
}
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));
}
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);
}
}
Aggregations