Search in sources :

Example 16 with HttpRequest

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

the class MalformedDataAfterHttpMessageTest method afterResponse.

@Test
void afterResponse() throws Exception {
    ServerSocketChannel server = nettyServer(RESPONSE_MSG);
    try (BlockingHttpClient client = stClient(server.localAddress())) {
        HttpRequest request = client.get("/1");
        ReservedBlockingHttpConnection connection = client.reserveConnection(request);
        CountDownLatch connectionClosedLatch = new CountDownLatch(1);
        connection.connectionContext().onClose().whenFinally(connectionClosedLatch::countDown).subscribe();
        validateClientResponse(connection.request(request));
        // Verify that the next request fails and connection gets closed:
        // The exception generation is currently racy. A write maybe triggered while the channel is not active
        // which will lead to ClosedChannelException.
        assertThat(assertThrows(Exception.class, () -> connection.request(connection.get("/2"))), anyOf(instanceOf(DecoderException.class), instanceOf(ClosedChannelException.class)));
        connectionClosedLatch.await();
    } finally {
        server.close().sync();
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) ReservedBlockingHttpConnection(io.servicetalk.http.api.ReservedBlockingHttpConnection) DecoderException(io.netty.handler.codec.DecoderException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 17 with HttpRequest

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

the class ServerRespondsOnClosingTest method handleRequests.

private void handleRequests() {
    Exchange exchange;
    while ((exchange = requests.poll()) != null) {
        HttpRequest request = exchange.request;
        HttpResponse response = RESPONSE_FACTORY.ok().setHeader("Request-Path", request.path()).payloadBody(RESPONSE_PAYLOAD_BODY, textSerializerUtf8());
        if (request.hasQueryParameter("serverShouldClose")) {
            response.setHeader(CONNECTION, CLOSE);
        }
        exchange.responseProcessor.onSuccess(response);
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) HttpResponse(io.servicetalk.http.api.HttpResponse)

Example 18 with HttpRequest

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

the class TracingHttpServiceFilterTest method testRequestWithTraceKey.

@Test
void testRequestWithTraceKey() throws Exception {
    CountingInMemorySpanEventListener spanListener = new CountingInMemorySpanEventListener();
    try (ServerContext context = buildServer(spanListener)) {
        try (HttpClient client = forSingleAddress(serverHostAndPort(context)).build()) {
            String traceId = randomHexId();
            String spanId = randomHexId();
            String parentSpanId = randomHexId();
            String requestUrl = "/";
            HttpRequest request = client.get(requestUrl);
            request.headers().set(TRACE_ID, traceId).set(SPAN_ID, spanId).set(PARENT_SPAN_ID, parentSpanId).set(SAMPLED, "0");
            HttpResponse response = client.request(request).toFuture().get();
            TestSpanState serverSpanState = response.payloadBody(SPAN_STATE_SERIALIZER);
            assertSpan(spanListener, traceId, spanId, requestUrl, serverSpanState, false);
        }
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ServerContext(io.servicetalk.transport.api.ServerContext) HttpClient(io.servicetalk.http.api.HttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) CountingInMemorySpanEventListener(io.servicetalk.opentracing.http.TestUtils.CountingInMemorySpanEventListener) Test(org.junit.jupiter.api.Test)

Example 19 with HttpRequest

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

the class TracingHttpServiceFilterTest method tracerThrowsReturnsErrorResponse.

@Test
void tracerThrowsReturnsErrorResponse() throws Exception {
    when(mockTracer.buildSpan(any())).thenThrow(DELIBERATE_EXCEPTION);
    try (ServerContext context = HttpServers.forAddress(localAddress(0)).appendServiceFilter(new TracingHttpServiceFilter(mockTracer, "testServer")).listenStreamingAndAwait(((ctx, request, responseFactory) -> succeeded(responseFactory.forbidden())))) {
        try (HttpClient client = forSingleAddress(serverHostAndPort(context)).build()) {
            HttpRequest request = client.get("/");
            HttpResponse response = client.request(request).toFuture().get();
            assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
        }
    }
}
Also used : AsyncContextHttpFilterVerifier.verifyServerFilterAsyncContextVisibility(io.servicetalk.http.netty.AsyncContextHttpFilterVerifier.verifyServerFilterAsyncContextVisibility) BeforeEach(org.junit.jupiter.api.BeforeEach) TestUtils.randomHexId(io.servicetalk.opentracing.http.TestUtils.randomHexId) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) Matchers.not(org.hamcrest.Matchers.not) TimeoutException(java.util.concurrent.TimeoutException) StreamingHttpServiceFilterFactory(io.servicetalk.http.api.StreamingHttpServiceFilterFactory) CountingInMemorySpanEventListener(io.servicetalk.opentracing.http.TestUtils.CountingInMemorySpanEventListener) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) HttpRequest(io.servicetalk.http.api.HttpRequest) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) PublisherSource(io.servicetalk.concurrent.PublisherSource) HttpClients.forSingleAddress(io.servicetalk.http.netty.HttpClients.forSingleAddress) HttpResponse(io.servicetalk.http.api.HttpResponse) LoggerStringWriter(io.servicetalk.log4j2.mdc.utils.LoggerStringWriter) SamplingStrategies(io.servicetalk.opentracing.inmemory.SamplingStrategies) Test(org.junit.jupiter.api.Test) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) DefaultInMemoryTracer(io.servicetalk.opentracing.inmemory.DefaultInMemoryTracer) SAMPLED(io.servicetalk.opentracing.internal.ZipkinHeaderNames.SAMPLED) Matchers.is(org.hamcrest.Matchers.is) TRUE(java.lang.Boolean.TRUE) TRACING_TEST_LOG_LINE_PREFIX(io.servicetalk.opentracing.http.TestUtils.TRACING_TEST_LOG_LINE_PREFIX) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Mock(org.mockito.Mock) SPAN_STATE_SERIALIZER(io.servicetalk.opentracing.http.TestUtils.SPAN_STATE_SERIALIZER) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) InMemorySpan(io.servicetalk.opentracing.inmemory.api.InMemorySpan) PARENT_SPAN_ID(io.servicetalk.opentracing.internal.ZipkinHeaderNames.PARENT_SPAN_ID) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) TestUtils.verifyTraceIdPresentInLogs(io.servicetalk.opentracing.http.TestUtils.verifyTraceIdPresentInLogs) ERROR(io.opentracing.tag.Tags.ERROR) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) TRACE_ID(io.servicetalk.opentracing.internal.ZipkinHeaderNames.TRACE_ID) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) Nullable(javax.annotation.Nullable) INTERNAL_SERVER_ERROR(io.servicetalk.http.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) Logger(org.slf4j.Logger) ServerContext(io.servicetalk.transport.api.ServerContext) Tracer(io.opentracing.Tracer) Single(io.servicetalk.concurrent.api.Single) LoggerStringWriter.stableAccumulated(io.servicetalk.log4j2.mdc.utils.LoggerStringWriter.stableAccumulated) SCOPE_MANAGER(io.servicetalk.opentracing.asynccontext.AsyncContextInMemoryScopeManager.SCOPE_MANAGER) Mockito.when(org.mockito.Mockito.when) Matchers.equalToIgnoringCase(org.hamcrest.Matchers.equalToIgnoringCase) SPAN_ID(io.servicetalk.opentracing.internal.ZipkinHeaderNames.SPAN_ID) AfterEach(org.junit.jupiter.api.AfterEach) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) HttpServers(io.servicetalk.http.netty.HttpServers) HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ServerContext(io.servicetalk.transport.api.ServerContext) HttpClient(io.servicetalk.http.api.HttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 20 with HttpRequest

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

the class TracingHttpServiceFilterTest method testRequestWithTraceKeyWithoutSampled.

@Test
void testRequestWithTraceKeyWithoutSampled() throws Exception {
    CountingInMemorySpanEventListener spanListener = new CountingInMemorySpanEventListener();
    try (ServerContext context = buildServer(spanListener)) {
        try (HttpClient client = forSingleAddress(serverHostAndPort(context)).build()) {
            String traceId = randomHexId();
            String spanId = randomHexId();
            String requestUrl = "/";
            HttpRequest request = client.get(requestUrl);
            request.headers().set(TRACE_ID, traceId).set(SPAN_ID, spanId);
            HttpResponse response = client.request(request).toFuture().get();
            TestSpanState serverSpanState = response.payloadBody(SPAN_STATE_SERIALIZER);
            assertSpan(spanListener, traceId, spanId, requestUrl, serverSpanState, true);
        }
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ServerContext(io.servicetalk.transport.api.ServerContext) HttpClient(io.servicetalk.http.api.HttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) CountingInMemorySpanEventListener(io.servicetalk.opentracing.http.TestUtils.CountingInMemorySpanEventListener) Test(org.junit.jupiter.api.Test)

Aggregations

HttpRequest (io.servicetalk.http.api.HttpRequest)42 HttpResponse (io.servicetalk.http.api.HttpResponse)28 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)26 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 Test (org.junit.jupiter.api.Test)19 HttpClient (io.servicetalk.http.api.HttpClient)17 MethodSource (org.junit.jupiter.params.provider.MethodSource)16 ServerContext (io.servicetalk.transport.api.ServerContext)15 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)14 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)11 HostAndPort (io.servicetalk.transport.api.HostAndPort)10 InetSocketAddress (java.net.InetSocketAddress)10 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)10 OK (io.servicetalk.http.api.HttpResponseStatus.OK)9 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)9 Buffer (io.servicetalk.buffer.api.Buffer)8 Nullable (javax.annotation.Nullable)8 Matchers.emptyString (org.hamcrest.Matchers.emptyString)8 ReservedBlockingHttpConnection (io.servicetalk.http.api.ReservedBlockingHttpConnection)5