use of io.servicetalk.http.api.ReservedBlockingHttpConnection in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientSendsInvalidContentLength.
private void clientSendsInvalidContentLength(boolean addTrailers, BiConsumer<HttpHeaders, Integer> headersModifier) throws Exception {
assumeFalse(!h2PriorKnowledge && addTrailers, "HTTP/1.1 does not support Content-Length with trailers");
InetSocketAddress serverAddress = bindHttpEchoServer();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).appendClientFilter(client1 -> new StreamingHttpClientFilter(client1) {
@Override
protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
return request.toRequest().map(req -> {
req.headers().remove(TRANSFER_ENCODING);
headersModifier.accept(req.headers(), req.payloadBody().readableBytes());
return req.toStreamingRequest();
}).flatMap(delegate::request);
}
}).buildBlocking()) {
HttpRequest request = client.get("/").payloadBody("a", textSerializerUtf8());
if (addTrailers) {
request.trailers().set("mytrailer", "myvalue");
}
if (h2PriorKnowledge) {
assertThrows(H2StreamResetException.class, () -> client.request(request));
} else {
try (ReservedBlockingHttpConnection reservedConn = client.reserveConnection(request)) {
assertThrows(IOException.class, () -> {
// Either the current request or the next one should fail
reservedConn.request(request);
reservedConn.request(client.get("/"));
});
}
}
}
}
use of io.servicetalk.http.api.ReservedBlockingHttpConnection in project servicetalk by apple.
the class ConnectionFactoryFilterTest method reserveConnection.
@Test
void reserveConnection() throws Exception {
AtomicInteger activeConnections = new AtomicInteger();
CountDownLatch doneLatch = new CountDownLatch(1);
client = clientBuilder.appendConnectionFactoryFilter(newConnectionFactoryFilter(connectionCounter(activeConnections, doneLatch))).buildBlocking();
ReservedBlockingHttpConnection c = client.reserveConnection(client.get("/"));
assertThat("Unexpected active connections.", activeConnections.get(), is(1));
c.close();
doneLatch.await();
assertThat("Unexpected active connections.", activeConnections.get(), is(0));
}
use of io.servicetalk.http.api.ReservedBlockingHttpConnection 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.ReservedBlockingHttpConnection 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.ReservedBlockingHttpConnection in project servicetalk by apple.
the class AlpnClientAndServerTest method testAlpnConnection.
@ParameterizedTest(name = "serverAlpnProtocols={0}, clientAlpnProtocols={1}," + " expectedProtocol={2}, expectedExceptionType={3}")
@MethodSource("clientExecutors")
void testAlpnConnection(List<String> serverSideProtocols, List<String> clientSideProtocols, @Nullable HttpProtocolVersion expectedProtocol, @Nullable Class<? extends Throwable> expectedExceptionType, @Nullable Class<? extends Throwable> optionalExceptionWrapperType) throws Exception {
setUp(serverSideProtocols, clientSideProtocols, expectedProtocol);
if (expectedExceptionType != null) {
assertThrows(expectedExceptionType, optionalExceptionWrapperType, () -> client.request(client.get("/")));
return;
}
try (ReservedBlockingHttpConnection connection = client.reserveConnection(client.get("/"))) {
assertThat(connection.connectionContext().protocol(), is(expectedProtocol));
assertThat(connection.connectionContext().sslSession(), is(notNullValue()));
assertResponseAndServiceContext(connection.request(client.get("/")));
}
}
Aggregations