use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.
the class ContentLengthAndTrailersTest method test.
private void test(Transformer<StreamingHttpRequest> requestTransformer, Transformer<StreamingHttpResponse> responseTransformer, boolean hasContentLength, boolean chunked, boolean hasTrailers) throws Exception {
StreamingHttpRequest preRequest = streamingHttpConnection().post("/");
if (!content.isEmpty()) {
preRequest.payloadBody(from(content), appSerializerUtf8FixLen());
}
StreamingHttpRequest request = requestTransformer.transform(preRequest);
StreamingHttpResponse response = responseTransformer.transform(makeRequest(request));
assertResponse(response, protocol.version, OK);
HttpHeaders headers = response.headers();
assertThat("Unexpected content-length on the response", mergeValues(headers.values(CONTENT_LENGTH)), contentEqualTo(hasContentLength ? valueOf(addFixedLengthFramingOverhead(content.length())) : ""));
assertThat("Unexpected transfer-encoding on the response", mergeValues(headers.values(TRANSFER_ENCODING)), contentEqualTo(chunked ? CHUNKED : ""));
assertThat("Unexpected content-length on the request", headers.get(CLIENT_CONTENT_LENGTH), hasContentLength ? contentEqualTo(valueOf(addFixedLengthFramingOverhead(content.length()))) : nullValue());
assertThat("Unexpected transfer-encoding on the request", headers.get(CLIENT_TRANSFER_ENCODING), chunked ? contentEqualTo(CHUNKED) : nullValue());
if (content.isEmpty()) {
response.transform(new TrailersTransformer<Object, Buffer>() {
@Nullable
@Override
public Integer newState() {
return null;
}
@Override
public Buffer accept(@Nullable final Object o, final Buffer buffer) {
assertThat(buffer.readableBytes(), equalTo(0));
return buffer;
}
@Override
public HttpHeaders payloadComplete(@Nullable final Object o, final HttpHeaders trailers) {
assertThat("Unexpected trailers on the request", trailers.get(TRAILER_NAME), hasTrailers ? contentEqualTo(TRAILER_VALUE) : nullValue());
return trailers;
}
@Override
public HttpHeaders catchPayloadFailure(@Nullable final Object o, final Throwable cause, final HttpHeaders trailers) throws Throwable {
throw cause;
}
}).messageBody().ignoreElements().toFuture().get();
} else {
response.transform(new TrailersTransformer<StringBuilder, String>() {
@Override
public StringBuilder newState() {
return new StringBuilder();
}
@Override
public String accept(final StringBuilder o, final String s) {
o.append(s);
return s;
}
@Override
public HttpHeaders payloadComplete(final StringBuilder o, final HttpHeaders trailers) {
assertThat(o.toString(), equalTo(content));
assertThat("Unexpected trailers on the request", trailers.get(TRAILER_NAME), hasTrailers ? contentEqualTo(TRAILER_VALUE) : nullValue());
return trailers;
}
@Override
public HttpHeaders catchPayloadFailure(@Nullable final StringBuilder o, final Throwable cause, final HttpHeaders trailers) throws Throwable {
throw cause;
}
}, appSerializerUtf8FixLen()).messageBody().ignoreElements().toFuture().get();
}
}
use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.
the class HttpServerMultipleRequestsTest 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().subscribe();
}
use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.
the class HttpTransportObserverTest method setUp.
private void setUp(HttpProtocol protocol) {
this.protocol = protocol;
protocol(protocol.config);
connectionAcceptor(ctx -> {
ctx.onClose().whenFinally(serverConnectionClosed::countDown).subscribe();
return completed();
});
serviceFilterFactory(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(HttpServiceContext ctx, StreamingHttpRequest request, StreamingHttpResponseFactory responseFactory) {
requestReceived.countDown();
try {
processRequest.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throwException(e);
}
return delegate().handle(ctx, request, responseFactory);
}
});
clientTransportObserver = mock(TransportObserver.class, "clientTransportObserver");
clientConnectionObserver = mock(ConnectionObserver.class, "clientConnectionObserver");
clientDataObserver = mock(DataObserver.class, "clientDataObserver");
clientMultiplexedObserver = mock(MultiplexedObserver.class, "clientMultiplexedObserver");
clientStreamObserver = mock(StreamObserver.class, "clientStreamObserver");
clientReadObserver = mock(ReadObserver.class, "clientReadObserver");
clientWriteObserver = mock(WriteObserver.class, "clientWriteObserver");
when(clientTransportObserver.onNewConnection(any(), any())).thenReturn(clientConnectionObserver);
lenient().when(clientConnectionObserver.connectionEstablished(any(ConnectionInfo.class))).thenReturn(clientDataObserver);
lenient().when(clientConnectionObserver.multiplexedConnectionEstablished(any(ConnectionInfo.class))).thenReturn(clientMultiplexedObserver);
lenient().when(clientMultiplexedObserver.onNewStream()).thenReturn(clientStreamObserver);
lenient().when(clientStreamObserver.streamEstablished()).thenReturn(clientDataObserver);
lenient().when(clientDataObserver.onNewRead()).thenReturn(clientReadObserver);
lenient().when(clientDataObserver.onNewWrite()).thenReturn(clientWriteObserver);
serverTransportObserver = mock(TransportObserver.class, "serverTransportObserver");
serverConnectionObserver = mock(ConnectionObserver.class, "serverConnectionObserver");
serverDataObserver = mock(DataObserver.class, "serverDataObserver");
serverMultiplexedObserver = mock(MultiplexedObserver.class, "serverMultiplexedObserver");
serverStreamObserver = mock(StreamObserver.class, "serverStreamObserver");
serverReadObserver = mock(ReadObserver.class, "serverReadObserver");
serverWriteObserver = mock(WriteObserver.class, "serverWriteObserver");
when(serverTransportObserver.onNewConnection(any(), any())).thenReturn(serverConnectionObserver);
lenient().when(serverConnectionObserver.connectionEstablished(any(ConnectionInfo.class))).thenReturn(serverDataObserver);
lenient().when(serverConnectionObserver.multiplexedConnectionEstablished(any(ConnectionInfo.class))).thenReturn(serverMultiplexedObserver);
lenient().when(serverMultiplexedObserver.onNewStream()).thenReturn(serverStreamObserver);
lenient().when(serverStreamObserver.streamEstablished()).thenReturn(serverDataObserver);
lenient().when(serverDataObserver.onNewRead()).thenReturn(serverReadObserver);
lenient().when(serverDataObserver.onNewWrite()).thenReturn(serverWriteObserver);
transportObserver(clientTransportObserver, serverTransportObserver);
setUp(CACHED, CACHED_SERVER);
}
use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.
the class HttpStreamingClientOverrideOffloadingTest method reserveRespectsDisable.
@ParameterizedTest
@EnumSource(Params.class)
void reserveRespectsDisable(final Params params) throws Exception {
setUp(params);
StreamingHttpRequest request = client.get("/");
request.context().put(HTTP_EXECUTION_STRATEGY_KEY, this.overridingStrategy);
client.reserveConnection(request).beforeOnSuccess(__ -> {
if (isInvalidThread()) {
throw new AssertionError("Invalid thread found providing the connection. Thread: " + currentThread());
}
}).toFuture().get().closeAsync().toFuture().get();
}
use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.
the class MalformedDataAfterHttpMessageTest method afterResponseNextClientRequestSucceeds.
@ParameterizedTest
@ValueSource(booleans = { true, false })
void afterResponseNextClientRequestSucceeds(boolean doOffloading) throws Exception {
Queue<ConnectionContext> contextQueue = new LinkedBlockingQueue<>();
ServerSocketChannel server = nettyServer(RESPONSE_MSG);
try (BlockingHttpClient client = stClientBuilder(server.localAddress()).executionStrategy(doOffloading ? defaultStrategy() : offloadNever()).appendClientFilter(new RetryingHttpRequesterFilter.Builder().retryOther((req, cause) -> ofConstantBackoffFullJitter(ofNanos(1), MAX_VALUE)).build()).appendConnectionFilter(connection -> new StreamingHttpConnectionFilter(connection) {
@Override
public Single<StreamingHttpResponse> request(final StreamingHttpRequest request) {
contextQueue.add(connectionContext());
return super.request(request);
}
}).buildBlocking()) {
validateClientResponse(client.request(client.get("/1")));
validateClientResponse(client.request(client.get("/2")));
ConnectionContext ctx1 = contextQueue.poll();
assertThat(ctx1, not(nullValue()));
// RetryingHttpRequesterFilter or AutoRetry may re-issue the request if a failure is seen locally. Verify
// the last connection (used for second request) is different from the first.
ConnectionContext ctx2 = null;
ConnectionContext tmp;
while ((tmp = contextQueue.poll()) != null) {
ctx2 = tmp;
}
assertThat(ctx2, not(nullValue()));
assertThat(ctx1, not(equalTo(ctx2)));
assertThat(contextQueue, empty());
} finally {
server.close().sync();
}
}
Aggregations