Search in sources :

Example 96 with StreamingHttpResponse

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

the class AbstractHttpConnectionTest method requestShouldWriteFlatStreamToConnectionAndReadFlatStreamSplicedIntoResponseAndPayload.

@SuppressWarnings("unchecked")
@Test
void requestShouldWriteFlatStreamToConnectionAndReadFlatStreamSplicedIntoResponseAndPayload() throws Exception {
    Buffer chunk1 = allocator.fromAscii("test");
    Buffer chunk2 = allocator.fromAscii("payload");
    Buffer chunk3 = allocator.fromAscii("payload");
    HttpHeaders trailers = headersFactory.newEmptyTrailers();
    HttpHeaders headers = headersFactory.newHeaders();
    headers.add(TRANSFER_ENCODING, CHUNKED);
    StreamingHttpRequest req = newTransportRequest(GET, "/foo", HTTP_1_1, headers, allocator, from(chunk1, chunk2, chunk3, trailers), false, headersFactory);
    HttpResponseMetaData respMeta = newResponseMetaData(HTTP_1_1, OK, INSTANCE.newHeaders().add(CONTENT_TYPE, TEXT_PLAIN));
    Publisher<Object> respFlat = from(respMeta, chunk1, chunk2, chunk3, trailers);
    ArgumentCaptor<Publisher<Object>> reqFlatCaptor = ArgumentCaptor.forClass(Publisher.class);
    when(reqResp.apply(reqFlatCaptor.capture())).thenReturn(respFlat);
    Single<StreamingHttpResponse> responseSingle = http.request(req);
    StreamingHttpResponse resp = awaitIndefinitelyNonNull(responseSingle);
    assertThat(reqFlatCaptor.getValue().toFuture().get(), contains(req, chunk1, chunk2, chunk3, trailers));
    assertThat(resp.status(), equalTo(OK));
    assertThat(resp.version(), equalTo(HTTP_1_1));
    assertThat(resp.headers().get(CONTENT_TYPE), equalTo(TEXT_PLAIN));
    assertThat(resp.payloadBody().toFuture().get(), contains(chunk1, chunk2, chunk3));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Publisher(io.servicetalk.concurrent.api.Publisher) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 97 with StreamingHttpResponse

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

the class AbstractHttpServiceAsyncContextTest 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().toFuture().get();
}
Also used : StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 98 with StreamingHttpResponse

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

the class AbstractTimeoutHttpFilterTest method payloadBodyTimeout.

@ParameterizedTest
@MethodSource("executionStrategies")
void payloadBodyTimeout(HttpExecutionStrategy strategy) {
    TestPublisher<Buffer> payloadBody = new TestPublisher<>();
    AtomicBoolean responseSucceeded = new AtomicBoolean();
    StepVerifiers.create(applyFilter(ofMillis(100L), true, strategy, responseWith(payloadBody)).whenOnSuccess(__ -> responseSucceeded.set(true)).flatMapPublisher(StreamingHttpResponse::payloadBody)).thenRequest(MAX_VALUE).expectErrorMatches(t -> TimeoutException.class.isInstance(t) && (Thread.currentThread() instanceof IoThreadFactory.IoThread ^ strategy.hasOffloads())).verify();
    assertThat("Response did not succeeded", responseSucceeded.get(), is(true));
    assertThat("No subscribe for payload body", payloadBody.isSubscribed(), is(true));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) Arrays(java.util.Arrays) TestSingle(io.servicetalk.concurrent.api.TestSingle) BiFunction(java.util.function.BiFunction) MAX_VALUE(java.lang.Long.MAX_VALUE) TimeoutException(java.util.concurrent.TimeoutException) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) Duration(java.time.Duration) Executor(io.servicetalk.concurrent.api.Executor) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) MethodSource(org.junit.jupiter.params.provider.MethodSource) Executors.immediate(io.servicetalk.concurrent.api.Executors.immediate) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Test(org.junit.jupiter.api.Test) Buffer(io.servicetalk.buffer.api.Buffer) HttpExecutionStrategies.offloadAll(io.servicetalk.http.api.HttpExecutionStrategies.offloadAll) ZERO(java.time.Duration.ZERO) Matchers.is(org.hamcrest.Matchers.is) Duration.ofMillis(java.time.Duration.ofMillis) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) Publisher(io.servicetalk.concurrent.api.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Duration.ofSeconds(java.time.Duration.ofSeconds) AtomicReference(java.util.concurrent.atomic.AtomicReference) EmptyHttpHeaders(io.servicetalk.http.api.EmptyHttpHeaders) StreamingHttpResponses.newResponse(io.servicetalk.http.api.StreamingHttpResponses.newResponse) IoThreadFactory(io.servicetalk.transport.api.IoThreadFactory) Duration.ofNanos(java.time.Duration.ofNanos) StepVerifiers(io.servicetalk.concurrent.api.test.StepVerifiers) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) DEFAULT_TIMEOUT_SECONDS(io.servicetalk.concurrent.internal.TimeoutTracingInfoExtension.DEFAULT_TIMEOUT_SECONDS) HttpExecutionStrategies(io.servicetalk.http.api.HttpExecutionStrategies) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DefaultThreadFactory(io.servicetalk.concurrent.api.DefaultThreadFactory) ValueSource(org.junit.jupiter.params.provider.ValueSource) NettyIoExecutors(io.servicetalk.transport.netty.NettyIoExecutors) Single(io.servicetalk.concurrent.api.Single) OK(io.servicetalk.http.api.HttpResponseStatus.OK) IoExecutor(io.servicetalk.transport.api.IoExecutor) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) TimeSource(io.servicetalk.concurrent.TimeSource) Executors(io.servicetalk.concurrent.api.Executors) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) HttpExecutionStrategies.offloadNever(io.servicetalk.http.api.HttpExecutionStrategies.offloadNever) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) IoThreadFactory(io.servicetalk.transport.api.IoThreadFactory) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) TimeoutException(java.util.concurrent.TimeoutException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 99 with StreamingHttpResponse

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

the class AbstractTimeoutHttpFilterTest method payloadBodyDoesNotTimeoutWhenIgnored.

@Test
void payloadBodyDoesNotTimeoutWhenIgnored() {
    Duration timeout = ofMillis(100L);
    TestPublisher<Buffer> payloadBody = new TestPublisher<>();
    AtomicBoolean responseSucceeded = new AtomicBoolean();
    StepVerifiers.create(applyFilter(timeout, false, defaultStrategy(), responseWith(payloadBody)).whenOnSuccess(__ -> responseSucceeded.set(true)).flatMapPublisher(StreamingHttpResponse::payloadBody)).expectSubscriptionConsumed(subscription -> immediate().schedule(subscription::cancel, timeout.plusMillis(10L))).thenRequest(MAX_VALUE).expectNoSignals(timeout.plusMillis(5L)).expectError(// should never happen
    IllegalStateException.class).verify();
    assertThat("Response did not succeeded", responseSucceeded.get(), is(true));
    assertThat("No subscribe for payload body", payloadBody.isSubscribed(), is(true));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) Arrays(java.util.Arrays) TestSingle(io.servicetalk.concurrent.api.TestSingle) BiFunction(java.util.function.BiFunction) MAX_VALUE(java.lang.Long.MAX_VALUE) TimeoutException(java.util.concurrent.TimeoutException) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) Duration(java.time.Duration) Executor(io.servicetalk.concurrent.api.Executor) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) MethodSource(org.junit.jupiter.params.provider.MethodSource) Executors.immediate(io.servicetalk.concurrent.api.Executors.immediate) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Test(org.junit.jupiter.api.Test) Buffer(io.servicetalk.buffer.api.Buffer) HttpExecutionStrategies.offloadAll(io.servicetalk.http.api.HttpExecutionStrategies.offloadAll) ZERO(java.time.Duration.ZERO) Matchers.is(org.hamcrest.Matchers.is) Duration.ofMillis(java.time.Duration.ofMillis) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) Publisher(io.servicetalk.concurrent.api.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Duration.ofSeconds(java.time.Duration.ofSeconds) AtomicReference(java.util.concurrent.atomic.AtomicReference) EmptyHttpHeaders(io.servicetalk.http.api.EmptyHttpHeaders) StreamingHttpResponses.newResponse(io.servicetalk.http.api.StreamingHttpResponses.newResponse) IoThreadFactory(io.servicetalk.transport.api.IoThreadFactory) Duration.ofNanos(java.time.Duration.ofNanos) StepVerifiers(io.servicetalk.concurrent.api.test.StepVerifiers) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) DEFAULT_TIMEOUT_SECONDS(io.servicetalk.concurrent.internal.TimeoutTracingInfoExtension.DEFAULT_TIMEOUT_SECONDS) HttpExecutionStrategies(io.servicetalk.http.api.HttpExecutionStrategies) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DefaultThreadFactory(io.servicetalk.concurrent.api.DefaultThreadFactory) ValueSource(org.junit.jupiter.params.provider.ValueSource) NettyIoExecutors(io.servicetalk.transport.netty.NettyIoExecutors) Single(io.servicetalk.concurrent.api.Single) OK(io.servicetalk.http.api.HttpResponseStatus.OK) IoExecutor(io.servicetalk.transport.api.IoExecutor) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) TimeSource(io.servicetalk.concurrent.TimeSource) Executors(io.servicetalk.concurrent.api.Executors) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) HttpExecutionStrategies.offloadNever(io.servicetalk.http.api.HttpExecutionStrategies.offloadNever) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) Duration(java.time.Duration) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 100 with StreamingHttpResponse

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

the class BeforeFinallyHttpOperatorTest method duplicateOnSuccess.

@ParameterizedTest(name = "{displayName} [{index}] discardEventsAfterCancel={0}")
@ValueSource(booleans = { false, true })
void duplicateOnSuccess(boolean discardEventsAfterCancel) {
    AtomicReference<SingleSource.Subscriber<? super StreamingHttpResponse>> subRef = new AtomicReference<>();
    Single<StreamingHttpResponse> original = new Single<StreamingHttpResponse>() {

        @Override
        protected void handleSubscribe(final Subscriber<? super StreamingHttpResponse> subscriber) {
            subRef.set(subscriber);
            subscriber.onSubscribe(IGNORE_CANCEL);
        }
    };
    final ResponseSubscriber subscriber = new ResponseSubscriber();
    toSource(original.liftSync(new BeforeFinallyHttpOperator(beforeFinally, discardEventsAfterCancel))).subscribe(subscriber);
    assertThat("Original Single not subscribed.", subRef.get(), is(notNullValue()));
    assertThat("onSubscribe not called.", subscriber.cancellable, is(notNullValue()));
    final StreamingHttpResponse response = reqRespFactory.newResponse(OK);
    subRef.get().onSuccess(response);
    final StreamingHttpResponse received = subscriber.verifyResponseReceived();
    verifyNoInteractions(beforeFinally);
    final StreamingHttpResponse response2 = reqRespFactory.newResponse(OK);
    try {
        subRef.get().onSuccess(response2);
    } catch (AssertionError e) {
    // silence assert on dupe success when not canceled
    }
    final StreamingHttpResponse received2 = subscriber.verifyResponseReceived();
    // Old response should be preserved.
    assertThat("Duplicate response received.", received2, is(received));
    verifyNoInteractions(beforeFinally);
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) Subscriber(io.servicetalk.concurrent.SingleSource.Subscriber) LegacyTestSingle(io.servicetalk.concurrent.api.LegacyTestSingle) Single(io.servicetalk.concurrent.api.Single) AtomicReference(java.util.concurrent.atomic.AtomicReference) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)162 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)90 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)82 MethodSource (org.junit.jupiter.params.provider.MethodSource)62 Test (org.junit.jupiter.api.Test)53 Buffer (io.servicetalk.buffer.api.Buffer)45 Single (io.servicetalk.concurrent.api.Single)41 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)32 OK (io.servicetalk.http.api.HttpResponseStatus.OK)30 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)30 Nullable (javax.annotation.Nullable)29 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)28 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)27 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)26 ServerContext (io.servicetalk.transport.api.ServerContext)24 InetSocketAddress (java.net.InetSocketAddress)24 CountDownLatch (java.util.concurrent.CountDownLatch)24 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)23 Matchers.is (org.hamcrest.Matchers.is)23 Publisher (io.servicetalk.concurrent.api.Publisher)20