Search in sources :

Example 26 with StreamingHttpClient

use of io.servicetalk.http.api.StreamingHttpClient 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 27 with StreamingHttpClient

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

the class RedirectingHttpRequesterFilterTest method shouldRedirectThrows.

@Test
void shouldRedirectThrows() {
    when(httpClient.request(any())).thenReturn(redirectResponse(MOVED_PERMANENTLY), okResponse());
    StreamingHttpClient client = newClient(new RedirectConfigBuilder().redirectPredicate((relative, count, request, response) -> {
        throw DELIBERATE_EXCEPTION;
    }).build());
    ExecutionException e = assertThrows(ExecutionException.class, () -> client.request(newRequest(client, GET)).toFuture().get());
    assertThat(e.getCause(), sameInstance(DELIBERATE_EXCEPTION));
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with StreamingHttpClient

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

the class RedirectingHttpRequesterFilterTest method redirectForOnlyRelativeWithAbsoluteFormRelativeLocationWithPort.

@Test
void redirectForOnlyRelativeWithAbsoluteFormRelativeLocationWithPort() throws Exception {
    when(httpClient.request(any())).thenReturn(redirectResponse(MOVED_PERMANENTLY, "http://servicetalk.io:80"), okResponse());
    StreamingHttpClient client = newClient(new RedirectConfigBuilder().allowNonRelativeRedirects(false).build());
    verifyRedirected(client, newRequest(client, GET).setHeader(HOST, "servicetalk.io:80"), true, false);
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with StreamingHttpClient

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

the class RedirectingHttpRequesterFilterTest method multipleFollowUpRedirects.

@Test
void multipleFollowUpRedirects() throws Exception {
    when(httpClient.request(any())).thenReturn(redirectResponse(MOVED_PERMANENTLY, 1), redirectResponse(MOVED_PERMANENTLY, 2), redirectResponse(MOVED_PERMANENTLY, 3), okResponse());
    StreamingHttpClient client = newClient(new RedirectConfigBuilder().build());
    verifyResponse(client, newRequest(client, GET), OK, -1, 4, GET);
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with StreamingHttpClient

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

the class RedirectingHttpRequesterFilterTest method misConfigureRedirectOfHeadersAndMessageBodyForNonRelativeRedirects.

@Test
void misConfigureRedirectOfHeadersAndMessageBodyForNonRelativeRedirects() throws Exception {
    when(httpClient.request(any())).thenReturn(redirectResponse(MOVED_PERMANENTLY, "http://non-relative.servicetalk.io"), okResponse());
    StreamingHttpClient client = newClient(new RedirectConfigBuilder().allowNonRelativeRedirects(true).headersToRedirect("Unknown-header").redirectPayloadBody(false).trailersToRedirect("Unknown-trailer").build());
    verifyRedirected(client, newRequest(client, GET), false, true);
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)77 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)48 RedirectConfigBuilder (io.servicetalk.http.api.RedirectConfigBuilder)33 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)30 Test (org.junit.jupiter.api.Test)30 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)26 Buffer (io.servicetalk.buffer.api.Buffer)24 MethodSource (org.junit.jupiter.params.provider.MethodSource)22 StreamingHttpConnection (io.servicetalk.http.api.StreamingHttpConnection)19 InetSocketAddress (java.net.InetSocketAddress)16 HttpServerContext (io.servicetalk.http.api.HttpServerContext)15 SingleAddressHttpClientBuilder (io.servicetalk.http.api.SingleAddressHttpClientBuilder)15 ServerContext (io.servicetalk.transport.api.ServerContext)14 Single (io.servicetalk.concurrent.api.Single)12 BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)11 HostAndPort (io.servicetalk.transport.api.HostAndPort)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 ExecutionException (java.util.concurrent.ExecutionException)11 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)10 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)10