Search in sources :

Example 1 with Context

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"));
}
Also used : Buffer(com.hotels.styx.api.Buffer) ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) ByteStream(com.hotels.styx.api.ByteStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 2 with Context

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"));
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with Context

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"));
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) Test(org.junit.jupiter.api.Test)

Example 4 with Context

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"));
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 5 with Context

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"));
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Context (com.hotels.styx.api.HttpInterceptor.Context)7 ConnectionPool (com.hotels.styx.client.connectionpool.ConnectionPool)7 HttpInterceptorContext (com.hotels.styx.server.HttpInterceptorContext)7 Support.requestContext (com.hotels.styx.support.Support.requestContext)7 Test (org.junit.jupiter.api.Test)7 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Buffer (com.hotels.styx.api.Buffer)1 ByteStream (com.hotels.styx.api.ByteStream)1 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)1 Subscription (org.reactivestreams.Subscription)1