use of com.hotels.styx.api.HttpInterceptor.Context in project styx by ExpediaGroup.
the class StyxHostHttpClientTest method terminatesConnectionDueToUnsubscribedBody.
@Test
public void terminatesConnectionDueToUnsubscribedBody() {
TestPublisher<Buffer> testPublisher = TestPublisher.create();
Connection connection = mockConnection(just(LiveHttpResponse.response(OK).body(new ByteStream(testPublisher)).build()));
ConnectionPool pool = mockPool(connection);
Context context = mockContext();
AtomicReference<LiveHttpResponse> receivedResponse = new AtomicReference<>();
StyxHostHttpClient hostClient = new StyxHostHttpClient(pool);
StepVerifier.create(hostClient.sendRequest(request, context)).consumeNextWith(receivedResponse::set).expectComplete().verify();
StepVerifier.create(receivedResponse.get().body()).thenCancel().verify();
verify(pool).closeConnection(any(Connection.class));
verify(context).add(ORIGINID_CONTEXT_KEY, Id.id("mockorigin"));
}
use of com.hotels.styx.api.HttpInterceptor.Context in project styx by ExpediaGroup.
the class StyxHostHttpClientTest method ignoresCancelledHeaders.
@Test
public void ignoresCancelledHeaders() {
// Request observable unsubscribe/cancel has to be ignored after "onNext" event.
// This is because Reactor Mono will automatically cancel after an event has
// been published.
Connection connection = mockConnection(just(response));
ConnectionPool pool = mockPool(connection);
Context context = mockContext();
AtomicReference<LiveHttpResponse> transformedResponse = new AtomicReference<>();
StyxHostHttpClient hostClient = new StyxHostHttpClient(pool);
// The StepVerifier consumes the response event and then unsubscribes
// from the response observable.
StepVerifier.create(hostClient.sendRequest(request, context)).consumeNextWith(transformedResponse::set).verifyComplete();
// The response is still alive...
verify(pool, never()).returnConnection(any(Connection.class));
verify(pool, never()).closeConnection(any(Connection.class));
// ... until response body is consumed:
transformedResponse.get().consume();
// Finally, the connection is returned after the response body is fully consumed:
verify(pool).returnConnection(any(Connection.class));
verify(context).add(ORIGINID_CONTEXT_KEY, Id.id("mockorigin"));
}
use of com.hotels.styx.api.HttpInterceptor.Context in project styx by ExpediaGroup.
the class StyxHostHttpClientTest method releasesConnectionWhenResponseFailsBeforeHeaders.
@Test
public void releasesConnectionWhenResponseFailsBeforeHeaders() {
Connection connection = mockConnection(Flux.error(new RuntimeException()));
ConnectionPool pool = mockPool(connection);
Context context = mockContext();
StyxHostHttpClient hostClient = new StyxHostHttpClient(pool);
StepVerifier.create(hostClient.sendRequest(request, context)).expectNextCount(0).expectError().verify();
verify(pool).closeConnection(any(Connection.class));
verify(context).add(ORIGINID_CONTEXT_KEY, Id.id("mockorigin"));
}
use of com.hotels.styx.api.HttpInterceptor.Context in project styx by ExpediaGroup.
the class StyxHostHttpClientTest method releasesIfRequestIsCancelledBeforeHeaders.
@Test
public void releasesIfRequestIsCancelledBeforeHeaders() {
Connection connection = mockConnection(EmitterProcessor.create());
ConnectionPool pool = mockPool(connection);
Context context = mockContext();
StyxHostHttpClient hostClient = new StyxHostHttpClient(pool);
AtomicReference<Subscription> subscription = new AtomicReference<>();
Flux.from(hostClient.sendRequest(request, context)).subscribe(new BaseSubscriber<LiveHttpResponse>() {
@Override
protected void hookOnSubscribe(Subscription s) {
super.hookOnSubscribe(s);
s.request(1);
subscription.set(s);
}
});
subscription.get().cancel();
verify(pool).closeConnection(any(Connection.class));
verify(context).add(ORIGINID_CONTEXT_KEY, Id.id("mockorigin"));
}
use of com.hotels.styx.api.HttpInterceptor.Context in project styx by ExpediaGroup.
the class StyxHostHttpClientTest method ignoresResponseObservableErrorsAfterHeaders.
@Test
public void ignoresResponseObservableErrorsAfterHeaders() {
Connection connection = mockConnection(responseProvider);
ConnectionPool pool = mockPool(connection);
Context context = mockContext();
AtomicReference<LiveHttpResponse> newResponse = new AtomicReference<>();
StyxHostHttpClient hostClient = new StyxHostHttpClient(pool);
StepVerifier.create(hostClient.sendRequest(request, context)).then(() -> {
responseProvider.onNext(response);
responseProvider.onError(new RuntimeException("oh dear ..."));
}).consumeNextWith(newResponse::set).expectError().verify();
newResponse.get().consume();
verify(pool).returnConnection(any(Connection.class));
verify(context).add(ORIGINID_CONTEXT_KEY, Id.id("mockorigin"));
}
Aggregations