Search in sources :

Example 11 with StreamingHttpConnection

use of io.servicetalk.http.api.StreamingHttpConnection in project servicetalk by apple.

the class ExpectContinueTest method serverError.

@ParameterizedTest(name = "protocol={0} withCL={1}")
@MethodSource("arguments")
void serverError(HttpProtocol protocol, boolean withCL) throws Exception {
    try (HttpServerContext serverContext = startServer(protocol, (ctx, request, response) -> {
        requestReceived.countDown();
        returnResponse.await();
        if (request.headers().contains(EXPECT, CONTINUE)) {
            response.status(INTERNAL_SERVER_ERROR);
        }
        response.sendMetaData().close();
    });
        StreamingHttpClient client = createClient(serverContext, protocol);
        StreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get()) {
        TestPublisher<Buffer> payload = new TestPublisher.Builder<Buffer>().singleSubscriber().build();
        connection.request(newRequest(connection, withCL, true, payload)).subscribe(responses::add);
        requestReceived.await();
        assertThat("Unexpected subscribe to payload body before 100 (Continue)", payload.isSubscribed(), is(false));
        returnResponse.countDown();
        assertResponse(INTERNAL_SERVER_ERROR, "");
        // send a follow-up request on the same connection:
        connection.request(connection.get("/")).subscribe(responses::add);
        assertResponse(OK, "");
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) HttpServerContext(io.servicetalk.http.api.HttpServerContext) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with StreamingHttpConnection

use of io.servicetalk.http.api.StreamingHttpConnection in project servicetalk by apple.

the class ExpectContinueTest method expectContinueThenFailure.

@ParameterizedTest(name = "protocol={0} withCL={1}")
@MethodSource("arguments")
void expectContinueThenFailure(HttpProtocol protocol, boolean withCL) throws Exception {
    try (HttpServerContext serverContext = startServer(protocol, (ctx, request, response) -> {
        requestReceived.countDown();
        sendContinue.await();
        StringBuilder sb = new StringBuilder();
        request.payloadBody().forEach(chunk -> sb.append(chunk.toString(US_ASCII)));
        returnResponse.await();
        try (HttpPayloadWriter<Buffer> writer = response.status(UNPROCESSABLE_ENTITY).sendMetaData()) {
            writer.write(ctx.executionContext().bufferAllocator().fromAscii(sb));
        }
    });
        StreamingHttpClient client = createClient(serverContext, protocol);
        StreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get()) {
        BufferAllocator allocator = connection.executionContext().bufferAllocator();
        TestPublisher<Buffer> payload = new TestPublisher.Builder<Buffer>().singleSubscriber().build();
        connection.request(newRequest(connection, withCL, false, payload)).subscribe(responses::add);
        requestReceived.await();
        assertThat("Unexpected subscribe to payload body before 100 (Continue)", payload.isSubscribed(), is(false));
        sendContinue.countDown();
        sendRequestPayload(payload, allocator);
        returnResponse.countDown();
        assertResponse(UNPROCESSABLE_ENTITY, PAYLOAD + PAYLOAD);
        sendFollowUpRequest(connection, withCL, allocator, UNPROCESSABLE_ENTITY);
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) HttpServerContext(io.servicetalk.http.api.HttpServerContext) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) BufferAllocator(io.servicetalk.buffer.api.BufferAllocator) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 13 with StreamingHttpConnection

use of io.servicetalk.http.api.StreamingHttpConnection in project servicetalk by apple.

the class FlushStrategyForClientApiTest method streamingApiShouldFlushOnEach.

@Test
void streamingApiShouldFlushOnEach() throws Exception {
    final StreamingHttpConnection connection = streamingHttpConnection();
    final SingleSource.Processor<Buffer, Buffer> payloadItemProcessor = Processors.newSingleProcessor();
    final Publisher<Buffer> payload = fromSource(payloadItemProcessor).toPublisher();
    final Single<StreamingHttpResponse> responseSingle = connection.request(connection.newRequest(POST, "/").payloadBody(payload));
    // Subscribe, to initiate the request, but we don't care about the response.
    responseSingle.toFuture();
    // Wait for the server to receive the response, meaning the client wrote and flushed.
    requestLatch.await();
    MatcherAssert.assertThat(payloadBuffersReceived.size(), is(0));
    final Buffer payloadItem = BufferAllocators.DEFAULT_ALLOCATOR.fromAscii("Hello");
    payloadItemProcessor.onSuccess(payloadItem);
    // Wait for the server to receive the payload
    Buffer receivedBuffer = payloadBuffersReceived.take();
    MatcherAssert.assertThat(receivedBuffer, is(payloadItem));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) SingleSource(io.servicetalk.concurrent.SingleSource) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 14 with StreamingHttpConnection

use of io.servicetalk.http.api.StreamingHttpConnection in project servicetalk by apple.

the class FlushStrategyForClientApiTest method aggregatedApiShouldNotOverrideExplicit.

@Test
void aggregatedApiShouldNotOverrideExplicit() throws Exception {
    final StreamingHttpConnection connection = streamingHttpConnection();
    ((NettyConnectionContext) connection.connectionContext()).updateFlushStrategy((prev, isOriginal) -> FlushStrategies.flushOnEach());
    final Single<StreamingHttpResponse> responseSingle = connection.request(connection.asConnection().newRequest(POST, "/").addHeader(TRANSFER_ENCODING, CHUNKED).toStreamingRequest().payloadBody(Publisher.never()));
    // Subscribe, to initiate the request, but we don't care about the response.
    responseSingle.toFuture();
    // Wait for the server to receive the response, meaning the client wrote and flushed.
    requestLatch.await();
}
Also used : NettyConnectionContext(io.servicetalk.transport.netty.internal.NettyConnectionContext) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 15 with StreamingHttpConnection

use of io.servicetalk.http.api.StreamingHttpConnection in project servicetalk by apple.

the class FullDuplexAndSequentialModeTest method deferResponseUntilAfterRequestSent.

@Test
void deferResponseUntilAfterRequestSent() throws Exception {
    clientFilterFactory(EnforceSequentialModeRequesterFilter.INSTANCE);
    setUp(CACHED, CACHED_SERVER);
    StreamingHttpConnection connection = streamingHttpConnection();
    CountDownLatch continueRequest = new CountDownLatch(1);
    try (InputStream payload = payload()) {
        Future<StreamingHttpResponse> responseFuture = stallingSendRequest(connection, continueRequest, payload);
        // Delay completion of the request payload body:
        Thread.sleep(100);
        // response meta-data completes only after request is sent
        assertThat(responseFuture.isDone(), is(false));
        continueRequest.countDown();
        assertResponse(responseFuture.get(), HTTP_1_1, OK, SIZE);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Publisher.fromInputStream(io.servicetalk.concurrent.api.Publisher.fromInputStream) InputStream(java.io.InputStream) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) CountDownLatch(java.util.concurrent.CountDownLatch) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

StreamingHttpConnection (io.servicetalk.http.api.StreamingHttpConnection)29 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)13 Buffer (io.servicetalk.buffer.api.Buffer)12 Test (org.junit.jupiter.api.Test)12 SingleAddressHttpClientBuilder (io.servicetalk.http.api.SingleAddressHttpClientBuilder)10 HttpServerContext (io.servicetalk.http.api.HttpServerContext)9 RedirectConfigBuilder (io.servicetalk.http.api.RedirectConfigBuilder)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 ExecutionException (java.util.concurrent.ExecutionException)6 BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)5 EnumSource (org.junit.jupiter.params.provider.EnumSource)5 ServerContext (io.servicetalk.transport.api.ServerContext)4 Single (io.servicetalk.concurrent.api.Single)3 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CharSequences.newAsciiString (io.servicetalk.buffer.api.CharSequences.newAsciiString)2 Publisher.fromInputStream (io.servicetalk.concurrent.api.Publisher.fromInputStream)2 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)2