use of io.servicetalk.http.api.ReservedBlockingHttpConnection in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientReserveConnectionMultipleRequests.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void clientReserveConnectionMultipleRequests(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
String responseBody1 = "1.hello world.1";
String responseBody2 = "2.hello world.2";
InetSocketAddress serverAddress = bindHttpEchoServer();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpRequest request = client.get("/");
ReservedBlockingHttpConnection reservedConnection = client.reserveConnection(request);
try {
// We interleave the requests intentionally to make sure the internal transport sequences the
// reads and writes correctly.
HttpResponse response1 = client.request(request.payloadBody(responseBody1, textSerializerUtf8()));
HttpResponse response2 = client.request(request.payloadBody(responseBody2, textSerializerUtf8()));
assertEquals(responseBody1, response1.payloadBody(textSerializerUtf8()));
assertEquals(responseBody2, response2.payloadBody(textSerializerUtf8()));
} finally {
reservedConnection.release();
}
}
}
use of io.servicetalk.http.api.ReservedBlockingHttpConnection in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method serverSendsInvalidContentLength.
private void serverSendsInvalidContentLength(boolean addTrailers, BiConsumer<HttpHeaders, Integer> headersModifier) throws Exception {
assumeFalse(!h2PriorKnowledge && addTrailers, "HTTP/1.1 does not support Content-Length with trailers");
InetSocketAddress serverAddress = bindHttpEchoServer(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
return delegate().handle(ctx, request, responseFactory).flatMap(resp -> resp.transformMessageBody(// honored during "streaming -> aggregated -> streaming" conversion.
pub -> addTrailers ? pub : pub.filter(i -> i instanceof Buffer)).toResponse().map(aggResp -> {
aggResp.headers().remove(TRANSFER_ENCODING);
headersModifier.accept(aggResp.headers(), aggResp.payloadBody().readableBytes());
return aggResp.toStreamingResponse();
}));
}
}, null);
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpRequest request = client.get("/").payloadBody("a", textSerializerUtf8());
if (addTrailers) {
request.trailers().set("mytrailer", "myvalue");
}
if (h2PriorKnowledge) {
assertThrows(Http2Exception.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 ExecutionStrategyInContextTest method testBlocking.
@ParameterizedTest(name = "customStrategy={0}")
@ValueSource(booleans = { false, true })
void testBlocking(boolean customStrategy) throws Exception {
BlockingHttpClient client = initClientAndServer(builder -> builder.listenBlocking((ctx, request, responseFactory) -> {
serviceStrategyRef.set(ctx.executionContext().executionStrategy());
return responseFactory.ok();
}), customStrategy).buildBlocking();
clientAsCloseable = client;
if (!customStrategy) {
assert expectedClientStrategy == null;
expectedClientStrategy = customStrategyBuilder().offloadNone().offloadEvent().build();
assert expectedServerStrategy == null;
expectedServerStrategy = customStrategyBuilder().offloadReceiveData().build();
}
HttpExecutionStrategy clientStrat = client.executionContext().executionStrategy();
assertThat("Unexpected client strategy.", clientStrat, equalStrategies(expectedClientStrategy));
client.request(client.get("/"));
assertThat("Unexpected service strategy", serviceStrategyRef.get(), equalStrategies(expectedServerStrategy));
ReservedBlockingHttpConnection conn = client.reserveConnection(client.get("/"));
assertThat("Unexpected connection strategy (from execution context).", conn.executionContext().executionStrategy(), equalStrategies(expectedClientStrategy));
assertThat("Unexpected connection strategy (from execution context).", conn.connectionContext().executionContext().executionStrategy(), equalStrategies(expectedClientStrategy));
}
use of io.servicetalk.http.api.ReservedBlockingHttpConnection in project servicetalk by apple.
the class SniTest method noSniClientDefaultServerFallbackSuccess.
@ParameterizedTest(name = "protocols={0}, alpn={1}")
@MethodSource("protocolsAndAlpn")
void noSniClientDefaultServerFallbackSuccess(List<HttpProtocol> protocols, boolean useALPN) throws Exception {
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).protocols(protocolConfigs(protocols)).sslConfig(trustedServerConfig(alpnIds(protocols, useALPN)), singletonMap("no_match" + SNI_HOSTNAME, untrustedServerConfig())).listenBlockingAndAwait(newSslVerifyService());
BlockingHttpClient client = HttpClients.forSingleAddress(getLoopbackAddress().getHostName(), serverHostAndPort(serverContext).port()).protocols(protocolConfigs(protocols)).sslConfig(configureAlpn(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()), protocols, useALPN).build()).inferSniHostname(false).buildBlocking()) {
HttpRequest request = client.get("/");
ReservedBlockingHttpConnection reserved = client.reserveConnection(request);
assertNotNull(reserved.connectionContext().sslSession());
assertEquals(HttpResponseStatus.OK, reserved.request(request).status());
reserved.release();
}
}
use of io.servicetalk.http.api.ReservedBlockingHttpConnection in project servicetalk by apple.
the class DefaultSingleAddressHttpClientBuilderTest method hostToCharSequenceFunction.
private static void hostToCharSequenceFunction(String hostNamePrefix, String hostName, String hostNameSuffix, @Nullable Integer port) throws Exception {
try (ServerContext serverCtx = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build()).listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
BlockingHttpClient client = forResolvedAddress(hostNamePrefix + hostName + hostNameSuffix + (port == null ? "" : port), u -> serverCtx.listenAddress()).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).hostnameVerificationAlgorithm("").build()).buildBlocking()) {
ReservedBlockingHttpConnection conn = client.reserveConnection(client.get("/"));
try {
SSLSession sslSession = conn.connectionContext().sslSession();
assertNotNull(sslSession);
assertThat(sslSession.getPeerHost(), startsWith(hostName));
InetSocketAddress socketAddress = (InetSocketAddress) conn.connectionContext().remoteAddress();
assertEquals(socketAddress.getPort(), sslSession.getPeerPort());
} finally {
conn.release();
}
}
}
Aggregations