Search in sources :

Example 1 with LiveHttpResponse

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

the class StyxBackendServiceClientTest method incrementsResponseStatusMetricsForBadResponse.

@Test
public void incrementsResponseStatusMetricsForBadResponse() {
    StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(BAD_REQUEST).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(BAD_REQUEST));
    verify(hostClient).sendRequest(eq(SOME_REQ), any(Context.class));
    assertThat(meterRegistry.get("proxy.client.responseCode.errorStatus").tag("statusCode", "400").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 2 with LiveHttpResponse

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

the class StyxBackendServiceClientTest method sendsRequestToHostChosenByLoadBalancer.

@Test
public void sendsRequestToHostChosenByLoadBalancer() {
    StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(OK).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));
    verify(hostClient).sendRequest(eq(SOME_REQ), any(Context.class));
}
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 3 with LiveHttpResponse

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

the class StyxBackendServiceClientTest method updatesCountersWhenTransactionIsCancelled.

@Test
public void updatesCountersWhenTransactionIsCancelled() {
    Origin origin = originWithId("localhost:234", "App-X", "Origin-Y");
    Processor<LiveHttpResponse, LiveHttpResponse> processor = EmitterProcessor.create();
    StyxHostHttpClient hostClient = mockHostClient(processor);
    StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)))).metrics(metrics).build();
    StepVerifier.create(styxHttpClient.sendRequest(SOME_REQ, requestContext())).thenCancel().verify();
    assertThat(meterRegistry.get("proxy.client.requests.cancelled").tags("appId", "App-X", "originId", "Origin-Y").counter().count(), is(1.0));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 4 with LiveHttpResponse

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

the class StyxBackendServiceClientTest method retriesWhenRetryPolicyTellsToRetry.

@Test
public void retriesWhenRetryPolicyTellsToRetry() {
    RetryPolicy retryPolicy = mockRetryPolicy(true, false);
    StyxHostHttpClient firstClient = mockHostClient(Flux.error(new OriginUnreachableException(ORIGIN_1, new RuntimeException("An error occurred"))));
    StyxHostHttpClient secondClient = mockHostClient(Flux.just(response(OK).build()));
    StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(ORIGIN_1, toHandler(firstClient), firstClient)), Optional.of(remoteHost(ORIGIN_2, toHandler(secondClient), secondClient)))).retryPolicy(retryPolicy).build();
    LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(SOME_REQ, requestContext())).block();
    ArgumentCaptor<RetryPolicy.Context> retryContext = ArgumentCaptor.forClass(RetryPolicy.Context.class);
    ArgumentCaptor<LoadBalancer> lbPreference = ArgumentCaptor.forClass(LoadBalancer.class);
    ArgumentCaptor<LoadBalancer.Preferences> lbContext = ArgumentCaptor.forClass(LoadBalancer.Preferences.class);
    verify(retryPolicy).evaluate(retryContext.capture(), lbPreference.capture(), lbContext.capture());
    assertThat(retryContext.getValue().appId(), is(backendService.id()));
    assertThat(retryContext.getValue().currentRetryCount(), is(1));
    assertThat(retryContext.getValue().lastException().get(), Matchers.instanceOf(OriginUnreachableException.class));
    assertThat(lbPreference.getValue(), notNullValue());
    assertThat(lbContext.getValue().preferredOrigins(), is(Optional.empty()));
    assertThat(lbContext.getValue().avoidOrigins(), is(asList(ORIGIN_1)));
    assertThat(response.status(), is(OK));
    InOrder ordered = inOrder(firstClient, secondClient);
    ordered.verify(firstClient).sendRequest(eq(SOME_REQ), any(Context.class));
    ordered.verify(secondClient).sendRequest(eq(SOME_REQ), any(Context.class));
}
Also used : Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) InOrder(org.mockito.InOrder) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) RetryPolicy(com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy) Test(org.junit.jupiter.api.Test)

Example 5 with LiveHttpResponse

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

the class StyxBackendServiceClientTest method constructsRetryContextWhenLoadBalancerDoesNotFindAvailableOrigins.

@Test
public void constructsRetryContextWhenLoadBalancerDoesNotFindAvailableOrigins() {
    RetryPolicy retryPolicy = mockRetryPolicy(true, true, true);
    StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(OK).build()));
    StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.empty(), Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))).retryPolicy(retryPolicy).build();
    LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(SOME_REQ, requestContext())).block();
    ArgumentCaptor<RetryPolicy.Context> retryContext = ArgumentCaptor.forClass(RetryPolicy.Context.class);
    ArgumentCaptor<LoadBalancer> lbPreference = ArgumentCaptor.forClass(LoadBalancer.class);
    ArgumentCaptor<LoadBalancer.Preferences> lbContext = ArgumentCaptor.forClass(LoadBalancer.Preferences.class);
    verify(retryPolicy).evaluate(retryContext.capture(), lbPreference.capture(), lbContext.capture());
    assertThat(retryContext.getValue().appId(), is(backendService.id()));
    assertThat(retryContext.getValue().currentRetryCount(), is(1));
    assertThat(retryContext.getValue().lastException(), is(Optional.empty()));
    assertThat(lbPreference.getValue(), notNullValue());
    assertThat(lbContext.getValue().avoidOrigins(), is(empty()));
    assertThat(lbContext.getValue().preferredOrigins(), is(Optional.empty()));
    assertThat(response.status(), is(OK));
}
Also used : Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) RetryPolicy(com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy) 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