use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method updatedRequestWithUpdatedHostHeaderIsPresentInResponseWhenOverrideHostHeaderIsTrue.
@Test
public void updatedRequestWithUpdatedHostHeaderIsPresentInResponseWhenOverrideHostHeaderIsTrue() {
HttpInterceptor.Context requestContext = requestContext();
StyxHostHttpClient hostClient = mock(StyxHostHttpClient.class);
HttpHandler httpHandler = mock(HttpHandler.class);
Origin origin = newOriginBuilder(updatedHostName, 9090).applicationId(GENERIC_APP).build();
RemoteHost remoteHost = remoteHost(origin, httpHandler, hostClient);
LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost));
when(httpHandler.handle(any(), any())).thenReturn(Eventual.of(testResponse));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).originStatsFactory(mock(OriginStatsFactory.class)).originsRestrictionCookieName("someCookie").originIdHeader("origin-id").loadBalancer(loadBalancer).retryPolicy(new RetryNTimes(0)).metrics(metrics).overrideHostHeader(true).build();
Publisher<LiveHttpResponse> responsePublisher = styxHttpClient.sendRequest(testRequest, requestContext);
StepVerifier.create(responsePublisher).expectNextMatches(it -> it.request().header(HttpHeaderNames.HOST).isPresent() && it.request().header(HttpHeaderNames.HOST).get().equals(updatedHostName)).verifyComplete();
}
use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method incrementsResponseStatusMetricsFor501.
@Test
public void incrementsResponseStatusMetricsFor501() {
StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(NOT_IMPLEMENTED).build()));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))).build();
LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(SOME_REQ, requestContext())).block();
assertThat(response.status(), is(NOT_IMPLEMENTED));
verify(hostClient).sendRequest(eq(SOME_REQ), any(Context.class));
assertThat(meterRegistry.get("proxy.client.responseCode.errorStatus").tag("statusCode", "501").counter(), is(notNullValue()));
}
use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method originalRequestIsPresentInResponseWhenOverrideHostHeaderIsFalse.
@Test
public void originalRequestIsPresentInResponseWhenOverrideHostHeaderIsFalse() {
HttpInterceptor.Context requestContext = requestContext();
StyxHostHttpClient hostClient = mock(StyxHostHttpClient.class);
HttpHandler httpHandler = mock(HttpHandler.class);
Origin origin = newOriginBuilder(incomingHostname, 9090).applicationId(GENERIC_APP).build();
RemoteHost remoteHost = remoteHost(origin, httpHandler, hostClient);
LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost));
when(httpHandler.handle(any(), any())).thenReturn(Eventual.of(testResponse));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).originStatsFactory(mock(OriginStatsFactory.class)).originsRestrictionCookieName("someCookie").originIdHeader("origin-id").loadBalancer(loadBalancer).retryPolicy(new RetryNTimes(0)).metrics(metrics).overrideHostHeader(false).build();
Publisher<LiveHttpResponse> responsePublisher = styxHttpClient.sendRequest(testRequest, requestContext);
StepVerifier.create(responsePublisher).expectNextMatches(it -> it.request().header(HttpHeaderNames.HOST).isPresent() && it.request().header(HttpHeaderNames.HOST).get().equals(incomingHostname)).verifyComplete();
}
use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.
the class StyxBackendServiceClientTest method incrementsResponseStatusMetricsFor401.
@Test
public void incrementsResponseStatusMetricsFor401() {
StyxHostHttpClient hostClient = mockHostClient(Flux.just(response(UNAUTHORIZED).build()));
StyxBackendServiceClient styxHttpClient = new StyxBackendServiceClient.Builder(backendService.id()).metrics(metrics).loadBalancer(mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))).build();
LiveHttpResponse response = Mono.from(styxHttpClient.sendRequest(SOME_REQ, requestContext())).block();
assertThat(response.status(), is(UNAUTHORIZED));
verify(hostClient).sendRequest(eq(SOME_REQ), any(Context.class));
assertThat(meterRegistry.get("proxy.client.responseCode.errorStatus").tag("statusCode", "401").counter(), is(notNullValue()));
}
use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.
the class HttpRequestOperation method execute.
public Flux<LiveHttpResponse> execute(NettyConnection nettyConnection) {
AtomicReference<RequestBodyChunkSubscriber> requestRequestBodyChunkSubscriber = new AtomicReference<>();
requestTime = System.currentTimeMillis();
executeCount.incrementAndGet();
Flux<LiveHttpResponse> responseFlux = Flux.create(sink -> {
if (nettyConnection.isConnected()) {
RequestBodyChunkSubscriber bodyChunkSubscriber = new RequestBodyChunkSubscriber(request, nettyConnection);
requestRequestBodyChunkSubscriber.set(bodyChunkSubscriber);
addProxyBridgeHandlers(nettyConnection, sink);
new WriteRequestToOrigin(sink, nettyConnection, request, bodyChunkSubscriber).write();
if (requestLoggingEnabled) {
httpRequestMessageLogger.logRequest(request, nettyConnection.getOrigin());
}
} else {
sink.error(new TransportLostException(nettyConnection.channel(), nettyConnection.getOrigin()));
}
});
if (requestLoggingEnabled) {
responseFlux = responseFlux.doOnNext(response -> {
httpRequestMessageLogger.logResponse(request, response);
});
}
return responseFlux.map(response -> Requests.doFinally(response, cause -> {
if (nettyConnection.isConnected()) {
removeProxyBridgeHandlers(nettyConnection);
if (requestIsOngoing(requestRequestBodyChunkSubscriber.get())) {
LOGGER.warn("Origin responded too quickly to an ongoing request, or it was cancelled. Connection={}, Request={}.", new Object[] { nettyConnection.channel(), this.request });
nettyConnection.close();
}
}
}));
}
Aggregations