Search in sources :

Example 26 with StreamingHttpConnection

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

the class FullDuplexAndSequentialModeTest method defaultFullDuplex.

@Test
void defaultFullDuplex() throws Exception {
    setUp(CACHED, CACHED_SERVER);
    StreamingHttpConnection connection = streamingHttpConnection();
    CountDownLatch continueRequest = new CountDownLatch(1);
    StreamingHttpResponse response;
    try (InputStream payload = payload()) {
        response = stallingSendRequest(connection, continueRequest, payload).get();
        // response meta-data received before request completes
        assertResponse(response, HTTP_1_1, OK);
    }
    continueRequest.countDown();
    ExecutionException e = assertThrows(ExecutionException.class, () -> response.payloadBody().toFuture().get());
    assertThat(e.getCause(), instanceOf(IOException.class));
    assertThat(e.getCause().getMessage(), containsString("Stream closed"));
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Publisher.fromInputStream(io.servicetalk.concurrent.api.Publisher.fromInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 27 with StreamingHttpConnection

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

the class H2ResponseCancelTest method testServerClosesStreamNotConnection.

@Test
void testServerClosesStreamNotConnection() throws Exception {
    StreamingHttpConnection connection = streamingHttpConnection();
    AtomicBoolean connectionClosed = new AtomicBoolean();
    connection.onClose().whenFinally(() -> connectionClosed.set(true)).subscribe();
    ExecutionException e = assertThrows(ExecutionException.class, () -> makeRequest(newRequest(connection, "close")));
    assertThat(e.getCause(), instanceOf(H2StreamResetException.class));
    // Mak sure we can use the same connection for future requests:
    assertSerializedResponse(makeRequest(newRequest(connection, "ok")), HTTP_2_0, OK, "ok");
    assertThat("Connection closed unexpectedly", connectionClosed.get(), is(false));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) H2StreamResetException(io.servicetalk.http.netty.NettyHttp2ExceptionUtils.H2StreamResetException) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) ReservedStreamingHttpConnection(io.servicetalk.http.api.ReservedStreamingHttpConnection) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 28 with StreamingHttpConnection

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

the class HttpTransportObserverTest method connectionEstablished.

@ParameterizedTest(name = "protocol={0}")
@EnumSource(HttpProtocol.class)
void connectionEstablished(HttpProtocol httpProtocol) throws Exception {
    setUp(httpProtocol);
    processRequest.countDown();
    StreamingHttpConnection connection = streamingHttpConnection();
    verify(clientTransportObserver).onNewConnection(any(), any());
    verify(serverTransportObserver, await()).onNewConnection(any(), any());
    verify(clientConnectionObserver).onTransportHandshakeComplete();
    verify(serverConnectionObserver, await()).onTransportHandshakeComplete();
    if (protocol == HTTP_1) {
        verify(clientConnectionObserver).connectionEstablished(any(ConnectionInfo.class));
        verify(serverConnectionObserver, await()).connectionEstablished(any(ConnectionInfo.class));
        verify(serverDataObserver, await()).onNewRead();
        verify(serverDataObserver, await()).onNewWrite();
    } else {
        verify(clientConnectionObserver).multiplexedConnectionEstablished(any(ConnectionInfo.class));
        verify(serverConnectionObserver, await()).multiplexedConnectionEstablished(any(ConnectionInfo.class));
    }
    connection.closeGracefully();
    assertConnectionClosed();
    verify(clientConnectionObserver).connectionClosed();
    verify(serverConnectionObserver, await()).connectionClosed();
    verifyNoMoreInteractions(clientTransportObserver, clientDataObserver, clientMultiplexedObserver, serverTransportObserver, serverDataObserver, serverMultiplexedObserver);
    if (protocol != HTTP_2) {
        // HTTP/2 coded adds additional write/flush events related to connection preface. Also, it may emit more
        // flush events on the pipeline after the connection is closed.
        verifyNoMoreInteractions(clientConnectionObserver, serverConnectionObserver);
    }
}
Also used : ConnectionInfo(io.servicetalk.transport.api.ConnectionInfo) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with StreamingHttpConnection

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

the class IllegalHeaderValueTest method noHeaderValueValidation.

@ParameterizedTest(name = "{index}: protocol={0}")
@EnumSource(HttpProtocol.class)
void noHeaderValueValidation(HttpProtocol protocol) throws Exception {
    protocol(config(protocol, false));
    setUp(CACHED, CACHED_SERVER);
    StreamingHttpConnection connection = streamingHttpConnection();
    StreamingHttpResponse response = requestWithInvalidHeader(connection);
    assertResponse(response, protocol.version, OK, 0);
    if (protocol == HTTP_1) {
        // HTTP/1.1 will try parsing the X_HEADER_NAME as a new request and fail, closing the connection:
        ExecutionException e = assertThrows(ExecutionException.class, () -> requestWithInvalidHeader(connection));
        assertThat(e.getCause(), instanceOf(IOException.class));
    } else {
        // HTTP/2 uses binary encoding and can transfer illegal header values with not problem:
        StreamingHttpResponse secondResponse = requestWithInvalidHeader(connection);
        assertResponse(secondResponse, protocol.version, OK, 0);
    }
}
Also used : IOException(java.io.IOException) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) ExecutionException(java.util.concurrent.ExecutionException) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

StreamingHttpConnection (io.servicetalk.http.api.StreamingHttpConnection)29 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)13 Buffer (io.servicetalk.buffer.api.Buffer)12 Test (org.junit.jupiter.api.Test)12 SingleAddressHttpClientBuilder (io.servicetalk.http.api.SingleAddressHttpClientBuilder)10 HttpServerContext (io.servicetalk.http.api.HttpServerContext)9 RedirectConfigBuilder (io.servicetalk.http.api.RedirectConfigBuilder)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 ExecutionException (java.util.concurrent.ExecutionException)6 BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)5 EnumSource (org.junit.jupiter.params.provider.EnumSource)5 ServerContext (io.servicetalk.transport.api.ServerContext)4 Single (io.servicetalk.concurrent.api.Single)3 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CharSequences.newAsciiString (io.servicetalk.buffer.api.CharSequences.newAsciiString)2 Publisher.fromInputStream (io.servicetalk.concurrent.api.Publisher.fromInputStream)2 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)2