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"));
}
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));
}
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));
}
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));
}
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)));
}
Aggregations