Search in sources :

Example 1 with RemoteHost

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

the class OriginRestrictionLoadBalancingStrategyTest method setUp.

@BeforeEach
public void setUp() {
    delegate = mock(LoadBalancer.class);
    when(delegate.choose(any(LoadBalancer.Preferences.class))).thenReturn(Optional.of(origins.get(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);
    log = new LoggingTestSupport(OriginRestrictionLoadBalancingStrategy.class);
}
Also used : Origin(com.hotels.styx.api.extension.Origin) ActiveOrigins(com.hotels.styx.api.extension.ActiveOrigins) RemoteHost(com.hotels.styx.api.extension.RemoteHost) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) LoggingTestSupport(com.hotels.styx.support.matchers.LoggingTestSupport) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with RemoteHost

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

the class OriginRestrictionLoadBalancingStrategyTest method logsInvalidPatterns.

@Test
public void logsInvalidPatterns() {
    Random mockRandom = mock(Random.class);
    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("*-01")));
    assertThat(log.lastMessage(), is(loggingEvent(ERROR, "Invalid origin restriction cookie value=.*, Cause=Dangling meta character .*")));
}
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)

Example 3 with RemoteHost

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

the class OriginRestrictionLoadBalancingStrategyTest method usesAllOriginsWhenRegularExpressionMatchesAll.

@Test
public void usesAllOriginsWhenRegularExpressionMatchesAll() {
    Random mockRandom = mock(Random.class);
    when(mockRandom.nextInt(any(Integer.class))).thenReturn(3);
    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);
    Optional<RemoteHost> chosenOne = strategy.choose(lbPreference(Optional.of(".*")));
    assertThat(chosenOne.get(), isOneOf(origins.get(3)));
    verify(mockRandom).nextInt(eq(7));
}
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)

Example 4 with RemoteHost

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

the class OriginRestrictionLoadBalancingStrategyTest method randomlyChoosesOneOfTheMatchingOrigins.

@Test
public void randomlyChoosesOneOfTheMatchingOrigins() {
    Random mockRandom = mock(Random.class);
    when(mockRandom.nextInt(5)).thenReturn(3);
    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);
    Optional<RemoteHost> chosenOne = strategy.choose(lbPreference(Optional.of("origin-[1-3], origin-(5|6)")));
    // The preferred origins are [1, 2, 3, 5, 6]
    // The index 3 (as chosen by rng) contains:
    assertThat(chosenOne.get().id(), is(id("origin-5")));
}
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)

Example 5 with RemoteHost

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

the class StyxBackendServiceClientTest method hostHeaderIsNotOverwrittenWhenOverrideHostHeaderIsFalse.

@Test
public void hostHeaderIsNotOverwrittenWhenOverrideHostHeaderIsFalse() {
    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();
    styxHttpClient.sendRequest(testRequest, requestContext);
    verify(httpHandler).handle(testRequest, requestContext);
}
Also used : Origin(com.hotels.styx.api.extension.Origin) RetryNTimes(com.hotels.styx.client.retry.RetryNTimes) Context(com.hotels.styx.api.HttpInterceptor.Context) HttpHandler(com.hotels.styx.api.HttpHandler) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) LoadBalancer(com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer) 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