Search in sources :

Example 51 with LiveHttpResponse

use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.

the class StyxBackendServiceClientTest method prefersRestrictedOriginsOverStickyOriginsWhenBothAreConfigured.

@Test
public void prefersRestrictedOriginsOverStickyOriginsWhenBothAreConfigured() {
    Origin origin = originWithId("localhost:234", "App-X", "Origin-Y");
    StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(OK).build()));
    LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));
    StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).originsRestrictionCookieName("restrictedOrigin").loadBalancer(loadBalancer).metrics(metrics).build();
    LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(get("/foo").cookies(requestCookie("restrictedOrigin", "Origin-Y"), requestCookie("styx_origin_" + Id.GENERIC_APP, "Origin-X")).build(), requestContext())).block();
    assertThat(response.status(), is(OK));
    ArgumentCaptor<LoadBalancer.Preferences> argPreferences = ArgumentCaptor.forClass(LoadBalancer.Preferences.class);
    verify(loadBalancer).choose(argPreferences.capture());
    assertThat(argPreferences.getValue().preferredOrigins(), is(Optional.of("Origin-Y")));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 52 with LiveHttpResponse

use of com.hotels.styx.api.LiveHttpResponse 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 53 with LiveHttpResponse

use of com.hotels.styx.api.LiveHttpResponse 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)

Example 54 with LiveHttpResponse

use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.

the class NettyToStyxResponsePropagatorTest method doesNotPropagateErrorsTwice.

@Test
public void doesNotPropagateErrorsTwice() throws Exception {
    NettyToStyxResponsePropagator handler = new NettyToStyxResponsePropagator(responseSubscriber, SOME_ORIGIN);
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    channel.writeInbound(httpResponseHeaders);
    LiveHttpResponse response = verifyNextCalledOnResponseSubscriber();
    StepVerifier.create(response.body()).then(// Execute onSubscribe in FSM
    channel::runPendingTasks).then(// Will emit BadHttpResponseException
    () -> channel.pipeline().fireExceptionCaught(new RuntimeException())).then(// Will emit TransportLostException
    () -> channel.pipeline().fireChannelInactive()).expectError(BadHttpResponseException.class).verify();
    verify(responseSubscriber, atMostOnce()).error(any());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BadHttpResponseException(com.hotels.styx.client.BadHttpResponseException) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 55 with LiveHttpResponse

use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.

the class NettyToStyxResponsePropagatorTest method pushesContentWhenObserverSubscribes.

@Test
public void pushesContentWhenObserverSubscribes() throws Exception {
    EmbeddedChannel channel = new EmbeddedChannel(new NettyToStyxResponsePropagator(responseSubscriber, SOME_ORIGIN));
    channel.writeInbound(httpResponseHeaders);
    channel.writeInbound(httpContentOne);
    channel.writeInbound(httpContentTwo);
    LiveHttpResponse response = verifyNextCalledOnResponseSubscriber();
    StepVerifier.create(response.body()).then(channel::runPendingTasks).assertNext(buf -> assertEquals(FIRST_CHUNK, new String(buf.content()))).assertNext(buf -> assertEquals(SECOND_CHUNK, new String(buf.content()))).thenCancel();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CONNECTION(io.netty.handler.codec.http.HttpHeaderNames.CONNECTION) StepVerifier(reactor.test.StepVerifier) FluxSink(reactor.core.publisher.FluxSink) GENERIC_APP(com.hotels.styx.api.Id.GENERIC_APP) Unpooled.copiedBuffer(io.netty.buffer.Unpooled.copiedBuffer) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) Constructor(java.lang.reflect.Constructor) ArgumentCaptor(org.mockito.ArgumentCaptor) ByteBuf(io.netty.buffer.ByteBuf) CLOSE(io.netty.handler.codec.http.HttpHeaderValues.CLOSE) HTTP_1_1(io.netty.handler.codec.http.HttpVersion.HTTP_1_1) StyxClientException(com.hotels.styx.client.StyxClientException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) OutOfDirectMemoryError(io.netty.util.internal.OutOfDirectMemoryError) HttpContent(io.netty.handler.codec.http.HttpContent) UTF_8(java.nio.charset.StandardCharsets.UTF_8) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Mockito.times(org.mockito.Mockito.times) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) EMPTY_LAST_CONTENT(io.netty.handler.codec.http.LastHttpContent.EMPTY_LAST_CONTENT) Mockito.atMostOnce(org.mockito.Mockito.atMostOnce) ResponseTimeoutException(com.hotels.styx.api.exceptions.ResponseTimeoutException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Mockito.verify(org.mockito.Mockito.verify) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Test(org.junit.jupiter.api.Test) ALL_IDLE_STATE_EVENT(io.netty.handler.timeout.IdleStateEvent.ALL_IDLE_STATE_EVENT) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Matchers.any(org.mockito.Matchers.any) DecoderResult(io.netty.handler.codec.DecoderResult) IsOptional.isValue(com.hotels.styx.support.matchers.IsOptional.isValue) Mockito.never(org.mockito.Mockito.never) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) Origin(com.hotels.styx.api.extension.Origin) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ResponseCookie.responseCookie(com.hotels.styx.api.ResponseCookie.responseCookie) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) NettyToStyxResponsePropagator.toStyxResponse(com.hotels.styx.client.netty.connectionpool.NettyToStyxResponsePropagator.toStyxResponse) BadHttpResponseException(com.hotels.styx.client.BadHttpResponseException) OK(io.netty.handler.codec.http.HttpResponseStatus.OK) Mockito.mock(org.mockito.Mockito.mock) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)80 Test (org.junit.jupiter.api.Test)69 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)25 Support.requestContext (com.hotels.styx.support.Support.requestContext)21 Eventual (com.hotels.styx.api.Eventual)15 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)15 HttpHandler (com.hotels.styx.api.HttpHandler)14 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)14 Mono (reactor.core.publisher.Mono)14 ByteStream (com.hotels.styx.api.ByteStream)13 Context (com.hotels.styx.api.HttpInterceptor.Context)13 OK (com.hotels.styx.api.HttpResponseStatus.OK)13 LiveHttpResponse.response (com.hotels.styx.api.LiveHttpResponse.response)13 TransportLostException (com.hotels.styx.api.exceptions.TransportLostException)12 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 Matchers.is (org.hamcrest.Matchers.is)12 Buffer (com.hotels.styx.api.Buffer)11 LiveHttpRequest.get (com.hotels.styx.api.LiveHttpRequest.get)11 HttpInterceptorContext (com.hotels.styx.server.HttpInterceptorContext)11