use of io.servicetalk.http.api.StreamingHttpConnectionFilter 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);
}
use of io.servicetalk.http.api.StreamingHttpConnectionFilter in project servicetalk by apple.
the class HttpLifecycleObserverTest method testConnectionFailsRequestBeforeWrite.
@ParameterizedTest(name = "{displayName} [{index}] protocol={0}")
@EnumSource(HttpProtocol.class)
void testConnectionFailsRequestBeforeWrite(HttpProtocol protocol) throws Exception {
connectionFilterFactory(client -> new StreamingHttpConnectionFilter(client) {
@Override
public Single<StreamingHttpResponse> request(StreamingHttpRequest request) {
return failed(DELIBERATE_EXCEPTION);
}
});
setUp(protocol);
ExecutionException e = assertThrows(ExecutionException.class, () -> makeRequestAndAssertResponse(SVC_ECHO, protocol, OK, CONTENT.readableBytes()));
assertThat(e.getCause(), sameInstance(DELIBERATE_EXCEPTION));
// server is not involved in this test, count down manually
bothTerminate.countDown();
bothTerminate.await();
clientInOrder.verify(clientLifecycleObserver).onNewExchange();
clientInOrder.verify(clientExchangeObserver).onRequest(any(StreamingHttpRequest.class));
clientInOrder.verify(clientExchangeObserver).onConnectionSelected(any(ConnectionInfo.class));
clientInOrder.verify(clientExchangeObserver).onResponseError(e.getCause());
clientInOrder.verify(clientExchangeObserver).onExchangeFinally();
verifyNoMoreInteractions(clientLifecycleObserver, clientExchangeObserver);
verifyNoInteractions(clientRequestObserver, clientResponseObserver, serverLifecycleObserver, serverExchangeObserver, serverRequestObserver, serverResponseObserver);
}
Aggregations