Search in sources :

Example 6 with StreamingHttpConnectionFilter

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

the class RequestResponseContextTest method setUp.

void setUp(Api api) {
    this.api = api;
    connectionFilterFactory(connection -> new StreamingHttpConnectionFilter(connection) {

        @Override
        public Single<StreamingHttpResponse> request(StreamingHttpRequest request) {
            final List<String> requestCtxValue = request.context().get(CLIENT_REQUEST_KEY);
            try {
                assertThat(requestCtxValue, is(notNullValue()));
                assertThat(requestCtxValue, hasSize(1));
                request.addHeader(CONTEXT_HEADER, requestCtxValue.get(0));
            } catch (Throwable t) {
                asyncError.add(t);
            }
            return delegate().request(request).map(response -> {
                response.context().put(CLIENT_RESPONSE_KEY, valueOf(response.headers().get(CONTEXT_HEADER)));
                return response;
            });
        }
    });
    serviceFilterFactory(service -> new StreamingHttpServiceFilter(service) {

        @Override
        public Single<StreamingHttpResponse> handle(HttpServiceContext ctx, StreamingHttpRequest request, StreamingHttpResponseFactory responseFactory) {
            CharSequence requestCtxValue = request.headers().get(CONTEXT_HEADER);
            try {
                assertThat(requestCtxValue, is(notNullValue()));
                request.context().put(SERVER_REQUEST_KEY, singletonList(requestCtxValue.toString()));
            } catch (Throwable t) {
                asyncError.add(t);
            }
            return delegate().handle(ctx, request, responseFactory).map(response -> {
                response.headers().add(CONTEXT_HEADER, valueOf(response.context().get(SERVER_RESPONSE_KEY)));
                return response;
            });
        }
    });
    setUp(CACHED, CACHED_SERVER);
}
Also used : TestUtils.assertNoAsyncErrors(io.servicetalk.test.resources.TestUtils.assertNoAsyncErrors) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) Key.newKey(io.servicetalk.context.api.ContextMap.Key.newKey) Collections.singletonList(java.util.Collections.singletonList) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) HttpPayloadWriter(io.servicetalk.http.api.HttpPayloadWriter) HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) HttpClient(io.servicetalk.http.api.HttpClient) Matchers.hasSize(org.hamcrest.Matchers.hasSize) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) HttpApiConversions.toStreamingHttpService(io.servicetalk.http.api.HttpApiConversions.toStreamingHttpService) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) Matchers.contentEqualTo(io.servicetalk.buffer.api.Matchers.contentEqualTo) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Nullable(javax.annotation.Nullable) CACHED_SERVER(io.servicetalk.http.netty.AbstractNettyHttpServerTest.ExecutorSupplier.CACHED_SERVER) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpService(io.servicetalk.http.api.HttpService) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Single(io.servicetalk.concurrent.api.Single) HttpResponse(io.servicetalk.http.api.HttpResponse) SVC_ECHO(io.servicetalk.http.netty.TestServiceStreaming.SVC_ECHO) StreamingHttpConnectionFilter(io.servicetalk.http.api.StreamingHttpConnectionFilter) OK(io.servicetalk.http.api.HttpResponseStatus.OK) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Test(org.junit.jupiter.api.Test) List(java.util.List) Buffer(io.servicetalk.buffer.api.Buffer) CACHED(io.servicetalk.http.netty.AbstractNettyHttpServerTest.ExecutorSupplier.CACHED) String.valueOf(java.lang.String.valueOf) ContextMap(io.servicetalk.context.api.ContextMap) BlockingHttpService(io.servicetalk.http.api.BlockingHttpService) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Matchers.contains(org.hamcrest.Matchers.contains) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) Matchers.is(org.hamcrest.Matchers.is) Queue(java.util.Queue) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) StreamingHttpConnectionFilter(io.servicetalk.http.api.StreamingHttpConnectionFilter) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) Single(io.servicetalk.concurrent.api.Single) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest)

Example 7 with StreamingHttpConnectionFilter

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

the class HttpLifecycleObserverTest method testConnectionFailsRequestBeforeWrite.

@ParameterizedTest(name = "{displayName} [{index}] protocol={0}")
@EnumSource(HttpProtocol.class)
void testConnectionFailsRequestBeforeWrite(HttpProtocol protocol) throws Exception {
    connectionFilterFactory(client -> new StreamingHttpConnectionFilter(client) {

        @Override
        public Single<StreamingHttpResponse> request(StreamingHttpRequest request) {
            return failed(DELIBERATE_EXCEPTION);
        }
    });
    setUp(protocol);
    ExecutionException e = assertThrows(ExecutionException.class, () -> makeRequestAndAssertResponse(SVC_ECHO, protocol, OK, CONTENT.readableBytes()));
    assertThat(e.getCause(), sameInstance(DELIBERATE_EXCEPTION));
    // server is not involved in this test, count down manually
    bothTerminate.countDown();
    bothTerminate.await();
    clientInOrder.verify(clientLifecycleObserver).onNewExchange();
    clientInOrder.verify(clientExchangeObserver).onRequest(any(StreamingHttpRequest.class));
    clientInOrder.verify(clientExchangeObserver).onConnectionSelected(any(ConnectionInfo.class));
    clientInOrder.verify(clientExchangeObserver).onResponseError(e.getCause());
    clientInOrder.verify(clientExchangeObserver).onExchangeFinally();
    verifyNoMoreInteractions(clientLifecycleObserver, clientExchangeObserver);
    verifyNoInteractions(clientRequestObserver, clientResponseObserver, serverLifecycleObserver, serverExchangeObserver, serverRequestObserver, serverResponseObserver);
}
Also used : StreamingHttpConnectionFilter(io.servicetalk.http.api.StreamingHttpConnectionFilter) Single(io.servicetalk.concurrent.api.Single) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ConnectionInfo(io.servicetalk.transport.api.ConnectionInfo) ExecutionException(java.util.concurrent.ExecutionException) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

StreamingHttpConnectionFilter (io.servicetalk.http.api.StreamingHttpConnectionFilter)7 Single (io.servicetalk.concurrent.api.Single)6 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)6 OK (io.servicetalk.http.api.HttpResponseStatus.OK)5 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)5 Channel (io.netty.channel.Channel)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)4 Buffer (io.servicetalk.buffer.api.Buffer)4 HttpResponse (io.servicetalk.http.api.HttpResponse)4 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)4 HostAndPort (io.servicetalk.transport.api.HostAndPort)4 InetSocketAddress (java.net.InetSocketAddress)4 Nullable (javax.annotation.Nullable)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Matchers.is (org.hamcrest.Matchers.is)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ChannelInitializer (io.netty.channel.ChannelInitializer)3 EventLoopGroup (io.netty.channel.EventLoopGroup)3 Matchers.contentEqualTo (io.servicetalk.buffer.api.Matchers.contentEqualTo)3 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)3