Search in sources :

Example 91 with HttpResponse

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

the class HttpProxyTest method testBuilderReuseEachClientUsesOwnProxy.

@Test
void testBuilderReuseEachClientUsesOwnProxy() throws Exception {
    final SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> builder = HttpClients.forSingleAddress(serverAddress);
    final BlockingHttpClient client = builder.proxyAddress(proxyAddress).buildBlocking();
    final HttpClient otherProxyClient = HttpClients.forMultiAddressUrl().build();
    final AtomicInteger otherProxyRequestCount = new AtomicInteger();
    try (ServerContext otherProxyContext = HttpServers.forAddress(localAddress(0)).listenAndAwait((ctx, request, responseFactory) -> {
        otherProxyRequestCount.incrementAndGet();
        return otherProxyClient.request(request);
    });
        BlockingHttpClient otherClient = builder.proxyAddress(serverHostAndPort(otherProxyContext)).appendConnectionFactoryFilter(new TargetAddressCheckConnectionFactoryFilter(targetAddress, false)).buildBlocking()) {
        final HttpResponse httpResponse = otherClient.request(client.get("/path"));
        assertThat(httpResponse.status(), is(OK));
        assertThat(otherProxyRequestCount.get(), is(1));
        assertThat(httpResponse.payloadBody().toString(US_ASCII), is("host: " + serverAddress));
    }
    final HttpResponse httpResponse = client.request(client.get("/path"));
    assertThat(httpResponse.status(), is(OK));
    assertThat(proxyRequestCount.get(), is(1));
    assertThat(httpResponse.payloadBody().toString(US_ASCII), is("host: " + serverAddress));
    assertThat(targetAddress.get(), is(equalTo(serverAddress.toString())));
}
Also used : AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TargetAddressCheckConnectionFactoryFilter(io.servicetalk.http.netty.HttpsProxyTest.TargetAddressCheckConnectionFactoryFilter) InetSocketAddress(java.net.InetSocketAddress) HttpClient(io.servicetalk.http.api.HttpClient) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 92 with HttpResponse

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

the class HttpRawDataSerializationTest method runTest.

private static <T> void runTest(T[] content, String contentType, int expectedContentLength, HttpStreamingSerializer<T> streamingSerializer) throws Exception {
    try (ServerContext srv = HttpServers.forAddress(localAddress(0)).listenStreamingAndAwait((ctx, request, responseFactory) -> succeeded(responseFactory.ok().payloadBody(from(content), streamingSerializer)));
        BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
        HttpResponse resp = clt.request(clt.get("/hello"));
        assertThat(Objects.toString(resp.headers().get(CONTENT_TYPE)), is(contentType));
        assertThat(resp.payloadBody().readableBytes(), is(expectedContentLength));
    }
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse)

Example 93 with HttpResponse

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

the class HttpServerFilterOrderTest method nonOffloadOrder.

@Test
void nonOffloadOrder() throws Exception {
    StreamingHttpService filter1 = newMockService();
    StreamingHttpService filter2 = newMockService();
    ServerContext serverContext = HttpServers.forAddress(localAddress(0)).appendNonOffloadingServiceFilter(addFilter(filter1)).appendNonOffloadingServiceFilter(addFilter(filter2)).listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
    BlockingHttpClient client = forSingleAddress(serverHostAndPort(serverContext)).buildBlocking();
    HttpResponse resp = client.request(client.get("/"));
    assertThat("Unexpected response.", resp.status(), is(OK));
    InOrder verifier = inOrder(filter1, filter2);
    verifier.verify(filter1).handle(any(), any(), any());
    verifier.verify(filter2).handle(any(), any(), any());
}
Also used : InOrder(org.mockito.InOrder) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 94 with HttpResponse

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

the class HttpServerFilterOrderTest method mixedFiltersOrder.

@Test
void mixedFiltersOrder() throws Exception {
    StreamingHttpService filter1 = newMockService();
    StreamingHttpService filter2 = newMockService();
    ServerContext serverContext = HttpServers.forAddress(localAddress(0)).appendServiceFilter(addFilter(filter2)).appendNonOffloadingServiceFilter(addFilter(filter1)).listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
    BlockingHttpClient client = forSingleAddress(serverHostAndPort(serverContext)).buildBlocking();
    HttpResponse resp = client.request(client.get("/"));
    assertThat("Unexpected response.", resp.status(), is(OK));
    InOrder verifier = inOrder(filter1, filter2);
    verifier.verify(filter1).handle(any(), any(), any());
    verifier.verify(filter2).handle(any(), any(), any());
}
Also used : InOrder(org.mockito.InOrder) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 95 with HttpResponse

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

the class HttpServerFilterOrderTest method prependOrder.

@Test
void prependOrder() throws Exception {
    StreamingHttpService filter1 = newMockService();
    StreamingHttpService filter2 = newMockService();
    ServerContext serverContext = HttpServers.forAddress(localAddress(0)).appendServiceFilter(addFilter(filter1)).appendServiceFilter(addFilter(filter2)).listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
    BlockingHttpClient client = forSingleAddress(serverHostAndPort(serverContext)).buildBlocking();
    HttpResponse resp = client.request(client.get("/"));
    assertThat("Unexpected response.", resp.status(), is(OK));
    InOrder verifier = inOrder(filter1, filter2);
    verifier.verify(filter1).handle(any(), any(), any());
    verifier.verify(filter2).handle(any(), any(), any());
}
Also used : InOrder(org.mockito.InOrder) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (io.servicetalk.http.api.HttpResponse)107 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)65 Test (org.junit.jupiter.api.Test)46 ServerContext (io.servicetalk.transport.api.ServerContext)45 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)44 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)35 HttpRequest (io.servicetalk.http.api.HttpRequest)29 MethodSource (org.junit.jupiter.params.provider.MethodSource)29 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)26 HttpClient (io.servicetalk.http.api.HttpClient)23 InetSocketAddress (java.net.InetSocketAddress)22 OK (io.servicetalk.http.api.HttpResponseStatus.OK)21 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)21 Single (io.servicetalk.concurrent.api.Single)19 HttpSerializers.textSerializerUtf8 (io.servicetalk.http.api.HttpSerializers.textSerializerUtf8)19 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)19 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)19 Matchers.is (org.hamcrest.Matchers.is)18 HostAndPort (io.servicetalk.transport.api.HostAndPort)17 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)16