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")));
}
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"));
}
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"));
}
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());
}
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();
}
Aggregations