Search in sources :

Example 1 with Id

use of com.hotels.styx.api.Id 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 2 with Id

use of com.hotels.styx.api.Id 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 3 with Id

use of com.hotels.styx.api.Id in project styx by ExpediaGroup.

the class OriginHealthStatusMonitorFactoryTest method createsScheduledOriginStatusMonitor.

@Test
public void createsScheduledOriginStatusMonitor() {
    HealthCheckConfig healthCheckConfig = newHealthCheckConfigBuilder().uri("/version.txt").interval(5, MILLISECONDS).build();
    OriginHealthCheckFunction checkFunction = (client, origin, callback) -> {
    };
    assertThat(factory.create(id, healthCheckConfig, () -> checkFunction, mock(HttpClient.class)), is(instanceOf(AnomalyExcludingOriginHealthStatusMonitor.class)));
}
Also used : HealthCheckConfig.newHealthCheckConfigBuilder(com.hotels.styx.api.extension.service.HealthCheckConfig.newHealthCheckConfigBuilder) IsInstanceOf.instanceOf(org.hamcrest.core.IsInstanceOf.instanceOf) GENERIC_APP(com.hotels.styx.api.Id.GENERIC_APP) HttpClient(com.hotels.styx.client.HttpClient) NoOriginHealthStatusMonitor(com.hotels.styx.client.healthcheck.monitors.NoOriginHealthStatusMonitor) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) AnomalyExcludingOriginHealthStatusMonitor(com.hotels.styx.client.healthcheck.monitors.AnomalyExcludingOriginHealthStatusMonitor) Test(org.junit.jupiter.api.Test) OriginHealthStatusMonitorFactory(com.hotels.styx.client.healthcheck.OriginHealthStatusMonitorFactory) OriginHealthCheckFunction(com.hotels.styx.client.healthcheck.OriginHealthCheckFunction) Is.is(org.hamcrest.core.Is.is) HealthCheckConfig(com.hotels.styx.api.extension.service.HealthCheckConfig) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Id(com.hotels.styx.api.Id) Mockito.mock(org.mockito.Mockito.mock) OriginHealthCheckFunction(com.hotels.styx.client.healthcheck.OriginHealthCheckFunction) HealthCheckConfig(com.hotels.styx.api.extension.service.HealthCheckConfig) Test(org.junit.jupiter.api.Test)

Example 4 with Id

use of com.hotels.styx.api.Id in project styx by ExpediaGroup.

the class IdTest method checkEquality.

@Test
public void checkEquality() {
    Id base = id("one");
    Id same = id("one");
    Id different = id("two");
    assertThat(base.equals(null), is(false));
    assertThat(base.equals(base), is(true));
    assertThat(base.equals(same), is(true));
    assertThat(base.equals(different), is(false));
}
Also used : Id(com.hotels.styx.api.Id) Test(org.junit.jupiter.api.Test)

Example 5 with Id

use of com.hotels.styx.api.Id in project styx by ExpediaGroup.

the class OriginsInventory method handleSetOriginsEvent.

private void handleSetOriginsEvent(SetOriginsEvent event) {
    Map<Id, Origin> newOriginsMap = event.newOrigins.stream().collect(toMap(Origin::id, o -> o));
    OriginChanges originChanges = new OriginChanges();
    concat(this.origins.keySet().stream(), newOriginsMap.keySet().stream()).collect(toSet()).forEach(originId -> {
        Origin origin = newOriginsMap.get(originId);
        if (isNewOrigin(originId, origin)) {
            MonitoredOrigin monitoredOrigin = addMonitoredEndpoint(origin);
            originChanges.addOrReplaceOrigin(originId, monitoredOrigin);
        } else if (isUpdatedOrigin(originId, origin)) {
            MonitoredOrigin monitoredOrigin = changeMonitoredEndpoint(origin);
            originChanges.addOrReplaceOrigin(originId, monitoredOrigin);
        } else if (isUnchangedOrigin(originId, origin)) {
            LOG.info("Existing origin has been left unchanged. Origin={}:{}", appId, origin);
            originChanges.keepExistingOrigin(originId, this.origins.get(originId));
        } else if (isRemovedOrigin(originId, origin)) {
            removeMonitoredEndpoint(originId);
            originChanges.noteRemovedOrigin();
        }
    });
    this.origins = originChanges.updatedOrigins();
    if (originChanges.changed()) {
        notifyStateChange();
    }
}
Also used : EnableOrigin(com.hotels.styx.client.origincommands.EnableOrigin) Origin(com.hotels.styx.api.extension.Origin) DisableOrigin(com.hotels.styx.client.origincommands.DisableOrigin) StyxFutures.await(com.hotels.styx.common.StyxFutures.await) QueueDrainingEventProcessor(com.hotels.styx.common.QueueDrainingEventProcessor) RemoteHost.remoteHost(com.hotels.styx.api.extension.RemoteHost.remoteHost) ActiveOrigins(com.hotels.styx.api.extension.ActiveOrigins) Collections.singleton(java.util.Collections.singleton) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Objects.isNull(java.util.Objects.isNull) Collectors.toSet(java.util.stream.Collectors.toSet) HttpHandler(com.hotels.styx.api.HttpHandler) EventProcessor(com.hotels.styx.common.EventProcessor) ConnectionPools.simplePoolFactory(com.hotels.styx.client.connectionpool.ConnectionPools.simplePoolFactory) Collection(java.util.Collection) OriginsChangeListener(com.hotels.styx.api.extension.OriginsChangeListener) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) DISABLED(com.hotels.styx.client.OriginsInventory.OriginState.DISABLED) EnableOrigin(com.hotels.styx.client.origincommands.EnableOrigin) StateMachine(com.hotels.styx.common.StateMachine) List(java.util.List) Origin(com.hotels.styx.api.extension.Origin) Objects.nonNull(java.util.Objects.nonNull) INACTIVE(com.hotels.styx.client.OriginsInventory.OriginState.INACTIVE) DisableOrigin(com.hotels.styx.client.origincommands.DisableOrigin) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) NoOriginHealthStatusMonitor(com.hotels.styx.client.healthcheck.monitors.NoOriginHealthStatusMonitor) ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) OriginHealthStatusMonitor(com.hotels.styx.client.healthcheck.OriginHealthStatusMonitor) EventBus(com.google.common.eventbus.EventBus) Deleter(com.hotels.styx.metrics.Deleter) Objects.requireNonNull(java.util.Objects.requireNonNull) OriginsSnapshot(com.hotels.styx.api.extension.OriginsSnapshot) BackendService(com.hotels.styx.api.extension.service.BackendService) Subscribe(com.google.common.eventbus.Subscribe) Stream.concat(java.util.stream.Stream.concat) ACTIVE(com.hotels.styx.client.OriginsInventory.OriginState.ACTIVE) Collections.emptyMap(java.util.Collections.emptyMap) Eventual(com.hotels.styx.api.Eventual) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) Preconditions.checkArgument(com.hotels.styx.common.Preconditions.checkArgument) GetOriginsInventorySnapshot(com.hotels.styx.client.origincommands.GetOriginsInventorySnapshot) RemoteHost(com.hotels.styx.api.extension.RemoteHost) Collectors.toList(java.util.stream.Collectors.toList) Closeable(java.io.Closeable) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) CentralisedMetrics(com.hotels.styx.metrics.CentralisedMetrics) Announcer(com.hotels.styx.api.extension.Announcer) Id(com.hotels.styx.api.Id) Id(com.hotels.styx.api.Id)

Aggregations

Id (com.hotels.styx.api.Id)7 Origin (com.hotels.styx.api.extension.Origin)5 Test (org.junit.jupiter.api.Test)5 Eventual (com.hotels.styx.api.Eventual)3 HttpHandler (com.hotels.styx.api.HttpHandler)3 GENERIC_APP (com.hotels.styx.api.Id.GENERIC_APP)3 NoAvailableHostsException (com.hotels.styx.api.exceptions.NoAvailableHostsException)3 OriginUnreachableException (com.hotels.styx.api.exceptions.OriginUnreachableException)3 RemoteHost (com.hotels.styx.api.extension.RemoteHost)3 RemoteHost.remoteHost (com.hotels.styx.api.extension.RemoteHost.remoteHost)3 BackendService (com.hotels.styx.api.extension.service.BackendService)3 CentralisedMetrics (com.hotels.styx.metrics.CentralisedMetrics)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Mockito.mock (org.mockito.Mockito.mock)3 EventBus (com.google.common.eventbus.EventBus)2 HostAndPort (com.google.common.net.HostAndPort)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