Search in sources :

Example 11 with StreamingHttpRequest

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

the class ConnectionAcceptingNettyHttpServerTest method assertConnectionRequestSucceeds.

private void assertConnectionRequestSucceeds(final StreamingHttpRequest request) throws Exception {
    final StreamingHttpResponse response = awaitIndefinitely(streamingHttpClient().reserveConnection(request).flatMap(conn -> conn.request(request)));
    assert response != null;
    assertResponse(response, HTTP_1_1, OK, "");
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) TRACE(io.servicetalk.logging.api.LogLevel.TRACE) TimeoutException(java.util.concurrent.TimeoutException) PlatformDependent.normalizedOs(io.netty.util.internal.PlatformDependent.normalizedOs) BlockingTestUtils.awaitIndefinitely(io.servicetalk.concurrent.api.BlockingTestUtils.awaitIndefinitely) BlockingTestUtils.await(io.servicetalk.concurrent.api.BlockingTestUtils.await) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) SO_BACKLOG(io.servicetalk.transport.api.ServiceTalkSocketOptions.SO_BACKLOG) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) Single(io.servicetalk.concurrent.api.Single) SVC_ECHO(io.servicetalk.http.netty.TestServiceStreaming.SVC_ECHO) CONNECT_TIMEOUT(io.servicetalk.transport.api.ServiceTalkSocketOptions.CONNECT_TIMEOUT) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) OK(io.servicetalk.http.api.HttpResponseStatus.OK) InetSocketAddress(java.net.InetSocketAddress) GET(io.servicetalk.http.api.HttpRequestMethod.GET) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CACHED(io.servicetalk.http.netty.AbstractNettyHttpServerTest.ExecutorSupplier.CACHED) RetryingHttpRequesterFilter.disableAutoRetries(io.servicetalk.http.netty.RetryingHttpRequesterFilter.disableAutoRetries) LimitingConnectionFactoryFilter.withMax(io.servicetalk.client.api.LimitingConnectionFactoryFilter.withMax) ConnectTimeoutException(io.servicetalk.client.api.ConnectTimeoutException) TRUE(java.lang.Boolean.TRUE) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) SECONDS(java.util.concurrent.TimeUnit.SECONDS) HostAndPort(io.servicetalk.transport.api.HostAndPort) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 12 with StreamingHttpRequest

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

the class ConnectionAcceptingNettyHttpServerTest method testIdleTimeout.

@Test
void testIdleTimeout() throws Exception {
    setUp(CACHED, CACHED);
    final StreamingHttpRequest request = streamingHttpClient().newRequest(GET, SVC_ECHO);
    assertConnectionRequestSucceeds(request);
    serverContext().acceptConnections(false);
    // Netty will evaluate the auto-read on the next round, so the next connection will go through.
    assertConnectionRequestSucceeds(request);
    // Connection will establish but remain in the accept-queue
    // (i.e., NOT accepted by the server => occupying 1 backlog entry)
    assertConnectionRequestReceiveTimesOut(request);
    try (StreamingHttpClient client = newClientWithConnectTimeout()) {
        // Since we control the backlog size, this connection won't establish (i.e., NO syn-ack)
        // timeout operator can be used to kill it or socket connection-timeout
        final Single<StreamingHttpResponse> response = client.reserveConnection(request).flatMap(conn -> conn.request(request));
        final ExecutionException executionException = assertThrows(ExecutionException.class, () -> awaitIndefinitely(response));
        assertThat(executionException.getCause(), instanceOf(ConnectTimeoutException.class));
    }
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ExecutionException(java.util.concurrent.ExecutionException) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ConnectTimeoutException(io.servicetalk.client.api.ConnectTimeoutException) Test(org.junit.jupiter.api.Test)

Example 13 with StreamingHttpRequest

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

the class ConnectionAcceptingNettyHttpServerTest method testStopAcceptingAndResume.

@Test
void testStopAcceptingAndResume() throws Exception {
    setUp(CACHED, CACHED);
    final StreamingHttpRequest request = streamingHttpClient().newRequest(GET, SVC_ECHO);
    assertConnectionRequestSucceeds(request);
    serverContext().acceptConnections(false);
    // Netty will evaluate the auto-read on the next round, so the next connection will go through.
    assertConnectionRequestSucceeds(request);
    // This connection should get established but not accepted.
    assertConnectionRequestReceiveTimesOut(request);
    // Restoring auto-read will resume accepting.
    serverContext().acceptConnections(true);
    assertConnectionRequestSucceeds(request);
}
Also used : StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Test(org.junit.jupiter.api.Test)

Example 14 with StreamingHttpRequest

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

the class HttpLifecycleObserverTest method makeRequestAndAssertResponse.

private void makeRequestAndAssertResponse(String path, HttpProtocol protocol, HttpResponseStatus status, int contentLength) throws Exception {
    StreamingHttpClient client = streamingHttpClient();
    StreamingHttpRequest request = contentLength == 0 ? client.get(path) : client.post(path).payloadBody(Publisher.from(CONTENT.duplicate())).transform(// adds empty trailers
    new StatelessTrailersTransformer<>());
    StreamingHttpResponse response = client.request(request).toFuture().get();
    assertResponse(response, protocol.version, status, contentLength);
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) StatelessTrailersTransformer(io.servicetalk.http.api.StatelessTrailersTransformer) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 15 with StreamingHttpRequest

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

the class HttpLifecycleObserverTest method testClientCancelsRequestAfterResponse.

@ParameterizedTest(name = "{displayName} [{index}] protocol={0}")
@EnumSource(HttpProtocol.class)
void testClientCancelsRequestAfterResponse(HttpProtocol protocol) throws Exception {
    TestPublisher<Buffer> serverResponsePayload = new TestPublisher<>();
    serviceFilterFactory(service -> new StreamingHttpServiceFilter(service) {

        @Override
        public Single<StreamingHttpResponse> handle(HttpServiceContext ctx, StreamingHttpRequest request, StreamingHttpResponseFactory responseFactory) {
            return request.payloadBody().ignoreElements().concat(succeeded(responseFactory.ok().payloadBody(serverResponsePayload)));
        }
    });
    setUp(protocol);
    StreamingHttpConnection connection = streamingHttpConnection();
    StreamingHttpRequest request = connection.post("/").payloadBody(Publisher.from(CONTENT.duplicate())).transform(// adds empty trailers
    new StatelessTrailersTransformer<>());
    StreamingHttpResponse response = connection.request(request).toFuture().get();
    assertResponse(response, protocol.version, OK);
    Future<Collection<Buffer>> payload = response.payloadBody().toFuture();
    payload.cancel(true);
    if (protocol == HttpProtocol.HTTP_1) {
        // wait for cancellation to close the connection:
        connection.onClose().toFuture().get();
    }
    // try to write server content to trigger write failure and close the server-side connection:
    serverResponsePayload.onNext(CONTENT.duplicate());
    bothTerminate.await();
    clientInOrder.verify(clientLifecycleObserver).onNewExchange();
    clientInOrder.verify(clientExchangeObserver).onConnectionSelected(any(ConnectionInfo.class));
    clientInOrder.verify(clientExchangeObserver).onRequest(any(StreamingHttpRequest.class));
    clientInOrder.verify(clientExchangeObserver).onResponse(any(StreamingHttpResponse.class));
    clientInOrder.verify(clientResponseObserver).onResponseCancel();
    clientRequestInOrder.verify(clientRequestObserver).onRequestData(any(Buffer.class));
    clientRequestInOrder.verify(clientRequestObserver).onRequestTrailers(any(HttpHeaders.class));
    clientRequestInOrder.verify(clientRequestObserver).onRequestComplete();
    clientInOrder.verify(clientExchangeObserver).onExchangeFinally();
    verifyNoMoreInteractions(clientLifecycleObserver, clientExchangeObserver, clientRequestObserver, clientResponseObserver);
    serverInOrder.verify(serverLifecycleObserver).onNewExchange();
    serverInOrder.verify(serverExchangeObserver).onConnectionSelected(any(ConnectionInfo.class));
    serverInOrder.verify(serverExchangeObserver).onRequest(any(StreamingHttpRequest.class));
    serverInOrder.verify(serverExchangeObserver).onResponse(any(StreamingHttpResponse.class));
    verify(serverResponseObserver, atMostOnce()).onResponseData(any(Buffer.class));
    serverInOrder.verify(serverResponseObserver).onResponseCancel();
    serverRequestInOrder.verify(serverRequestObserver).onRequestData(any(Buffer.class));
    serverRequestInOrder.verify(serverRequestObserver).onRequestComplete();
    serverInOrder.verify(serverExchangeObserver).onExchangeFinally();
    verifyNoMoreInteractions(serverLifecycleObserver, serverExchangeObserver, serverRequestObserver, serverResponseObserver);
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) Single(io.servicetalk.concurrent.api.Single) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) Collection(java.util.Collection) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ConnectionInfo(io.servicetalk.transport.api.ConnectionInfo) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)167 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)83 Test (org.junit.jupiter.api.Test)78 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)65 MethodSource (org.junit.jupiter.params.provider.MethodSource)47 Single (io.servicetalk.concurrent.api.Single)34 Buffer (io.servicetalk.buffer.api.Buffer)33 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)31 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)24 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)22 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)22 Nullable (javax.annotation.Nullable)21 OK (io.servicetalk.http.api.HttpResponseStatus.OK)20 InetSocketAddress (java.net.InetSocketAddress)20 StreamingHttpServiceFilter (io.servicetalk.http.api.StreamingHttpServiceFilter)19 CountDownLatch (java.util.concurrent.CountDownLatch)19 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)17 HttpHeaders (io.servicetalk.http.api.HttpHeaders)17 ServerContext (io.servicetalk.transport.api.ServerContext)16 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)16