use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class MalformedDataAfterHttpMessageTest method afterResponse.
@Test
void afterResponse() throws Exception {
ServerSocketChannel server = nettyServer(RESPONSE_MSG);
try (BlockingHttpClient client = stClient(server.localAddress())) {
HttpRequest request = client.get("/1");
ReservedBlockingHttpConnection connection = client.reserveConnection(request);
CountDownLatch connectionClosedLatch = new CountDownLatch(1);
connection.connectionContext().onClose().whenFinally(connectionClosedLatch::countDown).subscribe();
validateClientResponse(connection.request(request));
// Verify that the next request fails and connection gets closed:
// The exception generation is currently racy. A write maybe triggered while the channel is not active
// which will lead to ClosedChannelException.
assertThat(assertThrows(Exception.class, () -> connection.request(connection.get("/2"))), anyOf(instanceOf(DecoderException.class), instanceOf(ClosedChannelException.class)));
connectionClosedLatch.await();
} finally {
server.close().sync();
}
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class ServerRespondsOnClosingTest method handleRequests.
private void handleRequests() {
Exchange exchange;
while ((exchange = requests.poll()) != null) {
HttpRequest request = exchange.request;
HttpResponse response = RESPONSE_FACTORY.ok().setHeader("Request-Path", request.path()).payloadBody(RESPONSE_PAYLOAD_BODY, textSerializerUtf8());
if (request.hasQueryParameter("serverShouldClose")) {
response.setHeader(CONNECTION, CLOSE);
}
exchange.responseProcessor.onSuccess(response);
}
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class TracingHttpServiceFilterTest method testRequestWithTraceKey.
@Test
void testRequestWithTraceKey() throws Exception {
CountingInMemorySpanEventListener spanListener = new CountingInMemorySpanEventListener();
try (ServerContext context = buildServer(spanListener)) {
try (HttpClient client = forSingleAddress(serverHostAndPort(context)).build()) {
String traceId = randomHexId();
String spanId = randomHexId();
String parentSpanId = randomHexId();
String requestUrl = "/";
HttpRequest request = client.get(requestUrl);
request.headers().set(TRACE_ID, traceId).set(SPAN_ID, spanId).set(PARENT_SPAN_ID, parentSpanId).set(SAMPLED, "0");
HttpResponse response = client.request(request).toFuture().get();
TestSpanState serverSpanState = response.payloadBody(SPAN_STATE_SERIALIZER);
assertSpan(spanListener, traceId, spanId, requestUrl, serverSpanState, false);
}
}
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class TracingHttpServiceFilterTest method tracerThrowsReturnsErrorResponse.
@Test
void tracerThrowsReturnsErrorResponse() throws Exception {
when(mockTracer.buildSpan(any())).thenThrow(DELIBERATE_EXCEPTION);
try (ServerContext context = HttpServers.forAddress(localAddress(0)).appendServiceFilter(new TracingHttpServiceFilter(mockTracer, "testServer")).listenStreamingAndAwait(((ctx, request, responseFactory) -> succeeded(responseFactory.forbidden())))) {
try (HttpClient client = forSingleAddress(serverHostAndPort(context)).build()) {
HttpRequest request = client.get("/");
HttpResponse response = client.request(request).toFuture().get();
assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
}
}
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class TracingHttpServiceFilterTest method testRequestWithTraceKeyWithoutSampled.
@Test
void testRequestWithTraceKeyWithoutSampled() throws Exception {
CountingInMemorySpanEventListener spanListener = new CountingInMemorySpanEventListener();
try (ServerContext context = buildServer(spanListener)) {
try (HttpClient client = forSingleAddress(serverHostAndPort(context)).build()) {
String traceId = randomHexId();
String spanId = randomHexId();
String requestUrl = "/";
HttpRequest request = client.get(requestUrl);
request.headers().set(TRACE_ID, traceId).set(SPAN_ID, spanId);
HttpResponse response = client.request(request).toFuture().get();
TestSpanState serverSpanState = response.payloadBody(SPAN_STATE_SERIALIZER);
assertSpan(spanListener, traceId, spanId, requestUrl, serverSpanState, true);
}
}
}
Aggregations