use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class RedirectingClientAndConnectionFilterTest method redirectFilterNoHostHeaderRelativeLocation.
@ParameterizedTest(name = "{displayName} [{index}] {0}-{1}")
@MethodSource("requesterTypes")
void redirectFilterNoHostHeaderRelativeLocation(final RequesterType type, final SecurityType security) throws Exception {
setUp(security);
BlockingHttpRequester client = asBlockingRequester(createFilter(type, (responseFactory, request) -> {
if (request.requestTarget().equals("/")) {
return succeeded(responseFactory.permanentRedirect().addHeader(LOCATION, "/next"));
}
return succeeded(responseFactory.ok());
}, newFilterFactory()));
HttpRequest request = client.get("/");
HttpResponse response = client.request(request);
assertThat(response.status(), equalTo(PERMANENT_REDIRECT));
response = client.request(request.addHeader("X-REDIRECT", "TRUE"));
assertThat(response.status(), equalTo(OK));
// HTTP/1.0 doesn't support HOST, ensure that we don't get any errors and perform relative redirect
response = client.request(client.get("/").version(HTTP_1_0).addHeader("X-REDIRECT", "TRUE"));
assertThat(response.status(), equalTo(OK));
}
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 MalformedDataAfterHttpMessageTest method afterRequest.
@Test
void afterRequest() throws Exception {
try (ServerContext server = stServer();
BlockingHttpClient client = stClient(server.listenAddress())) {
Buffer malformedBody = client.executionContext().bufferAllocator().fromAscii(CONTENT).writeShort(// malformed data at the end of the request msg
0);
HttpRequest request = client.post("/").setHeader(CONTENT_LENGTH, valueOf(CONTENT.length())).setHeader(CONTENT_TYPE, TEXT_PLAIN).payloadBody(malformedBody);
ReservedBlockingHttpConnection connection = client.reserveConnection(request);
CountDownLatch connectionClosedLatch = new CountDownLatch(1);
connection.connectionContext().onClose().whenFinally(connectionClosedLatch::countDown).subscribe();
assertThrows(IOException.class, () -> connection.request(request));
// Server should close the connection:
connectionClosedLatch.await();
}
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class TracingHttpServiceFilterTest method testRequestWithTraceKeyWithNegativeSampledAndAlwaysTrueSampler.
@Test
void testRequestWithTraceKeyWithNegativeSampledAndAlwaysTrueSampler() throws Exception {
final CountingInMemorySpanEventListener spanListener = new CountingInMemorySpanEventListener();
try (ServerContext context = buildServer(spanListener, (__, ___) -> true)) {
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).set(SAMPLED, "0");
HttpResponse response = client.request(request).toFuture().get();
TestSpanState serverSpanState = response.payloadBody(SPAN_STATE_SERIALIZER);
assertSpan(spanListener, traceId, spanId, requestUrl, serverSpanState, true);
}
}
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class TracingHttpRequesterFilterTest method tracerThrowsReturnsErrorResponse.
@Test
void tracerThrowsReturnsErrorResponse() throws Exception {
when(mockTracer.buildSpan(any())).thenThrow(DELIBERATE_EXCEPTION);
try (ServerContext context = buildServer()) {
try (HttpClient client = forSingleAddress(serverHostAndPort(context)).appendConnectionFilter(new TracingHttpRequesterFilter(mockTracer, "testClient")).build()) {
HttpRequest request = client.get("/");
ExecutionException ex = assertThrows(ExecutionException.class, () -> client.request(request).toFuture().get());
assertThat(ex.getCause(), is(DELIBERATE_EXCEPTION));
}
}
}
Aggregations