Search in sources :

Example 16 with HttpHandler

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

the class StandardHttpPipelineTest method sendsExceptionUponMultipleSubscription.

@Test
public void sendsExceptionUponMultipleSubscription() {
    HttpHandler handler = (request, context) -> Eventual.of(response(OK).build());
    StandardHttpPipeline pipeline = new StandardHttpPipeline(handler);
    Eventual<LiveHttpResponse> responseObservable = pipeline.handle(get("/").build(), requestContext());
    LiveHttpResponse response = Mono.from(responseObservable).block();
    assertThat(response.status(), is(OK));
    assertThrows(IllegalStateException.class, () -> Mono.from(responseObservable).block());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 17 with HttpHandler

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

the class StaticPipelineBuilderTest method appliesPluginsInOrderTheyAreConfigured.

@Test
public void appliesPluginsInOrderTheyAreConfigured() throws Exception {
    Iterable<NamedPlugin> plugins = List.of(interceptor("Test-A", appendResponseHeader("X-From-Plugin", "A")), interceptor("Test-B", appendResponseHeader("X-From-Plugin", "B")));
    HttpHandler handler = new StaticPipelineFactory(clientFactory, environment, registry, plugins, executor, false).build();
    LiveHttpResponse response = Mono.from(handler.handle(get("/foo").build(), requestContext())).block();
    assertThat(response.status(), is(OK));
    assertThat(response.headers("X-From-Plugin"), hasItems("B", "A"));
}
Also used : HttpHandler(com.hotels.styx.api.HttpHandler) NamedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 18 with HttpHandler

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

the class StyxBackendServiceClientTest method hostHeaderIsOverwrittenWhenOverrideHostHeaderIsTrue.

@Test
public void hostHeaderIsOverwrittenWhenOverrideHostHeaderIsTrue() {
    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();
    styxHttpClient.sendRequest(testRequest, requestContext);
    ArgumentCaptor<LiveHttpRequest> updatedRequest = ArgumentCaptor.forClass(LiveHttpRequest.class);
    verify(httpHandler).handle(updatedRequest.capture(), eq(requestContext));
    assertThat(updatedRequest.getValue().header(HttpHeaderNames.HOST).get(), is(updatedHostName));
}
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) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) 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)

Example 19 with HttpHandler

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

the class MockOriginServer method newHandler.

private static HttpHandler newHandler(String originId, RequestHandler wireMockHandler) {
    return (httpRequest, ctx) -> httpRequest.aggregate(MAX_CONTENT_LENGTH).map(fullRequest -> {
        LOGGER.info("{} received: {}\n{}", new Object[] { originId, fullRequest.url(), fullRequest.body() });
        return fullRequest;
    }).flatMap(fullRequest -> {
        Request wmRequest = new WiremockStyxRequestAdapter(fullRequest);
        com.github.tomakehurst.wiremock.http.Response wmResponse = wireMockHandler.handle(wmRequest);
        return Eventual.of(toStyxResponse(wmResponse).stream());
    });
}
Also used : UrlMatchingStrategy(com.github.tomakehurst.wiremock.client.UrlMatchingStrategy) RequestHandler(com.github.tomakehurst.wiremock.http.RequestHandler) FILES_ROOT(com.github.tomakehurst.wiremock.WireMockServer.FILES_ROOT) ServiceManager(com.google.common.util.concurrent.ServiceManager) WireMockConfiguration(com.github.tomakehurst.wiremock.core.WireMockConfiguration) LoggerFactory(org.slf4j.LoggerFactory) StubRequestHandler(com.github.tomakehurst.wiremock.http.StubRequestHandler) Optional.absent(com.google.common.base.Optional.absent) MappingBuilder(com.github.tomakehurst.wiremock.client.MappingBuilder) AdminRequestHandler(com.github.tomakehurst.wiremock.http.AdminRequestHandler) HttpConnectorConfig(com.hotels.styx.server.HttpConnectorConfig) WireMock(com.github.tomakehurst.wiremock.client.WireMock) NotImplementedRequestDelayControl(com.github.tomakehurst.wiremock.global.NotImplementedRequestDelayControl) Unpooled(io.netty.buffer.Unpooled) StyxServers(com.hotels.styx.StyxServers) ByteBuf(io.netty.buffer.ByteBuf) BasicResponseRenderer(com.github.tomakehurst.wiremock.http.BasicResponseRenderer) WireMockApp(com.github.tomakehurst.wiremock.core.WireMockApp) Request(com.github.tomakehurst.wiremock.http.Request) WireMock.configureFor(com.github.tomakehurst.wiremock.client.WireMock.configureFor) Collections.emptyMap(java.util.Collections.emptyMap) Eventual(com.hotels.styx.api.Eventual) Logger(org.slf4j.Logger) WiremockResponseConverter.toStyxResponse(com.hotels.styx.servers.WiremockResponseConverter.toStyxResponse) RequestPatternBuilder(com.github.tomakehurst.wiremock.client.RequestPatternBuilder) ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) HttpHandler(com.hotels.styx.api.HttpHandler) UTF_8(java.nio.charset.StandardCharsets.UTF_8) HttpsConnectorConfig(com.hotels.styx.server.HttpsConnectorConfig) HttpServers(com.hotels.styx.server.HttpServers) List(java.util.List) StubResponseRenderer(com.github.tomakehurst.wiremock.http.StubResponseRenderer) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) InetServer(com.hotels.styx.InetServer) ProxyResponseRenderer(com.github.tomakehurst.wiremock.http.ProxyResponseRenderer) Request(com.github.tomakehurst.wiremock.http.Request)

Example 20 with HttpHandler

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

the class BackendServicesRouterTest method selectsUsingSingleSlashPath.

@Test
public void selectsUsingSingleSlashPath() throws Exception {
    Registry.Changes<BackendService> changes = added(appA().newCopy().path("/").build(), appB().newCopy().path("/appB/hotel/details.html").build());
    BackendServicesRouter router = new BackendServicesRouter(serviceClientFactory, environment, executor);
    router.onChange(changes);
    LiveHttpRequest request = get("/").build();
    Optional<HttpHandler> route = router.route(request, context);
    assertThat(proxyTo(route, request).header(ORIGIN_ID_DEFAULT), isValue(APP_A));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Registry(com.hotels.styx.api.extension.service.spi.Registry) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) MeterRegistry(com.hotels.styx.api.MeterRegistry) Test(org.junit.jupiter.api.Test)

Aggregations

HttpHandler (com.hotels.styx.api.HttpHandler)23 Test (org.junit.jupiter.api.Test)20 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)13 MeterRegistry (com.hotels.styx.api.MeterRegistry)11 MicrometerRegistry (com.hotels.styx.api.MicrometerRegistry)11 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)10 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)9 Eventual (com.hotels.styx.api.Eventual)8 BackendService (com.hotels.styx.api.extension.service.BackendService)8 Registry (com.hotels.styx.api.extension.service.spi.Registry)8 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)7 OK (com.hotels.styx.api.HttpResponseStatus.OK)6 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)6 Mono (reactor.core.publisher.Mono)6 LiveHttpRequest.get (com.hotels.styx.api.LiveHttpRequest.get)5 LiveHttpResponse.response (com.hotels.styx.api.LiveHttpResponse.response)5 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)5 Support.requestContext (com.hotels.styx.support.Support.requestContext)5 Arrays.asList (java.util.Arrays.asList)5 Matchers.is (org.hamcrest.Matchers.is)5