Search in sources :

Example 6 with HttpResponse

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

the class HttpProvidersTest method testSingleAddressHttpClientBuilderProviderForResolvedAddress.

@Test
void testSingleAddressHttpClientBuilderProviderForResolvedAddress() throws Exception {
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).listenStreamingAndAwait(new TestServiceStreaming())) {
        SocketAddress serverAddress = serverContext.listenAddress();
        TestSingleAddressHttpClientBuilderProvider.MODIFY_FOR_ADDRESS.set(serverAddress);
        try (BlockingHttpClient client = HttpClients.forResolvedAddress(serverAddress).buildBlocking()) {
            assertThat(TestSingleAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(1));
            HttpResponse response = client.request(client.get(SVC_ECHO));
            assertThat(response.status(), is(OK));
            assertThat(TestSingleAddressHttpClientBuilderProvider.CONNECTION_COUNTER.get(), is(1));
        }
    }
}
Also used : HttpServerContext(io.servicetalk.http.api.HttpServerContext) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test)

Example 7 with HttpResponse

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

the class HttpProvidersTest method testSingleAddressHttpClientBuilderProviderWithHostAndPort.

@Test
void testSingleAddressHttpClientBuilderProviderWithHostAndPort() throws Exception {
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).listenStreamingAndAwait(new TestServiceStreaming())) {
        HostAndPort serverAddress = serverHostAndPort(serverContext);
        TestSingleAddressHttpClientBuilderProvider.MODIFY_FOR_ADDRESS.set(serverAddress);
        try (BlockingHttpClient client = HttpClients.forSingleAddress(serverAddress).buildBlocking()) {
            assertThat(TestSingleAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(1));
            HttpResponse response = client.request(client.get(SVC_ECHO));
            assertThat(response.status(), is(OK));
            assertThat(TestSingleAddressHttpClientBuilderProvider.CONNECTION_COUNTER.get(), is(1));
        }
    }
}
Also used : AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpServerContext(io.servicetalk.http.api.HttpServerContext) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 8 with HttpResponse

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

the class HttpSerializationErrorTest method serializationMapThrowsPropagatesToClient.

@ParameterizedTest
@MethodSource("executors")
void serializationMapThrowsPropagatesToClient(HttpTestExecutionStrategy serverStrategy) throws Exception {
    serverExecutionStrategy = serverStrategy.executorSupplier.get();
    TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {
    };
    HttpSerializerDeserializer<Map<String, Object>> httpSerializer = jsonSerializer(JACKSON.serializerDeserializer(mapType));
    HttpStreamingSerializerDeserializer<Map<String, Object>> httpStreamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(mapType));
    try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenAndAwait((ctx, request, responseFactory) -> responseFactory.ok().toStreamingResponse().payloadBody(request.toStreamingRequest().payloadBody(httpStreamingSerializer).map(result -> {
        throw DELIBERATE_EXCEPTION;
    }), httpStreamingSerializer).toResponse());
        BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
        HttpResponse resp = clt.request(clt.post("/foo").payloadBody(emptyMap(), httpSerializer));
        assertEquals(INTERNAL_SERVER_ERROR, resp.status());
    }
}
Also used : BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) INTERNAL_SERVER_ERROR(io.servicetalk.http.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) Collections.emptyMap(java.util.Collections.emptyMap) HttpSerializers.jsonStreamingSerializer(io.servicetalk.http.api.HttpSerializers.jsonStreamingSerializer) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) JACKSON(io.servicetalk.data.jackson.JacksonSerializerFactory.JACKSON) DEFAULT(io.servicetalk.http.netty.HttpTestExecutionStrategy.DEFAULT) Collection(java.util.Collection) HttpResponse(io.servicetalk.http.api.HttpResponse) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) HttpStreamingSerializerDeserializer(io.servicetalk.http.api.HttpStreamingSerializerDeserializer) NO_OFFLOAD(io.servicetalk.http.netty.HttpTestExecutionStrategy.NO_OFFLOAD) HttpSerializers.jsonSerializer(io.servicetalk.http.api.HttpSerializers.jsonSerializer) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) HttpSerializerDeserializer(io.servicetalk.http.api.HttpSerializerDeserializer) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) MethodSource(org.junit.jupiter.params.provider.MethodSource) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Collections.emptyMap(java.util.Collections.emptyMap) Map(java.util.Map) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 9 with HttpResponse

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

the class DefaultMultiAddressUrlHttpClientBuilderTest method internalClientsInheritExecutionContext.

@Test
void internalClientsInheritExecutionContext() throws Exception {
    HttpExecutionContext expectedCtx = new HttpExecutionContextBuilder().ioExecutor(CTX.ioExecutor()).executor(CTX.executor()).bufferAllocator(PREFER_DIRECT_ALLOCATOR).executionStrategy(offloadNone()).build();
    AtomicReference<HttpExecutionContext> actualInternalCtx = new AtomicReference<>();
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).executionStrategy(offloadNone()).listenStreamingAndAwait((ctx, request, responseFactory) -> succeeded(responseFactory.ok()));
        BlockingHttpClient blockingHttpClient = HttpClients.forMultiAddressUrl().initializer((scheme, address, builder) -> builder.appendClientFilter(client -> {
            actualInternalCtx.set(client.executionContext());
            return new StreamingHttpClientFilter(client) {
            };
        })).ioExecutor(expectedCtx.ioExecutor()).executor(expectedCtx.executor()).bufferAllocator(expectedCtx.bufferAllocator()).executionStrategy(expectedCtx.executionStrategy()).buildBlocking()) {
        // Check external client
        assertExecutionContext(expectedCtx, blockingHttpClient.executionContext());
        // Make a request to trigger the filter execution that extracts the execution context.
        HttpResponse response = blockingHttpClient.request(blockingHttpClient.get("http://" + serverHostAndPort(serverContext)));
        assertThat(response.status(), is(OK));
        // Check internal client
        assertExecutionContext(expectedCtx, actualInternalCtx.get());
    }
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) HttpHeaders(io.servicetalk.http.api.HttpHeaders) ExecutionContextExtension.cached(io.servicetalk.transport.netty.internal.ExecutionContextExtension.cached) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) ServiceDiscovererEvent(io.servicetalk.client.api.ServiceDiscovererEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpRequester(io.servicetalk.http.api.HttpRequester) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) HttpRequest(io.servicetalk.http.api.HttpRequest) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) PREFER_HEAP_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.PREFER_HEAP_ALLOCATOR) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) Matchers.contentEqualTo(io.servicetalk.buffer.api.Matchers.contentEqualTo) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ExecutionContextExtension.immediate(io.servicetalk.transport.netty.internal.ExecutionContextExtension.immediate) ServerContext(io.servicetalk.transport.api.ServerContext) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) HttpResponse(io.servicetalk.http.api.HttpResponse) ServiceDiscoverer(io.servicetalk.client.api.ServiceDiscoverer) ExecutionContextExtension(io.servicetalk.transport.netty.internal.ExecutionContextExtension) BlockingHttpRequester(io.servicetalk.http.api.BlockingHttpRequester) BlockingStreamingHttpRequester(io.servicetalk.http.api.BlockingStreamingHttpRequester) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) OK(io.servicetalk.http.api.HttpResponseStatus.OK) InetSocketAddress(java.net.InetSocketAddress) Mockito.verify(org.mockito.Mockito.verify) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) Test(org.junit.jupiter.api.Test) PREFER_DIRECT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.PREFER_DIRECT_ALLOCATOR) Mockito.never(org.mockito.Mockito.never) HttpExecutionStrategies.offloadAll(io.servicetalk.http.api.HttpExecutionStrategies.offloadAll) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.is(org.hamcrest.Matchers.is) HttpHeadersFactory(io.servicetalk.http.api.HttpHeadersFactory) HostAndPort(io.servicetalk.transport.api.HostAndPort) Mockito.mock(org.mockito.Mockito.mock) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) HttpResponse(io.servicetalk.http.api.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 10 with HttpResponse

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

the class ExpectContinueTest method serverRespondsWithSuccessAggregated.

@ParameterizedTest(name = "protocol={0} withCL={1}")
@MethodSource("arguments")
void serverRespondsWithSuccessAggregated(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);
        HttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get().asConnection()) {
        BufferAllocator allocator = connection.executionContext().bufferAllocator();
        returnResponse.countDown();
        HttpResponse response = connection.request(newRequest(connection, withCL, false, PAYLOAD + PAYLOAD, allocator)).toFuture().get();
        assertThat(response.status(), is(ACCEPTED));
        assertThat(response.payloadBody().toString(US_ASCII), equalTo(PAYLOAD + PAYLOAD));
        sendFollowUpRequest(connection.asStreamingConnection(), withCL, allocator, ACCEPTED);
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) 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)

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