Search in sources :

Example 36 with HttpResponse

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

the class HttpStreamerTest method streamsRequestAndAggregatesResponses.

@Test
public void streamsRequestAndAggregatesResponses() {
    HttpHandler httpHandler = (request, ctx) -> Eventual.of(LiveHttpResponse.response(OK).body(ByteStream.from("abcdef", UTF_8)).build());
    HttpResponse response = Mono.from(new HttpStreamer(500, httpHandler).handle(HttpRequest.post("/").body("ABCDEF", UTF_8).build(), requestContext())).block();
    assertThat(response.bodyAs(UTF_8), is("abcdef"));
}
Also used : Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) HttpHandler(com.hotels.styx.api.HttpHandler) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Mono(reactor.core.publisher.Mono) HttpRequest(com.hotels.styx.api.HttpRequest) Test(org.junit.jupiter.api.Test) Support.requestContext(com.hotels.styx.support.Support.requestContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteStream(com.hotels.styx.api.ByteStream) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) HttpHandler(com.hotels.styx.api.HttpHandler) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 37 with HttpResponse

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

the class HttpPipelineHandlerTest method pluginPipelineThrowsAnExceptionInAcceptingRequestsState.

@Test
public void pluginPipelineThrowsAnExceptionInAcceptingRequestsState() throws Exception {
    Throwable cause = new RuntimeException("Simulated Styx plugin exception");
    pipeline = mock(HttpHandler.class);
    when(pipeline.handle(anyObject(), any(HttpInterceptor.Context.class))).thenThrow(cause);
    handler = createHandler(pipeline);
    assertThat(handler.state(), is(ACCEPTING_REQUESTS));
    handler.channelRead0(ctx, request);
    ArgumentCaptor<LiveHttpResponse> captor = ArgumentCaptor.forClass(LiveHttpResponse.class);
    verify(responseWriter).write(captor.capture());
    HttpResponse response = Mono.from(captor.getValue().aggregate(100)).block();
    assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
    assertThat(response.header(CONNECTION), is(Optional.of("close")));
    assertThat(response.header(CONTENT_LENGTH), is(Optional.of("29")));
    assertThat(response.bodyAs(UTF_8), is("Site temporarily unavailable."));
    verify(responseEnhancer).enhance(any(LiveHttpResponse.Transformer.class), eq(request));
    verify(errorListener).proxyErrorOccurred(request, InetSocketAddress.createUnresolved("localhost", 2), INTERNAL_SERVER_ERROR, cause);
    verify(statsCollector).onTerminate(request.id());
    assertThat(handler.state(), is(TERMINATED));
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpHandler(com.hotels.styx.api.HttpHandler) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 38 with HttpResponse

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

the class HttpPipelineHandlerTest method mapsStyxClientExceptionToInternalServerErrorInWaitingForResponseState.

@Test
public void mapsStyxClientExceptionToInternalServerErrorInWaitingForResponseState() throws Exception {
    // In Waiting For Response state,
    // The response observable emits a StyxClientException.
    // Then, respond with INTERNAL_SERVER_ERROR and close the channel.
    setupHandlerTo(WAITING_FOR_RESPONSE);
    responseObservable.onError(new StyxClientException("Client error occurred", new JustATestException()));
    assertThat(responseUnsubscribed.get(), is(true));
    ArgumentCaptor<LiveHttpResponse> captor = ArgumentCaptor.forClass(LiveHttpResponse.class);
    verify(responseWriter).write(captor.capture());
    HttpResponse response = Mono.from(captor.getValue().aggregate(100)).block();
    assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
    assertThat(response.header(CONNECTION), is(Optional.of("close")));
    assertThat(response.header(CONTENT_LENGTH), is(Optional.of("29")));
    assertThat(response.bodyAs(UTF_8), is("Site temporarily unavailable."));
    writerFuture.complete(null);
    verify(statsCollector).onComplete(request.id(), 500);
    verify(errorListener).proxyErrorOccurred(any(LiveHttpRequest.class), any(InetSocketAddress.class), eq(INTERNAL_SERVER_ERROR), any(RuntimeException.class));
    // NOTE: channel closure is not verified. This is because cannot mock channel future.
    verify(ctx).close();
    assertThat(handler.state(), is(TERMINATED));
}
Also used : JustATestException(com.hotels.styx.support.JustATestException) StyxClientException(com.hotels.styx.client.StyxClientException) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) InetSocketAddress(java.net.InetSocketAddress) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 39 with HttpResponse

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

the class HttpPipelineHandlerTest method responseObservableEmitsContentOverflowExceptionInWaitingForResponseState.

@Test
public void responseObservableEmitsContentOverflowExceptionInWaitingForResponseState() throws Exception {
    // In Waiting For Response state,
    // When response observable emits ContentOverflowException.
    // Then respond with BAD_GATEWAY and close the channel
    setupHandlerTo(WAITING_FOR_RESPONSE);
    responseObservable.onError(new ContentOverflowException("Request Send Error"));
    assertThat(responseUnsubscribed.get(), is(true));
    ArgumentCaptor<LiveHttpResponse> captor = ArgumentCaptor.forClass(LiveHttpResponse.class);
    verify(responseWriter).write(captor.capture());
    HttpResponse response = Mono.from(captor.getValue().aggregate(100)).block();
    assertThat(response.status(), is(BAD_GATEWAY));
    assertThat(response.header(CONNECTION), is(Optional.of("close")));
    assertThat(response.header(CONTENT_LENGTH), is(Optional.of("29")));
    assertThat(response.bodyAs(UTF_8), is("Site temporarily unavailable."));
    verify(responseEnhancer).enhance(any(LiveHttpResponse.Transformer.class), eq(request));
    writerFuture.complete(null);
    verify(statsCollector).onComplete(request.id(), 502);
    verify(errorListener).proxyErrorOccurred(any(LiveHttpRequest.class), any(InetSocketAddress.class), eq(BAD_GATEWAY), any(RuntimeException.class));
    // NOTE: channel closure is not verified. This is because cannot mock channel future.
    verify(ctx).close();
    assertThat(handler.state(), is(TERMINATED));
}
Also used : LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) InetSocketAddress(java.net.InetSocketAddress) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ContentOverflowException(com.hotels.styx.api.ContentOverflowException) Test(org.junit.jupiter.api.Test)

Example 40 with HttpResponse

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

the class MetricsSnapshot method downloadFrom.

public static MetricsSnapshot downloadFrom(String host, int port) throws IOException {
    HttpClient client = new StyxHttpClient.Builder().build();
    HttpResponse response = await(client.sendRequest(get(format("http://%s:%d/admin/metrics", host, port)).build()));
    return new MetricsSnapshot(decodeToMap(response.bodyAs(UTF_8)));
}
Also used : StyxHttpClient(com.hotels.styx.client.StyxHttpClient) StyxHttpClient(com.hotels.styx.client.StyxHttpClient) HttpClient(com.hotels.styx.client.HttpClient) HttpResponse(com.hotels.styx.api.HttpResponse)

Aggregations

HttpResponse (com.hotels.styx.api.HttpResponse)107 Test (org.junit.jupiter.api.Test)99 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)30 HttpRequest (com.hotels.styx.api.HttpRequest)18 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Eventual (com.hotels.styx.api.Eventual)10 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)9 StyxHttpClient (com.hotels.styx.client.StyxHttpClient)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 TlsSettings (com.hotels.styx.api.extension.service.TlsSettings)7 StyxObjectRecord (com.hotels.styx.StyxObjectRecord)6 HttpHandler (com.hotels.styx.api.HttpHandler)6 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)6 OK (com.hotels.styx.api.HttpResponseStatus.OK)5 HttpClient (com.hotels.styx.client.HttpClient)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)4 Plugin (com.hotels.styx.api.plugins.spi.Plugin)4 PluginFactory (com.hotels.styx.api.plugins.spi.PluginFactory)4 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)3