use of io.servicetalk.http.api.StatelessTrailersTransformer in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientAllowDropTrailers.
private void clientAllowDropTrailers(boolean allowDrop, boolean serverAddTrailerHeader) throws Exception {
String trailerName = "t1";
String trailerValue = "v1";
InetSocketAddress serverAddress = serverAddTrailerHeader ? bindHttpEchoServerWithTrailer(trailerName) : bindHttpEchoServer();
try (StreamingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).allowDropResponseTrailers(allowDrop).executionStrategy(clientExecutionStrategy).buildStreaming()) {
StreamingHttpResponse response = client.request(client.get("/").transform(new StatelessTrailersTransformer<Buffer>() {
@Override
protected HttpHeaders payloadComplete(final HttpHeaders trailers) {
trailers.add(trailerName, trailerValue);
return trailers;
}
})).toFuture().get();
SingleSource.Processor<HttpHeaders, HttpHeaders> trailersProcessor = Processors.newSingleProcessor();
// intermediate Buffer transform may drop trailers
response.transformPayloadBody(buf -> buf).transform(new StatelessTrailersTransformer<Buffer>() {
@Override
protected HttpHeaders payloadComplete(final HttpHeaders trailers) {
trailersProcessor.onSuccess(trailers);
return trailers;
}
}).messageBody().ignoreElements().toFuture().get();
HttpHeaders responseTrailers = fromSource(trailersProcessor).toFuture().get();
if (allowDrop && !serverAddTrailerHeader) {
assertFalse(responseTrailers.contains(trailerName));
} else {
assertHeaderValue(responseTrailers, trailerName, trailerValue);
}
}
}
use of io.servicetalk.http.api.StatelessTrailersTransformer 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.StatelessTrailersTransformer in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method bindHttpEchoServer.
private InetSocketAddress bindHttpEchoServer(@Nullable StreamingHttpServiceFilterFactory filterFactory, @Nullable CountDownLatch connectionOnClosingLatch) throws Exception {
HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0)).protocols(h2PriorKnowledge ? h2Default() : h1Default());
if (filterFactory != null) {
serverBuilder.appendServiceFilter(filterFactory);
}
if (connectionOnClosingLatch != null) {
serverBuilder.appendConnectionAcceptorFilter(original -> new DelegatingConnectionAcceptor(original) {
@Override
public Completable accept(final ConnectionContext context) {
((NettyConnectionContext) context).onClosing().whenFinally(connectionOnClosingLatch::countDown).subscribe();
return completed();
}
});
}
h1ServerContext = serverBuilder.listenStreaming((ctx, request, responseFactory) -> {
StreamingHttpResponse resp;
if (request.headers().contains(EXPECT, CONTINUE)) {
if (request.headers().contains(EXPECT_FAIL_HEADER)) {
return succeeded(responseFactory.expectationFailed());
} else {
resp = responseFactory.continueResponse();
}
} else {
resp = responseFactory.ok();
}
resp = resp.transformMessageBody(pub -> pub.ignoreElements().merge(request.messageBody())).transform(new StatelessTrailersTransformer<>());
CharSequence contentType = request.headers().get(CONTENT_TYPE);
if (contentType != null) {
resp.setHeader(CONTENT_TYPE, contentType);
}
CharSequence contentLength = request.headers().get(CONTENT_LENGTH);
if (contentLength != null) {
resp.setHeader(CONTENT_LENGTH, contentLength);
}
CharSequence transferEncoding = request.headers().get(TRANSFER_ENCODING);
if (transferEncoding != null) {
resp.setHeader(TRANSFER_ENCODING, transferEncoding);
}
resp.headers().set(COOKIE, request.headers().valuesIterator(COOKIE));
return succeeded(resp);
}).toFuture().get();
return (InetSocketAddress) h1ServerContext.listenAddress();
}
use of io.servicetalk.http.api.StatelessTrailersTransformer in project servicetalk by apple.
the class HttpLifecycleObserverTest method makeRequestAndAssertResponse.
private void makeRequestAndAssertResponse(String path, HttpProtocol protocol, HttpResponseStatus status, int contentLength) throws Exception {
StreamingHttpClient client = streamingHttpClient();
StreamingHttpRequest request = contentLength == 0 ? client.get(path) : client.post(path).payloadBody(Publisher.from(CONTENT.duplicate())).transform(// adds empty trailers
new StatelessTrailersTransformer<>());
StreamingHttpResponse response = client.request(request).toFuture().get();
assertResponse(response, protocol.version, status, contentLength);
}
use of io.servicetalk.http.api.StatelessTrailersTransformer in project servicetalk by apple.
the class TestServiceStreaming method newEchoResponse.
private static StreamingHttpResponse newEchoResponse(final StreamingHttpRequest req, final StreamingHttpResponseFactory factory) {
final StreamingHttpResponse response = factory.ok().version(req.version()).transformMessageBody(pub -> pub.ignoreElements().merge(req.messageBody())).transform(new StatelessTrailersTransformer<>());
final CharSequence contentLength = req.headers().get(CONTENT_LENGTH);
if (contentLength != null) {
response.addHeader(CONTENT_LENGTH, contentLength);
}
final CharSequence contentType = req.headers().get(CONTENT_TYPE);
if (contentType != null) {
response.addHeader(CONTENT_TYPE, contentType);
}
final CharSequence transferEncoding = req.headers().get(TRANSFER_ENCODING);
if (transferEncoding != null) {
response.addHeader(TRANSFER_ENCODING, transferEncoding);
}
return response;
}
Aggregations