Search in sources :

Example 46 with LiveHttpResponse

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

the class StaticPipelineBuilderTest method appliesPluginsInOrderTheyAreConfigured.

@Test
public void appliesPluginsInOrderTheyAreConfigured() throws Exception {
    Iterable<NamedPlugin> plugins = List.of(interceptor("Test-A", appendResponseHeader("X-From-Plugin", "A")), interceptor("Test-B", appendResponseHeader("X-From-Plugin", "B")));
    HttpHandler handler = new StaticPipelineFactory(clientFactory, environment, registry, plugins, executor, false).build();
    LiveHttpResponse response = Mono.from(handler.handle(get("/foo").build(), requestContext())).block();
    assertThat(response.status(), is(OK));
    assertThat(response.headers("X-From-Plugin"), hasItems("B", "A"));
}
Also used : HttpHandler(com.hotels.styx.api.HttpHandler) NamedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 47 with LiveHttpResponse

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

the class StyxBackendServiceClientTest method removesBadContentLength.

@Test
public void removesBadContentLength() {
    StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(OK).addHeader(CONTENT_LENGTH, 50).addHeader(TRANSFER_ENCODING, CHUNKED).build()));
    StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))).build();
    LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(SOME_REQ, requestContext())).block();
    assertThat(response.status(), is(OK));
    assertThat(response.contentLength().isPresent(), is(false));
    assertThat(response.header(TRANSFER_ENCODING).get(), is("chunked"));
}
Also used : LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 48 with LiveHttpResponse

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

the class StyxBackendServiceClientTest method incrementsResponseStatusMetricsFor500.

@Test
public void incrementsResponseStatusMetricsFor500() {
    StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(INTERNAL_SERVER_ERROR).build()));
    StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))).build();
    LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(SOME_REQ, requestContext())).block();
    assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
    verify(hostClient).sendRequest(eq(SOME_REQ), any(Context.class));
    assertThat(meterRegistry.get("proxy.client.responseCode.errorStatus").tag("statusCode", "500").counter(), is(notNullValue()));
}
Also used : Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 49 with LiveHttpResponse

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

the class StyxBackendServiceClientTest method prefersStickyOrigins.

@Test
public void prefersStickyOrigins() {
    Origin origin = originWithId("localhost:234", "App-X", "Origin-Y");
    StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(OK).build()));
    LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));
    StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).loadBalancer(loadBalancer).metrics(metrics).build();
    LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(get("/foo").cookies(requestCookie("styx_origin_" + Id.GENERIC_APP, "Origin-Y")).build(), requestContext())).block();
    assertThat(response.status(), is(OK));
    ArgumentCaptor<LoadBalancer.Preferences> argCaptor = ArgumentCaptor.forClass(LoadBalancer.Preferences.class);
    verify(loadBalancer).choose(argCaptor.capture());
    assertThat(argCaptor.getValue().preferredOrigins(), is(Optional.of("Origin-Y")));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 50 with LiveHttpResponse

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

the class StyxBackendServiceClientTest method prefersRestrictedOrigins.

@Test
public void prefersRestrictedOrigins() {
    Origin origin = originWithId("localhost:234", "App-X", "Origin-Y");
    StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(OK).build()));
    LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));
    StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).loadBalancer(loadBalancer).metrics(metrics).originsRestrictionCookieName("restrictedOrigin").build();
    LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(get("/foo").cookies(requestCookie("restrictedOrigin", "Origin-Y")).build(), requestContext())).block();
    assertThat(response.status(), is(OK));
    ArgumentCaptor<LoadBalancer.Preferences> argCaptor = ArgumentCaptor.forClass(LoadBalancer.Preferences.class);
    verify(loadBalancer).choose(argCaptor.capture());
    assertThat(argCaptor.getValue().preferredOrigins(), is(Optional.of("Origin-Y")));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) 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