use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method serverWriteTrailers.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void serverWriteTrailers(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
String payloadBody = "foo";
String myTrailerName = "mytrailer";
h1ServerContext = HttpServers.forAddress(localAddress(0)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).listenStreaming((ctx, request, responseFactory) -> request.payloadBody().map(Buffer::readableBytes).collect(AtomicInteger::new, (contentSize, bufferSize) -> {
contentSize.addAndGet(bufferSize);
return contentSize;
}).flatMap(contentSize -> succeeded(responseFactory.ok().transform(new ContentSizeTrailersTransformer(myTrailerName, contentSize))))).toFuture().get();
InetSocketAddress serverAddress = (InetSocketAddress) h1ServerContext.listenAddress();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpRequest request = client.post("/").payloadBody(payloadBody, textSerializerUtf8());
HttpResponse response = client.request(request);
assertEquals(0, response.payloadBody().readableBytes());
CharSequence responseTrailer = response.trailers().get(myTrailerName);
assertNotNull(responseTrailer);
assertEquals(payloadBody.length(), Integer.parseInt(responseTrailer.toString()));
}
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method serverAllowDropTrailers.
private void serverAllowDropTrailers(boolean allowDrop, boolean clientAddTrailerHeader) throws Exception {
String trailerName = "t1";
String trailerValue = "v1";
SingleSource.Processor<HttpHeaders, HttpHeaders> trailersProcessor = Processors.newSingleProcessor();
h1ServerContext = HttpServers.forAddress(localAddress(0)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).allowDropRequestTrailers(allowDrop).listenStreamingAndAwait((ctx, request, responseFactory) -> succeeded(responseFactory.ok().payloadBody(// intermediate Buffer transform may drop trailers
request.transformPayloadBody(buf -> buf).transform(new StatelessTrailersTransformer<Buffer>() {
@Override
protected HttpHeaders payloadComplete(final HttpHeaders trailers) {
trailersProcessor.onSuccess(trailers);
return trailers;
}
}).payloadBody())));
InetSocketAddress serverAddress = (InetSocketAddress) h1ServerContext.listenAddress();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).allowDropResponseTrailers(allowDrop).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpRequest request = client.get("/");
if (clientAddTrailerHeader) {
request.headers().add(TRAILER, trailerName);
}
request.trailers().add(trailerName, trailerValue);
client.request(request);
HttpHeaders requestTrailers = fromSource(trailersProcessor).toFuture().get();
if (allowDrop && !clientAddTrailerHeader) {
assertFalse(requestTrailers.contains(trailerName));
} else {
assertHeaderValue(requestTrailers, trailerName, trailerValue);
}
}
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientWriteTrailers.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void clientWriteTrailers(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()) {
String payloadBody = "foo";
String myTrailerName = "mytrailer";
String myTrailerValue = "myvalue";
HttpRequest request = client.post("/").payloadBody(payloadBody, textSerializerUtf8());
request.trailers().add(myTrailerName, myTrailerValue);
HttpResponse response = client.request(request);
assertEquals(payloadBody, response.payloadBody(textSerializerUtf8()));
CharSequence responseTrailer = response.trailers().get(myTrailerName);
assertNotNull(responseTrailer);
assertEquals(0, responseTrailer.toString().compareToIgnoreCase(myTrailerValue));
}
}
use of io.servicetalk.http.api.HttpRequest 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.HttpRequest in project servicetalk by apple.
the class RedirectingClientAndConnectionFilterTest method redirectFilterNoHostHeaderAbsoluteLocation.
@ParameterizedTest(name = "{displayName} [{index}] {0}-{1}")
@MethodSource("requesterTypes")
void redirectFilterNoHostHeaderAbsoluteLocation(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, format("http://%s/next", hostHeader(HostAndPort.of(remoteAddress())))));
}
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 => we can not infer that the absolute-form location is relative, don't redirect
response = client.request(client.get("/").version(HTTP_1_0).addHeader("X-REDIRECT", "TRUE"));
assertThat(response.status(), equalTo(PERMANENT_REDIRECT));
}
Aggregations