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