Search in sources :

Example 6 with RemoteHost

use of com.hotels.styx.api.extension.RemoteHost 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();
}
Also used : Origin(com.hotels.styx.api.extension.Origin) RetryNTimes(com.hotels.styx.client.retry.RetryNTimes) BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) Processor(org.reactivestreams.Processor) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) RemoteHost.remoteHost(com.hotels.styx.api.extension.RemoteHost.remoteHost) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) Arrays.asList(java.util.Arrays.asList) Matchers.eq(org.mockito.Matchers.eq) StickySessionConfig.stickySessionDisabled(com.hotels.styx.api.extension.service.StickySessionConfig.stickySessionDisabled) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) BAD_REQUEST(com.hotels.styx.api.HttpResponseStatus.BAD_REQUEST) HttpHandler(com.hotels.styx.api.HttpHandler) OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) HttpHeaderNames(com.hotels.styx.api.HttpHeaderNames) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) Test(org.junit.jupiter.api.Test) Matchers.any(org.mockito.Matchers.any) CHUNKED(com.hotels.styx.api.HttpHeaderNames.CHUNKED) CONTENT_LENGTH(com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Mockito.inOrder(org.mockito.Mockito.inOrder) Origin(com.hotels.styx.api.extension.Origin) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) RequestCookie.requestCookie(com.hotels.styx.api.RequestCookie.requestCookie) Arrays.stream(java.util.Arrays.stream) Mockito.mock(org.mockito.Mockito.mock) RetryPolicy(com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy) GENERIC_APP(com.hotels.styx.api.Id.GENERIC_APP) StickySessionConfig(com.hotels.styx.api.extension.service.StickySessionConfig) NOT_IMPLEMENTED(com.hotels.styx.api.HttpResponseStatus.NOT_IMPLEMENTED) UNAUTHORIZED(com.hotels.styx.api.HttpResponseStatus.UNAUTHORIZED) TRANSFER_ENCODING(com.hotels.styx.api.HttpHeaderNames.TRANSFER_ENCODING) ArgumentCaptor(org.mockito.ArgumentCaptor) RetryNTimes(com.hotels.styx.client.retry.RetryNTimes) NoAvailableHostsException(com.hotels.styx.api.exceptions.NoAvailableHostsException) EmitterProcessor(reactor.core.publisher.EmitterProcessor) BackendService(com.hotels.styx.api.extension.service.BackendService) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Eventual(com.hotels.styx.api.Eventual) Matchers.empty(org.hamcrest.Matchers.empty) InOrder(org.mockito.InOrder) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) Publisher(org.reactivestreams.Publisher) Matchers(org.hamcrest.Matchers) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) HostAndPort(com.google.common.net.HostAndPort) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Mockito.verify(org.mockito.Mockito.verify) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Flux(reactor.core.publisher.Flux) Mockito.never(org.mockito.Mockito.never) MeterRegistry(com.hotels.styx.api.MeterRegistry) OK(com.hotels.styx.api.HttpResponseStatus.OK) CentralisedMetrics(com.hotels.styx.metrics.CentralisedMetrics) Id(com.hotels.styx.api.Id) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpHandler(com.hotels.styx.api.HttpHandler) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Context(com.hotels.styx.api.HttpInterceptor.Context) RemoteHost(com.hotels.styx.api.extension.RemoteHost) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test)

Example 7 with RemoteHost

use of com.hotels.styx.api.extension.RemoteHost 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();
}
Also used : Origin(com.hotels.styx.api.extension.Origin) RetryNTimes(com.hotels.styx.client.retry.RetryNTimes) BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) Processor(org.reactivestreams.Processor) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) RemoteHost.remoteHost(com.hotels.styx.api.extension.RemoteHost.remoteHost) Support.requestContext(com.hotels.styx.support.Support.requestContext) Context(com.hotels.styx.api.HttpInterceptor.Context) Arrays.asList(java.util.Arrays.asList) Matchers.eq(org.mockito.Matchers.eq) StickySessionConfig.stickySessionDisabled(com.hotels.styx.api.extension.service.StickySessionConfig.stickySessionDisabled) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) BAD_REQUEST(com.hotels.styx.api.HttpResponseStatus.BAD_REQUEST) HttpHandler(com.hotels.styx.api.HttpHandler) OriginUnreachableException(com.hotels.styx.api.exceptions.OriginUnreachableException) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) HttpHeaderNames(com.hotels.styx.api.HttpHeaderNames) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) Test(org.junit.jupiter.api.Test) Matchers.any(org.mockito.Matchers.any) CHUNKED(com.hotels.styx.api.HttpHeaderNames.CHUNKED) CONTENT_LENGTH(com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Mockito.inOrder(org.mockito.Mockito.inOrder) Origin(com.hotels.styx.api.extension.Origin) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) RequestCookie.requestCookie(com.hotels.styx.api.RequestCookie.requestCookie) Arrays.stream(java.util.Arrays.stream) Mockito.mock(org.mockito.Mockito.mock) RetryPolicy(com.hotels.styx.api.extension.retrypolicy.spi.RetryPolicy) GENERIC_APP(com.hotels.styx.api.Id.GENERIC_APP) StickySessionConfig(com.hotels.styx.api.extension.service.StickySessionConfig) NOT_IMPLEMENTED(com.hotels.styx.api.HttpResponseStatus.NOT_IMPLEMENTED) UNAUTHORIZED(com.hotels.styx.api.HttpResponseStatus.UNAUTHORIZED) TRANSFER_ENCODING(com.hotels.styx.api.HttpHeaderNames.TRANSFER_ENCODING) ArgumentCaptor(org.mockito.ArgumentCaptor) RetryNTimes(com.hotels.styx.client.retry.RetryNTimes) NoAvailableHostsException(com.hotels.styx.api.exceptions.NoAvailableHostsException) EmitterProcessor(reactor.core.publisher.EmitterProcessor) BackendService(com.hotels.styx.api.extension.service.BackendService) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Eventual(com.hotels.styx.api.Eventual) Matchers.empty(org.hamcrest.Matchers.empty) InOrder(org.mockito.InOrder) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) Publisher(org.reactivestreams.Publisher) Matchers(org.hamcrest.Matchers) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) HostAndPort(com.google.common.net.HostAndPort) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Mockito.verify(org.mockito.Mockito.verify) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Flux(reactor.core.publisher.Flux) Mockito.never(org.mockito.Mockito.never) MeterRegistry(com.hotels.styx.api.MeterRegistry) OK(com.hotels.styx.api.HttpResponseStatus.OK) CentralisedMetrics(com.hotels.styx.metrics.CentralisedMetrics) Id(com.hotels.styx.api.Id) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpHandler(com.hotels.styx.api.HttpHandler) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Context(com.hotels.styx.api.HttpInterceptor.Context) RemoteHost(com.hotels.styx.api.extension.RemoteHost) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test)

Example 8 with RemoteHost

use of com.hotels.styx.api.extension.RemoteHost in project styx by ExpediaGroup.

the class BusyConnectionsStrategyTest method copesWithOnlyOneActiveOrigin.

@Test
public void copesWithOnlyOneActiveOrigin() {
    RemoteHost hostOne = remoteHost(ORIGIN_ONE, mock(HttpHandler.class), lbMetrics(4));
    when(activeOrigins.snapshot()).thenReturn(asList(hostOne));
    Optional<RemoteHost> sortedPool = strategy.choose(null);
    assertThat(sortedPool, is(Optional.of(hostOne)));
}
Also used : HttpHandler(com.hotels.styx.api.HttpHandler) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Test(org.junit.jupiter.api.Test)

Example 9 with RemoteHost

use of com.hotels.styx.api.extension.RemoteHost in project styx by ExpediaGroup.

the class PowerOfTwoStrategyTest method returnsEmptyWhenNoOriginsAreAvailable.

@Test
public void returnsEmptyWhenNoOriginsAreAvailable() {
    ActiveOrigins activeOrigins = mock(ActiveOrigins.class);
    when(activeOrigins.snapshot()).thenReturn(asList());
    PowerOfTwoStrategy loadBalancer = new PowerOfTwoStrategy(activeOrigins, new Random(RNG_SEED));
    Optional<RemoteHost> chosenOne = loadBalancer.choose(mock(LoadBalancer.Preferences.class));
    assertThat(chosenOne, is(Optional.empty()));
}
Also used : ActiveOrigins(com.hotels.styx.api.extension.ActiveOrigins) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Random(java.util.Random) Test(org.junit.jupiter.api.Test)

Example 10 with RemoteHost

use of com.hotels.styx.api.extension.RemoteHost in project styx by ExpediaGroup.

the class OriginRestrictionLoadBalancingStrategyTest method dontDelegateWhenOriginRestrictionCookieMatches.

@Test
public void dontDelegateWhenOriginRestrictionCookieMatches() {
    Random mockRandom = mock(Random.class);
    when(mockRandom.nextInt(eq(1))).thenReturn(0);
    strategy = new OriginRestrictionLoadBalancingStrategy(new ActiveOrigins() {

        @Override
        public Iterable<RemoteHost> snapshot() {
            return origins;
        }

        @Override
        public String getApplicationId() {
            return null;
        }

        @Override
        public List<Origin> origins() {
            return null;
        }
    }, delegate, mockRandom);
    strategy.choose(lbPreference(Optional.of("origin-1")));
    strategy.choose(lbPreference(Optional.of("origin-1")));
    strategy.choose(lbPreference(Optional.of("origin-1")));
    strategy.choose(lbPreference(Optional.of("origin-1")));
    strategy.choose(lbPreference(Optional.of("origin-1")));
    strategy.choose(lbPreference(Optional.of("origin-1")));
    verify(delegate, never()).choose(any(LoadBalancer.Preferences.class));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) ActiveOrigins(com.hotels.styx.api.extension.ActiveOrigins) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Random(java.util.Random) Test(org.junit.jupiter.api.Test)

Aggregations

RemoteHost (com.hotels.styx.api.extension.RemoteHost)20 Test (org.junit.jupiter.api.Test)19 Origin (com.hotels.styx.api.extension.Origin)14 ActiveOrigins (com.hotels.styx.api.extension.ActiveOrigins)13 Random (java.util.Random)12 HttpHandler (com.hotels.styx.api.HttpHandler)7 LoadBalancer (com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer)5 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)4 Context (com.hotels.styx.api.HttpInterceptor.Context)4 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)4 RetryNTimes (com.hotels.styx.client.retry.RetryNTimes)4 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 HostAndPort (com.google.common.net.HostAndPort)2 Eventual (com.hotels.styx.api.Eventual)2 HttpHeaderNames (com.hotels.styx.api.HttpHeaderNames)2 CHUNKED (com.hotels.styx.api.HttpHeaderNames.CHUNKED)2 CONTENT_LENGTH (com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH)2 TRANSFER_ENCODING (com.hotels.styx.api.HttpHeaderNames.TRANSFER_ENCODING)2 HttpResponseStatus (com.hotels.styx.api.HttpResponseStatus)2