use of io.servicetalk.http.api.HttpServiceContext 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.HttpServiceContext in project servicetalk by apple.
the class TestServiceStreaming method handle.
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext context, final StreamingHttpRequest req, final StreamingHttpResponseFactory factory) {
LOGGER.debug("({}) Handling {}", counter, req.toString((a, b) -> b));
final StreamingHttpResponse response;
switch(req.path()) {
case SVC_ECHO:
response = newEchoResponse(req, factory);
break;
case SVC_COUNTER_NO_LAST_CHUNK:
response = newTestCounterResponse(context, req, factory);
break;
case SVC_COUNTER:
response = newTestCounterResponseWithLastPayloadChunk(context, req, factory);
break;
case SVC_LARGE_LAST:
response = newLargeLastChunkResponse(context, req, factory);
break;
case SVC_TEST_PUBLISHER:
response = newTestPublisherResponse(req, factory);
break;
case SVC_NO_CONTENT:
response = newNoContentResponse(req, factory);
break;
case SVC_NO_CONTENT_AFTER_READ:
return req.payloadBody().ignoreElements().concat(succeeded(newNoContentResponse(req, factory)));
case SVC_ROT13:
response = newRot13Response(req, factory);
break;
case SVC_NEVER:
return req.payloadBody().ignoreElements().concat(never());
case SVC_THROW_ERROR:
response = throwErrorSynchronously();
break;
case SVC_SINGLE_ERROR:
return Single.failed(DELIBERATE_EXCEPTION);
case SVC_ERROR_BEFORE_READ:
response = throwErrorBeforeRead(req, factory);
break;
case SVC_ERROR_DURING_READ:
response = throwErrorDuringRead(req, factory);
break;
default:
response = newNotFoundResponse(req, factory);
}
return succeeded(response);
}
use of io.servicetalk.http.api.HttpServiceContext in project servicetalk by apple.
the class ContentLengthAndTrailersTest method setUp.
private void setUp(HttpProtocol protocol, String content) {
this.protocol = protocol;
this.content = content;
protocol(protocol.config);
serviceFilterFactory(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
// Use transform to simulate access to request trailers
return delegate().handle(ctx, request.transform(new StatelessTrailersTransformer<>()), responseFactory).map(response -> {
final HttpHeaders headers = request.headers();
if (headers.contains(CONTENT_LENGTH)) {
response.setHeader(CLIENT_CONTENT_LENGTH, mergeValues(headers.values(CONTENT_LENGTH)));
}
if (headers.contains(TRANSFER_ENCODING)) {
response.setHeader(CLIENT_TRANSFER_ENCODING, mergeValues(headers.values(TRANSFER_ENCODING)));
}
return response;
});
}
});
setUp(CACHED, CACHED_SERVER);
}
use of io.servicetalk.http.api.HttpServiceContext in project servicetalk by apple.
the class ConsumeRequestPayloadOnResponsePathTest method test.
private void test(final BiFunction<Single<StreamingHttpResponse>, StreamingHttpRequest, Single<StreamingHttpResponse>> consumeRequestPayload) throws Exception {
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).appendServiceFilter(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
return consumeRequestPayload.apply(delegate().handle(ctx, request, responseFactory), request);
}
}).listenStreamingAndAwait((ctx, request, responseFactory) -> {
final StreamingHttpResponse response = responseFactory.ok().addHeader(TRAILER, X_TOTAL_LENGTH).payloadBody(from("Response\n", "Payload\n", "Body\n"), appSerializerUtf8FixLen()).transform(new TrailersTransformer<AtomicInteger, Buffer>() {
@Override
public AtomicInteger newState() {
return new AtomicInteger();
}
@Override
public Buffer accept(final AtomicInteger total, final Buffer chunk) {
total.addAndGet(chunk.readableBytes());
return chunk;
}
@Override
public HttpHeaders payloadComplete(final AtomicInteger total, final HttpHeaders trailers) {
trailers.add(X_TOTAL_LENGTH, String.valueOf(total.get()));
return trailers;
}
@Override
public HttpHeaders catchPayloadFailure(final AtomicInteger __, final Throwable ___, final HttpHeaders trailers) {
return trailers;
}
});
return succeeded(response);
})) {
HttpResponse response;
try (BlockingHttpClient client = HttpClients.forSingleAddress(AddressUtils.serverHostAndPort(serverContext)).buildBlocking()) {
response = client.request(client.post("/").payloadBody(EXPECTED_REQUEST_PAYLOAD, textSerializerUtf8()));
serverLatch.await();
}
assertThat(response.status(), is(OK));
assertThat("Request payload body might be consumed by someone else", errorRef.get(), is(nullValue()));
assertThat(receivedPayload.toString(), is(EXPECTED_REQUEST_PAYLOAD));
assertThat(response.headers().contains(TRAILER, X_TOTAL_LENGTH), is(true));
assertThat(response.trailers().contains(X_TOTAL_LENGTH), is(true));
CharSequence trailerLength = response.trailers().get(X_TOTAL_LENGTH);
assertNotNull(trailerLength);
assertThat("Unexpected response payload: '" + response.payloadBody().toString(UTF_8) + "'", trailerLength.toString(), is(Integer.toString(response.payloadBody().readableBytes())));
}
}
use of io.servicetalk.http.api.HttpServiceContext in project servicetalk by apple.
the class AlpnClientAndServerTest method assertResponseAndServiceContext.
private void assertResponseAndServiceContext(HttpResponse response) throws Exception {
assertThat(response.version(), is(expectedProtocol));
assertThat(response.status(), is(OK));
assertThat(response.payloadBody(textSerializerUtf8()), is(PAYLOAD_BODY));
HttpServiceContext serviceCtx = serviceContext.take();
assertThat(serviceCtx.protocol(), is(expectedProtocol));
assertThat(serviceCtx.sslSession(), is(notNullValue()));
assertThat(serviceCtx.sslSession(), is(notNullValue()));
assertThat(requestVersion.take(), is(expectedProtocol));
assertThat(serviceContext, is(empty()));
assertThat(requestVersion, is(empty()));
}
Aggregations