Search in sources :

Example 16 with HttpServerBuilder

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

the class NettyHttpServerConnectionDrainTest method server.

private static ServerContext server(boolean autoDrain, StreamingHttpService handler) throws Exception {
    HttpServerBuilder httpServerBuilder = HttpServers.forAddress(AddressUtils.localAddress(0));
    if (!autoDrain) {
        httpServerBuilder = httpServerBuilder.drainRequestPayloadBody(false);
    }
    ServerContext serverContext = httpServerBuilder.listenStreamingAndAwait(handler);
    return new ServerContext() {

        @Override
        public SocketAddress listenAddress() {
            return serverContext.listenAddress();
        }

        @Override
        public ExecutionContext<?> executionContext() {
            return serverContext.executionContext();
        }

        @Override
        public void acceptConnections(final boolean accept) {
            serverContext.acceptConnections(accept);
        }

        @Override
        public Completable onClose() {
            return serverContext.onClose();
        }

        @Override
        public Completable closeAsync() {
            return serverContext.closeAsync();
        }

        @Override
        public void close() {
            // Without draining the request is expected to hang, don't wait too long unless on CI
            int timeoutSeconds = CI ? 15 : 1;
            awaitTermination(serverContext.closeAsyncGracefully().timeout(timeoutSeconds, SECONDS).onErrorResume(t -> serverContext.closeAsync().concat(Completable.failed(t))).toFuture());
        }
    };
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder)

Example 17 with HttpServerBuilder

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

the class ServiceTalkContentCodingTest method newServiceTalkServer.

private ServerContext newServiceTalkServer(final Scenario scenario, final Queue<Throwable> errors) throws Exception {
    HttpServerBuilder httpServerBuilder = HttpServers.forAddress(localAddress(0));
    StreamingHttpService service = (ctx, request, responseFactory) -> succeeded(buildResponse(responseFactory));
    StreamingHttpServiceFilterFactory filterFactory = REQ_FILTER.apply(scenario, errors);
    return httpServerBuilder.executionStrategy(defaultStrategy()).protocols(scenario.protocol.config).appendServiceFilter(new ContentCodingHttpServiceFilter(scenario.serverSupported, scenario.serverSupported)).appendServiceFilter(filterFactory).listenStreamingAndAwait(service);
}
Also used : TestUtils.assertNoAsyncErrors(io.servicetalk.test.resources.TestUtils.assertNoAsyncErrors) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) ContentCodec(io.servicetalk.encoding.api.ContentCodec) BiFunction(java.util.function.BiFunction) Collections.disjoint(java.util.Collections.disjoint) StreamingHttpServiceFilterFactory(io.servicetalk.http.api.StreamingHttpServiceFilterFactory) Collections.singletonList(java.util.Collections.singletonList) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) MethodSource(org.junit.jupiter.params.provider.MethodSource) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HeaderUtils.encodingFor(io.servicetalk.encoding.api.internal.HeaderUtils.encodingFor) ContentCodingHttpServiceFilter(io.servicetalk.http.api.ContentCodingHttpServiceFilter) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) List(java.util.List) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpSerializationProviders.textDeserializer(io.servicetalk.http.api.HttpSerializationProviders.textDeserializer) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Queue(java.util.Queue) Arrays.stream(java.util.Arrays.stream) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Identity.identity(io.servicetalk.encoding.api.Identity.identity) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) CharSequences.contentEquals(io.servicetalk.buffer.api.CharSequences.contentEquals) HttpSerializationProviders.textSerializer(io.servicetalk.http.api.HttpSerializationProviders.textSerializer) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) UNSUPPORTED_MEDIA_TYPE(io.servicetalk.http.api.HttpResponseStatus.UNSUPPORTED_MEDIA_TYPE) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpSerializers.appSerializerUtf8FixLen(io.servicetalk.http.api.HttpSerializers.appSerializerUtf8FixLen) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) 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) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) ContentCodingHttpRequesterFilter(io.servicetalk.http.api.ContentCodingHttpRequesterFilter) INTERNAL_SERVER_ERROR(io.servicetalk.http.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Single(io.servicetalk.concurrent.api.Single) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) ACCEPT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.ACCEPT_ENCODING) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Collectors.toList(java.util.stream.Collectors.toList) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) Single.failed(io.servicetalk.concurrent.api.Single.failed) StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) CONTENT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.CONTENT_ENCODING) HostAndPort(io.servicetalk.transport.api.HostAndPort) ContentCodingHttpServiceFilter(io.servicetalk.http.api.ContentCodingHttpServiceFilter) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) StreamingHttpServiceFilterFactory(io.servicetalk.http.api.StreamingHttpServiceFilterFactory)

Example 18 with HttpServerBuilder

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

the class AbstractJerseyStreamingHttpServiceTest method setUp.

protected void setUp(final RouterApi api) throws Exception {
    this.api = api;
    HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0));
    HttpJerseyRouterBuilder routerBuilder = new HttpJerseyRouterBuilder();
    configureBuilders(serverBuilder, routerBuilder);
    DefaultJerseyStreamingHttpRouter router = routerBuilder.from(application());
    final Configuration config = router.configuration();
    streamingJsonEnabled = getValue(config.getProperties(), config.getRuntimeType(), JSON_FEATURE, "", String.class).toLowerCase().contains("servicetalk");
    HttpServerBuilder httpServerBuilder = serverBuilder.ioExecutor(serverCtx.ioExecutor()).bufferAllocator(serverCtx.bufferAllocator());
    switch(api) {
        case ASYNC_AGGREGATED:
            serverContext = buildRouter(httpServerBuilder, toAggregated(router));
            break;
        case ASYNC_STREAMING:
            serverContext = buildRouter(httpServerBuilder, router);
            break;
        case BLOCKING_AGGREGATED:
            serverContext = buildRouter(httpServerBuilder, toBlocking(router));
            break;
        case BLOCKING_STREAMING:
            serverContext = buildRouter(httpServerBuilder, toBlockingStreaming(router));
            break;
        default:
            throw new IllegalArgumentException(api.name());
    }
    final HostAndPort hostAndPort = serverHostAndPort(serverContext);
    httpClient = HttpClients.forSingleAddress(hostAndPort).buildStreaming();
    hostHeader = hostHeader(hostAndPort);
}
Also used : AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) Configuration(javax.ws.rs.core.Configuration) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) TestUtils.getContentAsString(io.servicetalk.http.router.jersey.TestUtils.getContentAsString) CharSequences.newAsciiString(io.servicetalk.buffer.api.CharSequences.newAsciiString)

Example 19 with HttpServerBuilder

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

the class TcpFastOpenTest method requestSucceedsEvenIfTcpFastOpenNotEnabledOrSupported.

@ParameterizedTest(name = "{displayName} [{index}] protocols={0}, secure={1}, serverListenOptions={2}, clientOptions={3}")
@MethodSource("sslProviders")
void requestSucceedsEvenIfTcpFastOpenNotEnabledOrSupported(final Collection<HttpProtocol> protocols, final boolean secure, @SuppressWarnings("rawtypes") final Map<SocketOption, Object> serverListenOptions, @SuppressWarnings("rawtypes") final Map<SocketOption, Object> clientOptions) throws Exception {
    assumeTcpFastOpen(clientOptions);
    HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0)).protocols(toConfigs(protocols));
    if (secure) {
        serverBuilder.sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).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 = newClient(serverContext, protocols, secure, clientOptions)) {
        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) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

HttpServerBuilder (io.servicetalk.http.api.HttpServerBuilder)19 HostAndPort (io.servicetalk.transport.api.HostAndPort)9 ServerContext (io.servicetalk.transport.api.ServerContext)9 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)9 Test (org.junit.jupiter.api.Test)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)6 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)6 InetSocketAddress (java.net.InetSocketAddress)6 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)6 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)5 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)5 Single (io.servicetalk.concurrent.api.Single)4 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)4 DefaultTestCerts (io.servicetalk.test.resources.DefaultTestCerts)4 ServerSslConfigBuilder (io.servicetalk.transport.api.ServerSslConfigBuilder)4 Buffer (io.servicetalk.buffer.api.Buffer)3 AfterEach (org.junit.jupiter.api.AfterEach)3 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)3