Search in sources :

Example 76 with StreamingHttpRequest

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

the class HttpServerMultipleRequestsTest method makeClientRequestWithId.

private static void makeClientRequestWithId(StreamingHttpConnection connection, String requestId) throws ExecutionException, InterruptedException {
    StreamingHttpRequest request = connection.get("/");
    request.headers().set(REQUEST_ID_HEADER, requestId);
    StreamingHttpResponse response = connection.request(request).toFuture().get();
    assertEquals(OK, response.status());
    assertTrue(request.headers().contains(REQUEST_ID_HEADER, requestId));
    response.messageBody().ignoreElements().subscribe();
}
Also used : StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 77 with StreamingHttpRequest

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

the class HttpStreamingClientOverrideOffloadingTest method reserveRespectsDisable.

@ParameterizedTest
@EnumSource(Params.class)
void reserveRespectsDisable(final Params params) throws Exception {
    setUp(params);
    StreamingHttpRequest request = client.get("/");
    request.context().put(HTTP_EXECUTION_STRATEGY_KEY, this.overridingStrategy);
    client.reserveConnection(request).beforeOnSuccess(__ -> {
        if (isInvalidThread()) {
            throw new AssertionError("Invalid thread found providing the connection. Thread: " + currentThread());
        }
    }).toFuture().get().closeAsync().toFuture().get();
}
Also used : StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) Thread.currentThread(java.lang.Thread.currentThread) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) NettyIoExecutors.createIoExecutor(io.servicetalk.transport.netty.NettyIoExecutors.createIoExecutor) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) Executor(io.servicetalk.concurrent.api.Executor) Matchers.hasSize(org.hamcrest.Matchers.hasSize) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Nullable(javax.annotation.Nullable) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Predicate(java.util.function.Predicate) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) InetSocketAddress(java.net.InetSocketAddress) HTTP_EXECUTION_STRATEGY_KEY(io.servicetalk.http.api.HttpContextKeys.HTTP_EXECUTION_STRATEGY_KEY) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) IoExecutor(io.servicetalk.transport.api.IoExecutor) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Executors.newCachedThreadExecutor(io.servicetalk.concurrent.api.Executors.newCachedThreadExecutor) HostAndPort(io.servicetalk.transport.api.HostAndPort) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 78 with StreamingHttpRequest

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

the class HttpTransportObserverTest method setUp.

private void setUp(HttpProtocol protocol) {
    this.protocol = protocol;
    protocol(protocol.config);
    connectionAcceptor(ctx -> {
        ctx.onClose().whenFinally(serverConnectionClosed::countDown).subscribe();
        return completed();
    });
    serviceFilterFactory(service -> new StreamingHttpServiceFilter(service) {

        @Override
        public Single<StreamingHttpResponse> handle(HttpServiceContext ctx, StreamingHttpRequest request, StreamingHttpResponseFactory responseFactory) {
            requestReceived.countDown();
            try {
                processRequest.await();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throwException(e);
            }
            return delegate().handle(ctx, request, responseFactory);
        }
    });
    clientTransportObserver = mock(TransportObserver.class, "clientTransportObserver");
    clientConnectionObserver = mock(ConnectionObserver.class, "clientConnectionObserver");
    clientDataObserver = mock(DataObserver.class, "clientDataObserver");
    clientMultiplexedObserver = mock(MultiplexedObserver.class, "clientMultiplexedObserver");
    clientStreamObserver = mock(StreamObserver.class, "clientStreamObserver");
    clientReadObserver = mock(ReadObserver.class, "clientReadObserver");
    clientWriteObserver = mock(WriteObserver.class, "clientWriteObserver");
    when(clientTransportObserver.onNewConnection(any(), any())).thenReturn(clientConnectionObserver);
    lenient().when(clientConnectionObserver.connectionEstablished(any(ConnectionInfo.class))).thenReturn(clientDataObserver);
    lenient().when(clientConnectionObserver.multiplexedConnectionEstablished(any(ConnectionInfo.class))).thenReturn(clientMultiplexedObserver);
    lenient().when(clientMultiplexedObserver.onNewStream()).thenReturn(clientStreamObserver);
    lenient().when(clientStreamObserver.streamEstablished()).thenReturn(clientDataObserver);
    lenient().when(clientDataObserver.onNewRead()).thenReturn(clientReadObserver);
    lenient().when(clientDataObserver.onNewWrite()).thenReturn(clientWriteObserver);
    serverTransportObserver = mock(TransportObserver.class, "serverTransportObserver");
    serverConnectionObserver = mock(ConnectionObserver.class, "serverConnectionObserver");
    serverDataObserver = mock(DataObserver.class, "serverDataObserver");
    serverMultiplexedObserver = mock(MultiplexedObserver.class, "serverMultiplexedObserver");
    serverStreamObserver = mock(StreamObserver.class, "serverStreamObserver");
    serverReadObserver = mock(ReadObserver.class, "serverReadObserver");
    serverWriteObserver = mock(WriteObserver.class, "serverWriteObserver");
    when(serverTransportObserver.onNewConnection(any(), any())).thenReturn(serverConnectionObserver);
    lenient().when(serverConnectionObserver.connectionEstablished(any(ConnectionInfo.class))).thenReturn(serverDataObserver);
    lenient().when(serverConnectionObserver.multiplexedConnectionEstablished(any(ConnectionInfo.class))).thenReturn(serverMultiplexedObserver);
    lenient().when(serverMultiplexedObserver.onNewStream()).thenReturn(serverStreamObserver);
    lenient().when(serverStreamObserver.streamEstablished()).thenReturn(serverDataObserver);
    lenient().when(serverDataObserver.onNewRead()).thenReturn(serverReadObserver);
    lenient().when(serverDataObserver.onNewWrite()).thenReturn(serverWriteObserver);
    transportObserver(clientTransportObserver, serverTransportObserver);
    setUp(CACHED, CACHED_SERVER);
}
Also used : StreamObserver(io.servicetalk.transport.api.ConnectionObserver.StreamObserver) MultiplexedObserver(io.servicetalk.transport.api.ConnectionObserver.MultiplexedObserver) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) TransportObserver(io.servicetalk.transport.api.TransportObserver) WriteObserver(io.servicetalk.transport.api.ConnectionObserver.WriteObserver) DataObserver(io.servicetalk.transport.api.ConnectionObserver.DataObserver) ConnectionObserver(io.servicetalk.transport.api.ConnectionObserver) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) Single(io.servicetalk.concurrent.api.Single) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ConnectionInfo(io.servicetalk.transport.api.ConnectionInfo) ReadObserver(io.servicetalk.transport.api.ConnectionObserver.ReadObserver)

Example 79 with StreamingHttpRequest

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

the class MalformedDataAfterHttpMessageTest method afterResponseNextClientRequestSucceeds.

@ParameterizedTest
@ValueSource(booleans = { true, false })
void afterResponseNextClientRequestSucceeds(boolean doOffloading) throws Exception {
    Queue<ConnectionContext> contextQueue = new LinkedBlockingQueue<>();
    ServerSocketChannel server = nettyServer(RESPONSE_MSG);
    try (BlockingHttpClient client = stClientBuilder(server.localAddress()).executionStrategy(doOffloading ? defaultStrategy() : offloadNone()).appendClientFilter(new RetryingHttpRequesterFilter.Builder().retryOther((req, cause) -> ofConstantBackoffFullJitter(ofNanos(1), MAX_VALUE)).build()).appendConnectionFilter(connection -> new StreamingHttpConnectionFilter(connection) {

        @Override
        public Single<StreamingHttpResponse> request(final StreamingHttpRequest request) {
            contextQueue.add(connectionContext());
            return super.request(request);
        }
    }).buildBlocking()) {
        validateClientResponse(client.request(client.get("/1")));
        validateClientResponse(client.request(client.get("/2")));
        ConnectionContext ctx1 = contextQueue.poll();
        assertThat(ctx1, not(nullValue()));
        // RetryingHttpRequesterFilter or AutoRetry may re-issue the request if a failure is seen locally. Verify
        // the last connection (used for second request) is different from the first.
        ConnectionContext ctx2 = null;
        ConnectionContext tmp;
        while ((tmp = contextQueue.poll()) != null) {
            ctx2 = tmp;
        }
        assertThat(ctx2, not(nullValue()));
        assertThat(ctx1, not(equalTo(ctx2)));
        assertThat(contextQueue, empty());
    } finally {
        server.close().sync();
    }
}
Also used : ReferenceCountUtil.release(io.netty.util.ReferenceCountUtil.release) SocketAddress(java.net.SocketAddress) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) MAX_VALUE(java.lang.Integer.MAX_VALUE) Matchers.not(org.hamcrest.Matchers.not) CONTENT_TYPE(io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) HttpRequest(io.servicetalk.http.api.HttpRequest) Matchers.nullValue(org.hamcrest.Matchers.nullValue) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) DecoderException(io.netty.handler.codec.DecoderException) ChannelInitializer(io.netty.channel.ChannelInitializer) HttpClients.forSingleAddress(io.servicetalk.http.netty.HttpClients.forSingleAddress) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpConnectionFilter(io.servicetalk.http.api.StreamingHttpConnectionFilter) CONTENT_LENGTH(io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) InetSocketAddress(java.net.InetSocketAddress) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.servicetalk.buffer.api.Buffer) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) Queue(java.util.Queue) Matchers.anyOf(org.hamcrest.Matchers.anyOf) BackOffPolicy.ofConstantBackoffFullJitter(io.servicetalk.http.netty.RetryingHttpRequesterFilter.BackOffPolicy.ofConstantBackoffFullJitter) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) TRACE(io.servicetalk.logging.api.LogLevel.TRACE) BuilderUtils.serverChannel(io.servicetalk.transport.netty.internal.BuilderUtils.serverChannel) ByteBufUtil.writeAscii(io.netty.buffer.ByteBufUtil.writeAscii) HttpServers.forAddress(io.servicetalk.http.netty.HttpServers.forAddress) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Duration.ofNanos(java.time.Duration.ofNanos) HostAndPort.of(io.servicetalk.transport.api.HostAndPort.of) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Matchers.contentEqualTo(io.servicetalk.buffer.api.Matchers.contentEqualTo) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ConnectionContext(io.servicetalk.transport.api.ConnectionContext) ValueSource(org.junit.jupiter.params.provider.ValueSource) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) Matchers.empty(org.hamcrest.Matchers.empty) EventLoopGroup(io.netty.channel.EventLoopGroup) ServerContext(io.servicetalk.transport.api.ServerContext) EventLoopAwareNettyIoExecutors.toEventLoopAwareNettyIoExecutor(io.servicetalk.transport.netty.internal.EventLoopAwareNettyIoExecutors.toEventLoopAwareNettyIoExecutor) ClosedChannelException(java.nio.channels.ClosedChannelException) Single(io.servicetalk.concurrent.api.Single) ExecutionContextExtension(io.servicetalk.transport.netty.internal.ExecutionContextExtension) IOException(java.io.IOException) ReservedBlockingHttpConnection(io.servicetalk.http.api.ReservedBlockingHttpConnection) OK(io.servicetalk.http.api.HttpResponseStatus.OK) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) Channel(io.netty.channel.Channel) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) String.valueOf(java.lang.String.valueOf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) TEXT_PLAIN(io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HostAndPort(io.servicetalk.transport.api.HostAndPort) StreamingHttpConnectionFilter(io.servicetalk.http.api.StreamingHttpConnectionFilter) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) ConnectionContext(io.servicetalk.transport.api.ConnectionContext) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 80 with StreamingHttpRequest

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

the class MultiAddressUrlHttpClientTest method requestWithRelativeFormRequestTarget.

@Test
void requestWithRelativeFormRequestTarget() {
    StreamingHttpRequest request = client.get("/200?param=value");
    // no host header
    toSource(client.request(request)).subscribe(subscriber);
    assertThat(subscriber.awaitOnError(), is(instanceOf(MalformedURLException.class)));
}
Also used : StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Test(org.junit.jupiter.api.Test)

Aggregations

StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)169 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)84 Test (org.junit.jupiter.api.Test)82 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)66 MethodSource (org.junit.jupiter.params.provider.MethodSource)47 Single (io.servicetalk.concurrent.api.Single)35 Buffer (io.servicetalk.buffer.api.Buffer)33 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)32 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)25 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)23 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)23 Nullable (javax.annotation.Nullable)22 InetSocketAddress (java.net.InetSocketAddress)21 OK (io.servicetalk.http.api.HttpResponseStatus.OK)20 StreamingHttpServiceFilter (io.servicetalk.http.api.StreamingHttpServiceFilter)19 CountDownLatch (java.util.concurrent.CountDownLatch)19 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)18 HttpHeaders (io.servicetalk.http.api.HttpHeaders)17 ServerContext (io.servicetalk.transport.api.ServerContext)17 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)17