use of io.servicetalk.http.api.StreamingHttpServiceFilter 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.StreamingHttpServiceFilter 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.StreamingHttpServiceFilter in project servicetalk by apple.
the class TimeoutHttpServiceFilterTest method applyFilter.
private static Single<StreamingHttpResponse> applyFilter(TimeoutHttpServiceFilter filterFactory, final HttpExecutionStrategy strategy, final Single<StreamingHttpResponse> responseSingle) {
HttpExecutionContext executionContext = new DefaultHttpExecutionContext(DEFAULT_ALLOCATOR, IO_EXECUTOR, EXECUTOR, strategy);
HttpServiceContext serviceContext = mock(HttpServiceContext.class);
when(serviceContext.executionContext()).thenReturn(executionContext);
StreamingHttpService service = mock(StreamingHttpService.class);
when(service.handle(any(), any(), any())).thenReturn(responseSingle);
StreamingHttpServiceFilter filter = filterFactory.create(service);
return filter.handle(serviceContext, mock(StreamingHttpRequest.class), mock(StreamingHttpResponseFactory.class));
}
use of io.servicetalk.http.api.StreamingHttpServiceFilter in project servicetalk by apple.
the class BasicAuthHttpServiceFilterTest method closeAsync.
@Test
void closeAsync() throws Exception {
AtomicBoolean credentialsVerifierClosed = new AtomicBoolean();
AtomicBoolean nextServiceClosed = new AtomicBoolean();
StreamingHttpServiceFilter service = new BasicAuthHttpServiceFilter.Builder<>(new CredentialsVerifier<BasicUserInfo>() {
@Override
public Single<BasicUserInfo> apply(final String userId, final String password) {
return never();
}
@Override
public Completable closeAsync() {
return completed().beforeOnComplete(() -> credentialsVerifierClosed.set(true));
}
}, REALM_VALUE).buildServer().create(new StreamingHttpService() {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory factory) {
return never();
}
@Override
public Completable closeAsync() {
return completed().beforeOnComplete(() -> nextServiceClosed.set(true));
}
});
assertFalse(credentialsVerifierClosed.get());
assertFalse(nextServiceClosed.get());
service.closeAsync().toFuture().get();
assertTrue(credentialsVerifierClosed.get());
assertTrue(nextServiceClosed.get());
}
use of io.servicetalk.http.api.StreamingHttpServiceFilter in project servicetalk by apple.
the class BasicAuthHttpServiceFilterTest method testAuthenticatedForProxy.
private static void testAuthenticatedForProxy(StreamingHttpRequest request) throws Exception {
StreamingHttpServiceFilter service = new BasicAuthHttpServiceFilter.Builder<>(CREDENTIALS_VERIFIER, REALM_VALUE).userInfoAsyncContextKey(USER_INFO_AC_KEY).userInfoRequestContextKey(USER_INFO_RC_KEY).buildProxy().create(HELLO_WORLD_SERVICE);
StreamingHttpResponse response = awaitIndefinitelyNonNull(service.handle(CONN_CTX, request, reqRespFactory));
assertEquals(OK, response.status());
assertTrue(response.headers().contains(USER_ID_AC_HEADER_NAME, "userId"));
assertTrue(response.headers().contains(USER_ID_RC_HEADER_NAME, "userId"));
}
Aggregations