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));
}
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();
}
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));
}
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));
}
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);
}
Aggregations