Search in sources :

Example 36 with LiveHttpResponse

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

the class InterceptorPipelineBuilderTest method buildsPipelineWithInterceptors.

@Test
public void buildsPipelineWithInterceptors() throws Exception {
    HttpHandler pipeline = new InterceptorPipelineBuilder(environment, plugins, handler, false).build();
    LiveHttpResponse response = Mono.from(pipeline.handle(get("/foo").build(), requestContext())).block();
    assertThat(response.header("plug1"), isValue("1"));
    assertThat(response.header("plug2"), isValue("1"));
    assertThat(response.status(), is(OK));
}
Also used : HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 37 with LiveHttpResponse

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

the class StandardHttpPipelineTest method passesThroughAllInterceptors.

@Test
public void passesThroughAllInterceptors() {
    List<String> requestReceivers = new ArrayList<>();
    List<String> responseReceivers = new ArrayList<>();
    StandardHttpPipeline pipeline = pipeline(recordingInterceptor("interceptor 1", requestReceivers::add, responseReceivers::add), recordingInterceptor("interceptor 2", requestReceivers::add, responseReceivers::add), recordingInterceptor("interceptor 3", requestReceivers::add, responseReceivers::add));
    LiveHttpResponse response = sendRequestTo(pipeline);
    assertThat(response.status(), is(OK));
    assertThat(requestReceivers, contains("interceptor 1", "interceptor 2", "interceptor 3"));
    assertThat(responseReceivers, contains("interceptor 3", "interceptor 2", "interceptor 1"));
}
Also used : ArrayList(java.util.ArrayList) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 38 with LiveHttpResponse

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

the class StandardHttpPipelineTest method sendsExceptionUponExtraSubscriptionInsideInterceptor.

@ParameterizedTest
@MethodSource("multipleSubscriptionInterceptors")
public void sendsExceptionUponExtraSubscriptionInsideInterceptor(HttpInterceptor interceptor) throws Exception {
    HttpHandler handler = (request, context) -> Eventual.of(response(OK).build());
    List<HttpInterceptor> interceptors = singletonList(interceptor);
    StandardHttpPipeline pipeline = new StandardHttpPipeline(interceptors, handler, RequestTracker.NO_OP);
    Eventual<LiveHttpResponse> responseObservable = pipeline.handle(get("/").build(), requestContext());
    assertThrows(IllegalStateException.class, () -> Mono.from(responseObservable).block());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpHandler(com.hotels.styx.api.HttpHandler) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 39 with LiveHttpResponse

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

the class StandardHttpPipelineTest method sendsExceptionUponMultipleSubscription.

@Test
public void sendsExceptionUponMultipleSubscription() {
    HttpHandler handler = (request, context) -> Eventual.of(response(OK).build());
    StandardHttpPipeline pipeline = new StandardHttpPipeline(handler);
    Eventual<LiveHttpResponse> responseObservable = pipeline.handle(get("/").build(), requestContext());
    LiveHttpResponse response = Mono.from(responseObservable).block();
    assertThat(response.status(), is(OK));
    assertThrows(IllegalStateException.class, () -> Mono.from(responseObservable).block());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 40 with LiveHttpResponse

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

the class HttpPipelineHandler method onResponseObservableError.

private State onResponseObservableError(ChannelHandlerContext ctx, Throwable cause, Object requestId) {
    if (!ongoingRequest.id().equals(requestId)) {
        return this.state();
    }
    metrics.proxy().server().requestsCancelled("responseError").increment();
    cancelSubscription();
    LOGGER.error(warningMessage(format("message='Error proxying request', requestId=%s cause=%s", requestId, cause)));
    if (cause instanceof ConsumerDisconnectedException) {
        return TERMINATED;
    }
    LiveHttpResponse response = exceptionToResponse(cause, ongoingRequest, originsHeaderName);
    responseWriterFactory.create(ctx).write(response).handle((ignore, exception) -> {
        if (exception != null) {
            httpErrorStatusListener.proxyErrorOccurred(cause);
            httpErrorStatusListener.proxyErrorOccurred(exception);
        } else {
            httpErrorStatusListener.proxyErrorOccurred(ongoingRequest, remoteAddress(ctx), response.status(), cause);
            statsSink.onComplete(ongoingRequest.id(), response.status().code());
            tracker.endTrack(ongoingRequest);
        }
        ctx.close();
        return null;
    }).handle((ignore, exception) -> {
        statsSink.onTerminate(ongoingRequest.id());
        tracker.endTrack(ongoingRequest);
        if (exception != null) {
            LOGGER.error(warningMessage("message='Error during write completion handling'"), exception);
        }
        return null;
    });
    return TERMINATED;
}
Also used : RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) REQUEST_TIMEOUT(com.hotels.styx.api.HttpResponseStatus.REQUEST_TIMEOUT) ExceptionStatusMapperKt.buildExceptionStatusMapper(com.hotels.styx.server.netty.connectors.ExceptionStatusMapperKt.buildExceptionStatusMapper) QueueDrainingEventProcessor(com.hotels.styx.common.QueueDrainingEventProcessor) SENDING_RESPONSE(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State.SENDING_RESPONSE) TERMINATED(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State.TERMINATED) SERVICE_UNAVAILABLE(com.hotels.styx.api.HttpResponseStatus.SERVICE_UNAVAILABLE) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) BAD_REQUEST(com.hotels.styx.api.HttpResponseStatus.BAD_REQUEST) DecoderException(io.netty.handler.codec.DecoderException) HttpHandler(com.hotels.styx.api.HttpHandler) OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) REQUEST_ENTITY_TOO_LARGE(com.hotels.styx.api.HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE) HTTP_1_1(com.hotels.styx.api.HttpVersion.HTTP_1_1) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) EMPTY_LAST_CONTENT(io.netty.handler.codec.http.LastHttpContent.EMPTY_LAST_CONTENT) InetSocketAddress(java.net.InetSocketAddress) ResponseTimeoutException(com.hotels.styx.api.exceptions.ResponseTimeoutException) StateMachine(com.hotels.styx.common.StateMachine) String.format(java.lang.String.format) BaseSubscriber(reactor.core.publisher.BaseSubscriber) CONTENT_LENGTH(com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) DO_NOT_MODIFY_RESPONSE(com.hotels.styx.server.netty.connectors.ResponseEnhancer.DO_NOT_MODIFY_RESPONSE) Optional(java.util.Optional) IGNORE_REQUEST_PROGRESS(com.hotels.styx.server.RequestProgressListener.IGNORE_REQUEST_PROGRESS) HttpErrorStatusListener(com.hotels.styx.server.HttpErrorStatusListener) RequestTimeoutException(com.hotels.styx.server.RequestTimeoutException) ACCEPTING_REQUESTS(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State.ACCEPTING_REQUESTS) WAITING_FOR_RESPONSE(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State.WAITING_FOR_RESPONSE) ResourceExhaustedException(com.hotels.styx.client.connectionpool.ResourceExhaustedException) CompositeMeterRegistry(io.micrometer.core.instrument.composite.CompositeMeterRegistry) CompletableFuture(java.util.concurrent.CompletableFuture) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) NoAvailableHostsException(com.hotels.styx.api.exceptions.NoAvailableHostsException) RequestProgressListener(com.hotels.styx.server.RequestProgressListener) CONNECTION(com.hotels.styx.api.HttpHeaderNames.CONNECTION) Objects.requireNonNull(java.util.Objects.requireNonNull) PluginException(com.hotels.styx.api.plugins.spi.PluginException) StyxClientException(com.hotels.styx.client.StyxClientException) NoServiceConfiguredException(com.hotels.styx.server.NoServiceConfiguredException) CLOSE(io.netty.channel.ChannelFutureListener.CLOSE) Eventual(com.hotels.styx.api.Eventual) ConsumerDisconnectedException(com.hotels.styx.client.netty.ConsumerDisconnectedException) Logger(org.slf4j.Logger) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) IGNORE_ERROR_STATUS(com.hotels.styx.server.HttpErrorStatusListener.IGNORE_ERROR_STATUS) UTF_8(java.nio.charset.StandardCharsets.UTF_8) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) IOException(java.io.IOException) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) Buffer(com.hotels.styx.api.Buffer) BadRequestException(com.hotels.styx.server.BadRequestException) ContentOverflowException(com.hotels.styx.api.ContentOverflowException) Flux(reactor.core.publisher.Flux) ByteStream(com.hotels.styx.api.ByteStream) FsmEventProcessor(com.hotels.styx.common.FsmEventProcessor) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) SENDING_RESPONSE_CLIENT_CLOSED(com.hotels.styx.server.netty.connectors.HttpPipelineHandler.State.SENDING_RESPONSE_CLIENT_CLOSED) Subscription(org.reactivestreams.Subscription) GATEWAY_TIMEOUT(com.hotels.styx.api.HttpResponseStatus.GATEWAY_TIMEOUT) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) StyxException(com.hotels.styx.api.exceptions.StyxException) CentralisedMetrics(com.hotels.styx.metrics.CentralisedMetrics) BAD_GATEWAY(com.hotels.styx.api.HttpResponseStatus.BAD_GATEWAY) Id(com.hotels.styx.api.Id) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) BadHttpResponseException(com.hotels.styx.client.BadHttpResponseException) ConsumerDisconnectedException(com.hotels.styx.client.netty.ConsumerDisconnectedException) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse)

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