use of io.servicetalk.http.api.HttpRequest 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.HttpRequest in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method cookiesRoundTrip.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void cookiesRoundTrip(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
InetSocketAddress serverAddress = bindHttpEchoServer();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpRequest request = client.get("/");
String requestCookie = "name1=value1; name2=value2; name3=value3";
request.addHeader(COOKIE, requestCookie);
HttpResponse response = client.request(request);
CharSequence responseCookie = response.headers().get(COOKIE);
assertNotNull(responseCookie);
HttpCookiePair cookie = response.headers().getCookie("name1");
assertNotNull(cookie);
assertEquals("value1", cookie.value());
cookie = response.headers().getCookie("name2");
assertNotNull(cookie);
assertEquals("value2", cookie.value());
cookie = response.headers().getCookie("name3");
assertNotNull(cookie);
assertEquals("value3", cookie.value());
}
}
use of io.servicetalk.http.api.HttpRequest 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.HttpRequest in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientHeaderCookieRemovalAndIteration.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void clientHeaderCookieRemovalAndIteration(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
InetSocketAddress serverAddress = bindHttpEchoServer();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpRequest request = client.get("/");
headerCookieRemovalAndIteration(request.headers());
}
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method trailersWithContentLength.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void trailersWithContentLength(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
final String expectedPayload = "Hello World!";
final String expectedPayloadLength = valueOf(expectedPayload.length());
final String expectedTrailer = "foo";
final String expectedTrailerValue = "bar";
final AtomicReference<HttpRequest> requestReceived = new AtomicReference<>();
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).listenBlockingAndAwait((ctx, request, responseFactory) -> {
requestReceived.set(request);
return responseFactory.ok().addTrailer(expectedTrailer, expectedTrailerValue).addHeader(CONTENT_LENGTH, expectedPayloadLength).payloadBody(expectedPayload, textSerializerUtf8());
});
BlockingHttpClient client = forSingleAddress(HostAndPort.of((InetSocketAddress) serverContext.listenAddress())).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpResponse response = client.request(client.post("/").addTrailer(expectedTrailer, expectedTrailerValue).addHeader(CONTENT_LENGTH, expectedPayloadLength).payloadBody(expectedPayload, textSerializerUtf8()));
assertThat(response.status(), is(OK));
assertThat(response.payloadBody(textSerializerUtf8()), equalTo(expectedPayload));
assertHeaders(h2PriorKnowledge, response.headers(), expectedPayloadLength);
assertTrailers(response.trailers(), expectedTrailer, expectedTrailerValue);
// Verify what server received:
HttpRequest request = requestReceived.get();
assertThat(request.payloadBody(textSerializerUtf8()), equalTo(expectedPayload));
assertHeaders(h2PriorKnowledge, request.headers(), expectedPayloadLength);
assertTrailers(request.trailers(), expectedTrailer, expectedTrailerValue);
}
}
Aggregations