use of io.servicetalk.http.api.StreamingHttpResponseFactory 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.StreamingHttpResponseFactory 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.StreamingHttpResponseFactory in project servicetalk by apple.
the class ServiceTalkContentEncodingTest method runTest.
@Override
protected void runTest(final HttpProtocol protocol, final Encoder clientEncoding, final Decoders clientDecoder, final Encoders serverEncoder, final Decoders serverDecoder, boolean valid) throws Throwable {
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).protocols(protocol.config).appendServiceFilter(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
return delegate().handle(ctx, request, responseFactory).map(resp -> {
// content encoding should be stripped by the time the decoding is done.
assertThat(resp.headers().get(ACCEPT_ENCODING), nullValue());
CharSequence contentEncoding = resp.headers().get(CONTENT_ENCODING);
boolean found = contentEncoding == null && (serverEncoder.list.isEmpty() || !request.headers().contains(ACCEPT_ENCODING));
for (BufferEncoder be : serverEncoder.list) {
if (contentEncoding == null && contentEqualsIgnoreCase(be.encodingName(), identityEncoder().encodingName()) || contentEncoding != null && contentEqualsIgnoreCase(be.encodingName(), contentEncoding)) {
found = true;
break;
}
}
return found || !valid ? resp : responseFactory.ok().payloadBody(Publisher.from("server error: invalid " + CONTENT_ENCODING + ": " + contentEncoding), appSerializerUtf8FixLen());
}).onErrorReturn(AssertionError.class, cause -> responseFactory.ok().payloadBody(Publisher.from("server error: " + cause.toString()), appSerializerUtf8FixLen()));
}
}).appendServiceFilter(new ContentEncodingHttpServiceFilter(serverEncoder.list, serverDecoder.group)).listenBlockingAndAwait((ctx, request, responseFactory) -> {
String requestPayload = request.payloadBody(textSerializerUtf8());
if (payloadAsString((byte) 'a').equals(requestPayload)) {
return responseFactory.ok().payloadBody(payloadAsString((byte) 'b'), textSerializerUtf8());
} else {
return responseFactory.badRequest().payloadBody(requestPayload, textSerializerUtf8());
}
});
BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).protocols(protocol.config).appendClientFilter(new ContentEncodingHttpRequesterFilter(clientDecoder.group)).appendClientFilter(c -> new StreamingHttpClientFilter(c) {
@Override
protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
return Single.defer(() -> {
assertHeader(() -> clientEncoding.encoder == null ? null : clientEncoding.encoder.encodingName(), request.headers().get(CONTENT_ENCODING), true);
assertHeader(clientDecoder.group::advertisedMessageEncoding, request.headers().get(ACCEPT_ENCODING), false);
return delegate.request(request).shareContextOnSubscribe();
});
}
}).buildBlocking()) {
HttpResponse response = client.request(client.get("/").contentEncoding(clientEncoding.encoder).payloadBody(payloadAsString((byte) 'a'), textSerializerUtf8()));
if (valid) {
assertThat(response.status(), is(OK));
// content encoding should be stripped by the time the decoding is done.
assertThat(response.headers().get(CONTENT_ENCODING), nullValue());
assertEquals(payloadAsString((byte) 'b'), response.payloadBody(textSerializerUtf8()));
} else {
assertThat(response.status(), is(UNSUPPORTED_MEDIA_TYPE));
}
}
}
use of io.servicetalk.http.api.StreamingHttpResponseFactory in project servicetalk by apple.
the class SslAndNonSslConnectionsTest method beforeClass.
@BeforeAll
static void beforeClass() throws Exception {
final HttpHeaders httpHeaders = DefaultHttpHeadersFactory.INSTANCE.newHeaders().set(CONTENT_LENGTH, ZERO);
// Configure HTTP server
when(STREAMING_HTTP_SERVICE.handle(any(), any(), any())).thenAnswer((Answer<Single<StreamingHttpResponse>>) invocation -> {
StreamingHttpResponseFactory factory = invocation.getArgument(2);
StreamingHttpResponse resp = factory.ok();
resp.headers().set(httpHeaders);
return succeeded(resp);
});
when(STREAMING_HTTP_SERVICE.closeAsync()).thenReturn(completed());
when(STREAMING_HTTP_SERVICE.closeAsyncGracefully()).thenReturn(completed());
serverCtx = HttpServers.forAddress(localAddress(0)).executionStrategy(offloadNever()).listenStreamingAndAwait(STREAMING_HTTP_SERVICE);
final String serverHostHeader = hostHeader(serverHostAndPort(serverCtx));
requestTarget = "http://" + serverHostHeader + "/";
// Configure HTTPS server
when(SECURE_STREAMING_HTTP_SERVICE.handle(any(), any(), any())).thenAnswer(invocation -> {
StreamingHttpResponseFactory factory = invocation.getArgument(2);
StreamingHttpResponse resp = factory.ok();
resp.headers().set(httpHeaders);
return succeeded(resp);
});
when(SECURE_STREAMING_HTTP_SERVICE.closeAsync()).thenReturn(completed());
when(SECURE_STREAMING_HTTP_SERVICE.closeAsyncGracefully()).thenReturn(completed());
secureServerCtx = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build()).executionStrategy(offloadNever()).listenStreamingAndAwait(SECURE_STREAMING_HTTP_SERVICE);
final String secureServerHostHeader = hostHeader(serverHostAndPort(secureServerCtx));
secureRequestTarget = "https://" + secureServerHostHeader + "/";
}
use of io.servicetalk.http.api.StreamingHttpResponseFactory in project servicetalk by apple.
the class RequestResponseContextTest method setUp.
void setUp(Api api) {
this.api = api;
connectionFilterFactory(connection -> new StreamingHttpConnectionFilter(connection) {
@Override
public Single<StreamingHttpResponse> request(StreamingHttpRequest request) {
final List<String> requestCtxValue = request.context().get(CLIENT_REQUEST_KEY);
try {
assertThat(requestCtxValue, is(notNullValue()));
assertThat(requestCtxValue, hasSize(1));
request.addHeader(CONTEXT_HEADER, requestCtxValue.get(0));
} catch (Throwable t) {
asyncError.add(t);
}
return delegate().request(request).map(response -> {
response.context().put(CLIENT_RESPONSE_KEY, valueOf(response.headers().get(CONTEXT_HEADER)));
return response;
});
}
});
serviceFilterFactory(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(HttpServiceContext ctx, StreamingHttpRequest request, StreamingHttpResponseFactory responseFactory) {
CharSequence requestCtxValue = request.headers().get(CONTEXT_HEADER);
try {
assertThat(requestCtxValue, is(notNullValue()));
request.context().put(SERVER_REQUEST_KEY, singletonList(requestCtxValue.toString()));
} catch (Throwable t) {
asyncError.add(t);
}
return delegate().handle(ctx, request, responseFactory).map(response -> {
response.headers().add(CONTEXT_HEADER, valueOf(response.context().get(SERVER_RESPONSE_KEY)));
return response;
});
}
});
setUp(CACHED, CACHED_SERVER);
}
Aggregations