Search in sources :

Example 26 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class TracingHttpRequesterFilterTest method tracerThrowsReturnsErrorResponse.

@Test
void tracerThrowsReturnsErrorResponse() throws Exception {
    when(mockTracer.buildSpan(any())).thenThrow(DELIBERATE_EXCEPTION);
    try (ServerContext context = buildServer()) {
        try (HttpClient client = forSingleAddress(serverHostAndPort(context)).appendConnectionFilter(new TracingHttpRequesterFilter(mockTracer, "testClient")).build()) {
            HttpRequest request = client.get("/");
            ExecutionException ex = assertThrows(ExecutionException.class, () -> client.request(request).toFuture().get());
            assertThat(ex.getCause(), is(DELIBERATE_EXCEPTION));
        }
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ServerContext(io.servicetalk.transport.api.ServerContext) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) HttpClient(io.servicetalk.http.api.HttpClient) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 27 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class TracingHttpServiceFilterTest method testRequestWithTraceKeyWithNegativeSampledAndAlwaysTrueSampler.

@Test
void testRequestWithTraceKeyWithNegativeSampledAndAlwaysTrueSampler() throws Exception {
    final CountingInMemorySpanEventListener spanListener = new CountingInMemorySpanEventListener();
    try (ServerContext context = buildServer(spanListener, (__, ___) -> true)) {
        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).set(SAMPLED, "0");
            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)

Example 28 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class HttpAuthConnectionFactoryClientTest method simulateAuth.

@Test
void simulateAuth() throws Exception {
    serverContext = forAddress(localAddress(0)).ioExecutor(CTX.ioExecutor()).executionStrategy(offloadNone()).listenStreamingAndAwait((ctx, request, factory) -> succeeded(newTestResponse(factory)));
    client = forSingleAddress(serverHostAndPort(serverContext)).appendConnectionFactoryFilter(TestHttpAuthConnectionFactory::new).ioExecutor(CTX.ioExecutor()).executionStrategy(offloadNone()).buildStreaming();
    StreamingHttpResponse response = client.request(newTestRequest(client, "/foo")).toFuture().get();
    assertEquals(OK, response.status());
}
Also used : StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) HttpServers.forAddress(io.servicetalk.http.netty.HttpServers.forAddress) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) ZERO(io.servicetalk.http.api.HttpHeaderValues.ZERO) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DelegatingConnectionFactory(io.servicetalk.client.api.DelegatingConnectionFactory) Nullable(javax.annotation.Nullable) StreamingHttpRequestFactory(io.servicetalk.http.api.StreamingHttpRequestFactory) ConnectionFactory(io.servicetalk.client.api.ConnectionFactory) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ExecutionContextExtension.immediate(io.servicetalk.transport.netty.internal.ExecutionContextExtension.immediate) ServerContext(io.servicetalk.transport.api.ServerContext) Single(io.servicetalk.concurrent.api.Single) HttpClients.forSingleAddress(io.servicetalk.http.netty.HttpClients.forSingleAddress) ExecutionContextExtension(io.servicetalk.transport.netty.internal.ExecutionContextExtension) CONTENT_LENGTH(io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH) OK(io.servicetalk.http.api.HttpResponseStatus.OK) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) ContextMap(io.servicetalk.context.api.ContextMap) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) TransportObserver(io.servicetalk.transport.api.TransportObserver) Single.failed(io.servicetalk.concurrent.api.Single.failed) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 29 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class HttpConnectionContextProtocolTest method testProtocol.

@ParameterizedTest
@EnumSource(Config.class)
void testProtocol(Config config) throws Exception {
    try (ServerContext serverContext = startServer(config);
        BlockingHttpClient client = newClient(serverContext, config);
        BlockingHttpConnection connection = client.reserveConnection(client.get("/"))) {
        assertThat("Client-side connection protocol does not match expected value", connection.connectionContext().protocol(), equalTo(config.expectedProtocol));
        assertThat("Server-side connection protocol does not match expected value", connection.request(connection.get("/")).payloadBody(textSerializerUtf8()), equalTo(config.expectedProtocol.name()));
    }
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpConnection(io.servicetalk.http.api.BlockingHttpConnection) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class MutualSslTest method mutualSsl.

@ParameterizedTest
@MethodSource("params")
void mutualSsl(SslProvider serverSslProvider, SslProvider clientSslProvider, @SuppressWarnings("rawtypes") Map<SocketOption, Object> serverListenOptions, @SuppressWarnings("rawtypes") Map<SocketOption, Object> clientOptions) throws Exception {
    assumeTcpFastOpen(clientOptions);
    HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).trustManager(DefaultTestCerts::loadClientCAPem).clientAuthMode(REQUIRE).provider(serverSslProvider).build());
    for (@SuppressWarnings("rawtypes") Entry<SocketOption, Object> entry : serverListenOptions.entrySet()) {
        @SuppressWarnings("unchecked") SocketOption<Object> option = entry.getKey();
        serverBuilder.listenSocketOption(option, entry.getValue());
    }
    try (ServerContext serverContext = serverBuilder.listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
        BlockingHttpClient client = newClientBuilder(serverContext, clientOptions).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).provider(clientSslProvider).peerHost(serverPemHostname()).keyManager(DefaultTestCerts::loadClientPem, DefaultTestCerts::loadClientKey).build()).buildBlocking()) {
        assertEquals(HttpResponseStatus.OK, client.request(client.get("/")).status());
    }
}
Also used : SocketOption(java.net.SocketOption) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ServerContext (io.servicetalk.transport.api.ServerContext)106 Test (org.junit.jupiter.api.Test)57 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)42 HttpResponse (io.servicetalk.http.api.HttpResponse)39 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)34 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)34 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)33 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)28 InetSocketAddress (java.net.InetSocketAddress)27 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)26 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)24 MethodSource (org.junit.jupiter.params.provider.MethodSource)22 HostAndPort (io.servicetalk.transport.api.HostAndPort)20 Single (io.servicetalk.concurrent.api.Single)19 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)19 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)16 ClientSslConfigBuilder (io.servicetalk.transport.api.ClientSslConfigBuilder)15 OK (io.servicetalk.http.api.HttpResponseStatus.OK)14 DefaultTestCerts (io.servicetalk.test.resources.DefaultTestCerts)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14