use of io.servicetalk.buffer.api.Buffer 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.buffer.api.Buffer in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientGracefulClose.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void clientGracefulClose(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
InetSocketAddress serverAddress = bindHttpEchoServer();
StreamingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildStreaming();
CountDownLatch onCloseLatch = new CountDownLatch(1);
Processor<Buffer, Buffer> requestBody = newProcessor();
client.onClose().subscribe(onCloseLatch::countDown);
// We want to make a request, and intentionally not complete it. While the request is in process we invoke
// closeAsyncGracefully and verify that we wait until the request has completed before the underlying
// transport is closed.
StreamingHttpRequest request = client.post("/").payloadBody(fromSource(requestBody));
StreamingHttpResponse response = client.request(request).toFuture().get();
client.closeAsyncGracefully().subscribe();
// We expect this to timeout, because we have not completed the outstanding request.
assertFalse(onCloseLatch.await(300, MILLISECONDS));
requestBody.onComplete();
response.payloadBody().ignoreElements().toFuture();
onCloseLatch.await();
}
use of io.servicetalk.buffer.api.Buffer 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.buffer.api.Buffer in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method continue100.
private void continue100(boolean failExpectation) throws Exception {
InetSocketAddress serverAddress = bindHttpEchoServer();
try (StreamingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildStreaming()) {
Processor<Buffer, Buffer> requestBody1 = newProcessor();
StreamingHttpRequest request = client.post("/").payloadBody(fromSource(requestBody1));
request.addHeader(EXPECT, CONTINUE);
if (failExpectation) {
request.addHeader(EXPECT_FAIL_HEADER, "notused");
}
StreamingHttpResponse response = client.request(request).toFuture().get();
if (failExpectation) {
assertEquals(EXPECTATION_FAILED, response.status());
assertFalse(response.payloadBody().toIterable().iterator().hasNext());
} else {
assertEquals(HttpResponseStatus.CONTINUE, response.status());
String payloadBody = "foo";
requestBody1.onNext(client.executionContext().bufferAllocator().fromAscii(payloadBody));
requestBody1.onComplete();
Iterator<Buffer> responseBody = response.payloadBody().toIterable().iterator();
assertTrue(responseBody.hasNext());
Buffer next = responseBody.next();
assertNotNull(next);
assertEquals(payloadBody, next.toString(UTF_8));
assertFalse(responseBody.hasNext());
}
}
}
use of io.servicetalk.buffer.api.Buffer in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method fullDuplexMode.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void fullDuplexMode(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
InetSocketAddress serverAddress = bindHttpEchoServer();
try (StreamingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildStreaming()) {
Processor<Buffer, Buffer> requestBody1 = newProcessor();
StreamingHttpResponse response1 = client.request(client.post("/0").payloadBody(fromSource(requestBody1))).toFuture().get();
Processor<Buffer, Buffer> requestBody2 = newProcessor();
StreamingHttpResponse response2 = client.request(client.post("/1").payloadBody(fromSource(requestBody2))).toFuture().get();
Iterator<Buffer> response1Payload = response1.payloadBody().toIterable().iterator();
Iterator<Buffer> response2Payload = response2.payloadBody().toIterable().iterator();
fullDuplexModeWrite(client, requestBody1, "foo1", requestBody2, "bar1", response1Payload, response2Payload);
fullDuplexModeWrite(client, requestBody1, "foo2", requestBody2, "bar2", response1Payload, response2Payload);
requestBody1.onComplete();
requestBody2.onComplete();
assertFalse(response1Payload.hasNext());
assertFalse(response2Payload.hasNext());
}
}
Aggregations