use of io.servicetalk.http.api.HttpRequestMethod.GET 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.HttpRequestMethod.GET 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.HttpRequestMethod.GET 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.HttpRequestMethod.GET 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.HttpRequestMethod.GET in project servicetalk by apple.
the class HttpRequestEncoderTest method protocolPayloadEndOutboundShouldNotTriggerOnFailedFlush.
@Test
void protocolPayloadEndOutboundShouldNotTriggerOnFailedFlush() throws Exception {
AtomicReference<CloseHandler> closeHandlerRef = new AtomicReference<>();
try (CompositeCloseable resources = newCompositeCloseable()) {
Processor serverCloseTrigger = newCompletableProcessor();
CountDownLatch serverChannelLatch = new CountDownLatch(1);
AtomicReference<Channel> serverChannelRef = new AtomicReference<>();
ReadOnlyTcpServerConfig sConfig = new TcpServerConfig().asReadOnly();
ServerContext serverContext = resources.prepend(TcpServerBinder.bind(localAddress(0), sConfig, false, SEC, null, (channel, observer) -> DefaultNettyConnection.initChannel(channel, SEC.bufferAllocator(), SEC.executor(), SEC.ioExecutor(), forPipelinedRequestResponse(false, channel.config()), defaultFlushStrategy(), null, new TcpServerChannelInitializer(sConfig, observer).andThen(channel2 -> {
serverChannelRef.compareAndSet(null, channel2);
serverChannelLatch.countDown();
}), defaultStrategy(), mock(Protocol.class), observer, false, __ -> false), connection -> {
}).toFuture().get());
ReadOnlyHttpClientConfig cConfig = new HttpClientConfig().asReadOnly();
assert cConfig.h1Config() != null;
NettyConnection<Object, Object> conn = resources.prepend(TcpConnector.connect(null, serverHostAndPort(serverContext), cConfig.tcpConfig(), false, CEC, (channel, connectionObserver) -> {
CloseHandler closeHandler = spy(forPipelinedRequestResponse(true, channel.config()));
closeHandlerRef.compareAndSet(null, closeHandler);
return DefaultNettyConnection.initChannel(channel, CEC.bufferAllocator(), CEC.executor(), CEC.ioExecutor(), closeHandler, defaultFlushStrategy(), null, new TcpClientChannelInitializer(cConfig.tcpConfig(), connectionObserver).andThen(new HttpClientChannelInitializer(getByteBufAllocator(CEC.bufferAllocator()), cConfig.h1Config(), closeHandler)).andThen(channel2 -> channel2.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
// Propagate the user event in the pipeline before
// triggering the test condition.
ctx.fireUserEventTriggered(evt);
if (evt instanceof ChannelInputShutdownReadComplete) {
serverCloseTrigger.onComplete();
}
}
})), defaultStrategy(), HTTP_1_1, connectionObserver, true, __ -> false);
}, NoopTransportObserver.INSTANCE).toFuture().get());
// The server needs to wait to close the conneciton until after the client has established the connection.
serverChannelLatch.await();
Channel serverChannel = serverChannelRef.get();
assertNotNull(serverChannel);
assumeFalse(serverChannel instanceof NioSocketChannel, "Windows doesn't emit ChannelInputShutdownReadComplete. Investigation Required.");
((SocketChannel) serverChannel).config().setSoLinger(0);
// Close and send RST concurrently with client write
serverChannel.close();
StreamingHttpRequest request = reqRespFactory.post("/closeme");
fromSource(serverCloseTrigger).toFuture().get();
Completable write = conn.write(from(request, allocator.fromAscii("Bye"), EmptyHttpHeaders.INSTANCE));
assertThrows(ExecutionException.class, () -> write.toFuture().get());
CloseHandler closeHandler = closeHandlerRef.get();
assertNotNull(closeHandler);
verify(closeHandler, never()).protocolPayloadEndOutbound(any(), any());
}
}
Aggregations