Search in sources :

Example 41 with LiveHttpResponse

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

the class HttpPipelineHandler method onLegitimateRequest.

private State onLegitimateRequest(LiveHttpRequest request, ChannelHandlerContext ctx) {
    statsSink.onRequest(request.id());
    LiveHttpRequest v11Request = request.newBuilder().version(HTTP_1_1).build();
    tracker.trackRequest(request, () -> this.state().toString());
    ongoingRequest = request;
    // generates a response.
    try {
        Eventual<LiveHttpResponse> responseEventual = httpPipeline.handle(v11Request, new HttpInterceptorContext(this.secure, remoteAddress(ctx), ctx.executor()));
        responseEventual.subscribe(new BaseSubscriber<LiveHttpResponse>() {

            @Override
            public void hookOnSubscribe(Subscription s) {
                subscription = s;
                s.request(1);
            }

            @Override
            public void hookOnComplete() {
                eventProcessor.submit(new ResponseObservableCompletedEvent(ctx, request.id()));
            }

            @Override
            public void hookOnError(Throwable cause) {
                eventProcessor.submit(new ResponseObservableErrorEvent(ctx, cause, request.id()));
            }

            @Override
            public void hookOnNext(LiveHttpResponse response) {
                eventProcessor.submit(new ResponseReceivedEvent(response, ctx));
            }
        });
        return WAITING_FOR_RESPONSE;
    } catch (Throwable cause) {
        LiveHttpResponse response = exceptionToResponse(cause, request, originsHeaderName);
        httpErrorStatusListener.proxyErrorOccurred(request, remoteAddress(ctx), response.status(), cause);
        statsSink.onTerminate(request.id());
        tracker.endTrack(ongoingRequest);
        if (ctx.channel().isActive()) {
            respondAndClose(ctx, response);
        }
        return TERMINATED;
    }
}
Also used : LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Subscription(org.reactivestreams.Subscription)

Example 42 with LiveHttpResponse

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

the class ViaHeaderAppendingInterceptorTest method appendsHttp10RequestVersionInResponseViaHeader.

@Test
public void appendsHttp10RequestVersionInResponseViaHeader() throws Exception {
    LiveHttpResponse response = Mono.from(interceptor.intercept(get("/foo").build(), ANY_RESPONSE_HANDLER)).block();
    assertThat(response.headers().get(VIA), isValue("1.1 styx"));
}
Also used : LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 43 with LiveHttpResponse

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

the class InstrumentedPluginTest method metricIsNotRecordedWhenErrorStatusIsReturnedByChain.

@Test
public void metricIsNotRecordedWhenErrorStatusIsReturnedByChain() {
    Chain chain = request -> aResponse(INTERNAL_SERVER_ERROR);
    String pluginName = "doNotRecordMe";
    InstrumentedPlugin plugin = instrumentedPlugin(pluginName, PASS_THROUGH);
    LiveHttpResponse response = Mono.from(plugin.intercept(someRequest, chain)).block();
    assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
    assertThat(getStatusCount(pluginName, "500"), is(0.0));
    assertThat(getErrorCount(pluginName), is(0.0));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) Eventual.error(com.hotels.styx.api.Eventual.error) Matchers.not(org.hamcrest.Matchers.not) PluginException(com.hotels.styx.api.plugins.spi.PluginException) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) PASS_THROUGH(com.hotels.styx.api.plugins.spi.Plugin.PASS_THROUGH) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Counter(io.micrometer.core.instrument.Counter) Eventual(com.hotels.styx.api.Eventual) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Mono(reactor.core.publisher.Mono) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) Metrics.formattedExceptionName(com.hotels.styx.api.Metrics.formattedExceptionName) Chain(com.hotels.styx.api.HttpInterceptor.Chain) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) MeterRegistry(com.hotels.styx.api.MeterRegistry) Environment(com.hotels.styx.Environment) OK(com.hotels.styx.api.HttpResponseStatus.OK) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) BAD_GATEWAY(com.hotels.styx.api.HttpResponseStatus.BAD_GATEWAY) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Mockito.mock(org.mockito.Mockito.mock) Chain(com.hotels.styx.api.HttpInterceptor.Chain) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 44 with LiveHttpResponse

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

the class InstrumentedPluginTest method metricIsRecordedWhenResponseIsMappedToErrorStatus.

@Test
public void metricIsRecordedWhenResponseIsMappedToErrorStatus() {
    Chain chain = request -> aResponse(OK);
    String pluginName = "replaceStatus1";
    InstrumentedPlugin plugin = instrumentedPlugin(pluginName, (request, aChain) -> aChain.proceed(request).map(response -> responseWithNewStatusCode(response, INTERNAL_SERVER_ERROR)));
    LiveHttpResponse response = Mono.from(plugin.intercept(someRequest, chain)).block();
    assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
    assertThat(getStatusCount(pluginName, "500"), is(1.0));
    assertThat(getErrorCount(pluginName), is(1.0));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) Eventual.error(com.hotels.styx.api.Eventual.error) Matchers.not(org.hamcrest.Matchers.not) PluginException(com.hotels.styx.api.plugins.spi.PluginException) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) PASS_THROUGH(com.hotels.styx.api.plugins.spi.Plugin.PASS_THROUGH) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Counter(io.micrometer.core.instrument.Counter) Eventual(com.hotels.styx.api.Eventual) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Mono(reactor.core.publisher.Mono) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) Metrics.formattedExceptionName(com.hotels.styx.api.Metrics.formattedExceptionName) Chain(com.hotels.styx.api.HttpInterceptor.Chain) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) MeterRegistry(com.hotels.styx.api.MeterRegistry) Environment(com.hotels.styx.Environment) OK(com.hotels.styx.api.HttpResponseStatus.OK) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) BAD_GATEWAY(com.hotels.styx.api.HttpResponseStatus.BAD_GATEWAY) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Mockito.mock(org.mockito.Mockito.mock) Chain(com.hotels.styx.api.HttpInterceptor.Chain) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 45 with LiveHttpResponse

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

the class InstrumentedPluginTest method errorsMetricIsNotRecordedWhenPluginReturnsNon5005xxStatusEarly.

@Test
public void errorsMetricIsNotRecordedWhenPluginReturnsNon5005xxStatusEarly() {
    String pluginName = "returnEarly";
    InstrumentedPlugin plugin = instrumentedPlugin(pluginName, (request, chain) -> aResponse(BAD_GATEWAY));
    LiveHttpResponse response = Mono.from(plugin.intercept(someRequest, chain)).block();
    verify(chain, never()).proceed(any(LiveHttpRequest.class));
    assertThat(response.status(), is(BAD_GATEWAY));
    assertThat(getStatusCount(pluginName, "502"), is(1.0));
    assertThat(getErrorCount(pluginName), is(0.0));
}
Also used : LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) 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