Search in sources :

Example 21 with BufferAllocator

use of io.servicetalk.buffer.api.BufferAllocator in project servicetalk by apple.

the class ExpectContinueTest method serverErrorAggregated.

@ParameterizedTest(name = "protocol={0} withCL={1}")
@MethodSource("arguments")
void serverErrorAggregated(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);
        HttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get().asConnection()) {
        BufferAllocator allocator = connection.executionContext().bufferAllocator();
        Future<HttpResponse> responseFuture = connection.request(newRequest(connection, withCL, false, PAYLOAD + PAYLOAD, allocator)).toFuture();
        requestReceived.await();
        returnResponse.countDown();
        HttpResponse response = responseFuture.get();
        assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
        assertThat(response.payloadBody().toString(US_ASCII), equalTo(""));
        // send a follow-up request on the same connection:
        response = connection.request(connection.get("/")).toFuture().get();
        assertThat(response.status(), is(OK));
        assertThat(response.payloadBody().toString(US_ASCII), equalTo(""));
    }
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) HttpConnection(io.servicetalk.http.api.HttpConnection) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) HttpServerContext(io.servicetalk.http.api.HttpServerContext) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) BufferAllocator(io.servicetalk.buffer.api.BufferAllocator) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 22 with BufferAllocator

use of io.servicetalk.buffer.api.BufferAllocator in project servicetalk by apple.

the class ExpectContinueTest method serverRespondsWithSuccess.

@ParameterizedTest(name = "protocol={0} withCL={1}")
@MethodSource("arguments")
void serverRespondsWithSuccess(HttpProtocol protocol, boolean withCL) throws Exception {
    try (HttpServerContext serverContext = startServer(protocol, (ctx, request, response) -> {
        requestReceived.countDown();
        returnResponse.await();
        response.status(ACCEPTED);
        try (HttpPayloadWriter<Buffer> writer = response.sendMetaData()) {
            for (Buffer chunk : request.payloadBody()) {
                writer.write(chunk);
            }
        }
    });
        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));
        returnResponse.countDown();
        StreamingHttpResponse response = responses.take();
        assertThat(response.status(), is(ACCEPTED));
        sendRequestPayload(payload, allocator);
        assertThat(response.toResponse().toFuture().get().payloadBody().toString(US_ASCII), equalTo(PAYLOAD + PAYLOAD));
        sendFollowUpRequest(connection, withCL, allocator, ACCEPTED);
    }
}
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) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) BufferAllocator(io.servicetalk.buffer.api.BufferAllocator) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 23 with BufferAllocator

use of io.servicetalk.buffer.api.BufferAllocator in project servicetalk by apple.

the class ContentCodingHttpRequesterFilter method codecTransformBidirectionalIfNeeded.

private Single<StreamingHttpResponse> codecTransformBidirectionalIfNeeded(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
    final BufferAllocator alloc = delegate.executionContext().bufferAllocator();
    setAcceptEncoding(request.headers(), acceptedEncodingsHeader);
    encodePayloadContentIfAvailable(request, alloc);
    return decodePayloadContentIfEncoded(delegate.request(request), alloc);
}
Also used : BufferAllocator(io.servicetalk.buffer.api.BufferAllocator)

Aggregations

BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)23 Buffer (io.servicetalk.buffer.api.Buffer)15 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)10 HttpServerContext (io.servicetalk.http.api.HttpServerContext)9 StreamingHttpConnection (io.servicetalk.http.api.StreamingHttpConnection)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 Consumes (javax.ws.rs.Consumes)7 Produces (javax.ws.rs.Produces)7 CompositeBuffer (io.servicetalk.buffer.api.CompositeBuffer)6 Publisher (io.servicetalk.concurrent.api.Publisher)6 SingleAddressHttpClientBuilder (io.servicetalk.http.api.SingleAddressHttpClientBuilder)6 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)6 RedirectConfigBuilder (io.servicetalk.http.api.RedirectConfigBuilder)5 Single (io.servicetalk.concurrent.api.Single)4 JacksonSerializationProvider (io.servicetalk.data.jackson.JacksonSerializationProvider)3 HttpConnection (io.servicetalk.http.api.HttpConnection)3 HttpResponse (io.servicetalk.http.api.HttpResponse)3 RequestProperties.setResponseBufferPublisher (io.servicetalk.http.router.jersey.internal.RequestProperties.setResponseBufferPublisher)3 DefaultSerializer (io.servicetalk.serialization.api.DefaultSerializer)3