Search in sources :

Example 6 with HttpHandler

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

the class MockOriginServer method newHandler.

private static HttpHandler newHandler(RequestHandler wireMockHandler) {
    return (httpRequest, ctx) -> httpRequest.aggregate(MAX_CONTENT_LENGTH).map(fullRequest -> {
        LOGGER.info("Received: {}\n{}", new Object[] { 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 7 with HttpHandler

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

the class BackendServicesRouterTest method removesExistingServicesBeforeAddingNewOnes.

@Test
public void removesExistingServicesBeforeAddingNewOnes() throws Exception {
    BackendServicesRouter router = new BackendServicesRouter(serviceClientFactory, environment, executor);
    router.onChange(added(appB()));
    router.onChange(new Registry.Changes.Builder<BackendService>().added(newBackendServiceBuilder(appB()).id("X").build()).removed(appB()).build());
    LiveHttpRequest request = get("/appB/").build();
    Optional<HttpHandler> route = router.route(request, context);
    assertThat(proxyTo(route, request).header(ORIGIN_ID_DEFAULT), isValue("X"));
}
Also used : HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) BackendService.newBackendServiceBuilder(com.hotels.styx.api.extension.service.BackendService.newBackendServiceBuilder) 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)

Example 8 with HttpHandler

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

the class BackendServicesRouterTest method selectsUsingPathWithNoSubsequentCharacters.

@Test
public void selectsUsingPathWithNoSubsequentCharacters() throws Exception {
    Registry.Changes<BackendService> changes = added(appA().newCopy().path("/").build(), appB().newCopy().path("/appB/").build());
    BackendServicesRouter router = new BackendServicesRouter(serviceClientFactory, environment, executor);
    router.onChange(changes);
    LiveHttpRequest request = get("/appB/").build();
    Optional<HttpHandler> route = router.route(request, context);
    assertThat(proxyTo(route, request).header(ORIGIN_ID_DEFAULT), isValue(APP_B));
}
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)

Example 9 with HttpHandler

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

the class BackendServicesRouterTest method selectsApplicationBasedOnPathIfAppsAreProvidedInOppositeOrder.

@Test
public void selectsApplicationBasedOnPathIfAppsAreProvidedInOppositeOrder() throws Exception {
    Registry.Changes<BackendService> changes = added(appB().newCopy().path("/appB/hotel/details.html").build(), appA().newCopy().path("/").build());
    BackendServicesRouter router = new BackendServicesRouter(serviceClientFactory, environment, executor);
    router.onChange(changes);
    LiveHttpRequest request = get("/appB/hotel/details.html").build();
    Optional<HttpHandler> route = router.route(request, context);
    assertThat(proxyTo(route, request).header(ORIGIN_ID_DEFAULT), isValue(APP_B));
}
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)

Example 10 with HttpHandler

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

the class BackendServicesRouterTest method updatesRoutesOnBackendServicesChange.

@Test
public void updatesRoutesOnBackendServicesChange() throws Exception {
    BackendServicesRouter router = new BackendServicesRouter(serviceClientFactory, environment, executor);
    LiveHttpRequest request = get("/appB/").build();
    router.onChange(added(appB()));
    Optional<HttpHandler> route = router.route(request, context);
    assertThat(proxyTo(route, request).header(ORIGIN_ID_DEFAULT), isValue(APP_B));
    router.onChange(new Registry.Changes.Builder<BackendService>().build());
    Optional<HttpHandler> route2 = router.route(request, context);
    assertThat(proxyTo(route2, request).header(ORIGIN_ID_DEFAULT), isValue(APP_B));
}
Also used : HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) BackendService.newBackendServiceBuilder(com.hotels.styx.api.extension.service.BackendService.newBackendServiceBuilder) 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