Search in sources :

Example 26 with LiveHttpRequest

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

the class HttpRequestMessageLoggerTest method logsClientSideResponseDetailsShortFormat.

@Test
public void logsClientSideResponseDetailsShortFormat() {
    LiveHttpRequest styxRequest = get("http://www.hotels.com/foo/bar/request").build();
    LiveHttpResponse styxResponse = response(OK).build();
    new HttpRequestMessageLogger("com.hotels.styx.http-messages.outbound", false, httpMessageFormatter).logResponse(styxRequest, styxResponse);
    assertThat(log.lastMessage(), is(loggingEvent(INFO, format("requestId=%s, response=\\{version=HTTP/1.1, status=200 OK\\}", styxRequest.id()))));
}
Also used : HttpRequestMessageLogger(com.hotels.styx.common.logging.HttpRequestMessageLogger) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with LiveHttpRequest

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

the class HttpRequestOperationTest method shouldTransformStyxRequestToNettyRequestWithAllRelevantInformation.

@Test
public void shouldTransformStyxRequestToNettyRequestWithAllRelevantInformation() {
    LiveHttpRequest request = new LiveHttpRequest.Builder().method(GET).header("X-Forwarded-Proto", "https").cookies(requestCookie("HASESSION_V3", "asdasdasd"), requestCookie("has", "123456789")).uri("https://www.example.com/foo%2Cbar?foo,baf=2").build();
    DefaultHttpRequest nettyRequest = HttpRequestOperation.toNettyRequest(request);
    assertThat(nettyRequest.method(), is(io.netty.handler.codec.http.HttpMethod.GET));
    assertThat(nettyRequest.uri(), is("https://www.example.com/foo%2Cbar?foo%2Cbaf=2"));
    assertThat(nettyRequest.headers().get("X-Forwarded-Proto"), is("https"));
    assertThat(ServerCookieDecoder.LAX.decode(nettyRequest.headers().get("Cookie")), containsInAnyOrder(new DefaultCookie("HASESSION_V3", "asdasdasd"), new DefaultCookie("has", "123456789")));
}
Also used : DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) Test(org.junit.jupiter.api.Test)

Example 28 with LiveHttpRequest

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

the class StyxBackendServiceClientFactoryTest method usesTheOriginSpecifiedInTheOriginsRestrictionCookie.

@Test
public void usesTheOriginSpecifiedInTheOriginsRestrictionCookie() {
    MapBackedConfiguration config = new MapBackedConfiguration();
    config.set("originRestrictionCookie", ORIGINS_RESTRICTION_COOKIE);
    environment = new Environment.Builder().registry(new MicrometerRegistry(new SimpleMeterRegistry())).configuration(new StyxConfig(config)).build();
    BackendService backendService = newBackendServiceBuilder().origins(newOriginBuilder("localhost", 9091).id("x").build(), newOriginBuilder("localhost", 9092).id("y").build(), newOriginBuilder("localhost", 9093).id("z").build()).build();
    BackendServiceClient styxBackendServiceClient = new StyxBackendServiceClientFactory(environment).createClient(backendService, newOriginsInventoryBuilder(environment.centralisedMetrics(), backendService).hostClientFactory((pool) -> {
        if (pool.getOrigin().id().equals(id("x"))) {
            return hostClient(response(OK).header("X-Origin-Id", "x").build());
        } else if (pool.getOrigin().id().equals(id("y"))) {
            return hostClient(response(OK).header("X-Origin-Id", "y").build());
        } else {
            return hostClient(response(OK).header("X-Origin-Id", "z").build());
        }
    }).build(), new CachingOriginStatsFactory(environment.centralisedMetrics()));
    LiveHttpRequest requestz = get("/some-req").cookies(requestCookie(ORIGINS_RESTRICTION_COOKIE, id("z").toString())).build();
    LiveHttpRequest requestx = get("/some-req").cookies(requestCookie(ORIGINS_RESTRICTION_COOKIE, id("x").toString())).build();
    LiveHttpRequest requesty = get("/some-req").cookies(requestCookie(ORIGINS_RESTRICTION_COOKIE, id("y").toString())).build();
    LiveHttpResponse responsez = Mono.from(styxBackendServiceClient.sendRequest(requestz, requestContext())).block();
    LiveHttpResponse responsex = Mono.from(styxBackendServiceClient.sendRequest(requestx, requestContext())).block();
    LiveHttpResponse responsey = Mono.from(styxBackendServiceClient.sendRequest(requesty, requestContext())).block();
    assertThat(responsex.header("X-Origin-Id").get(), is("x"));
    assertThat(responsey.header("X-Origin-Id").get(), is("y"));
    assertThat(responsez.header("X-Origin-Id").get(), is("z"));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) StyxConfig(com.hotels.styx.StyxConfig) StyxBackendServiceClient(com.hotels.styx.client.StyxBackendServiceClient) BackendServiceClient(com.hotels.styx.client.BackendServiceClient) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) OriginsInventory.newOriginsInventoryBuilder(com.hotels.styx.client.OriginsInventory.newOriginsInventoryBuilder) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) BackendService.newBackendServiceBuilder(com.hotels.styx.api.extension.service.BackendService.newBackendServiceBuilder) StickySessionConfig.newStickySessionConfigBuilder(com.hotels.styx.api.extension.service.StickySessionConfig.newStickySessionConfigBuilder) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MapBackedConfiguration(com.hotels.styx.api.configuration.Configuration.MapBackedConfiguration) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 29 with LiveHttpRequest

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

use of com.hotels.styx.api.LiveHttpRequest 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)

Aggregations

LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)82 Test (org.junit.jupiter.api.Test)76 Condition (com.hotels.styx.server.routing.Condition)19 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)14 HttpHandler (com.hotels.styx.api.HttpHandler)10 MicrometerRegistry (com.hotels.styx.api.MicrometerRegistry)9 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)9 MeterRegistry (com.hotels.styx.api.MeterRegistry)8 BackendService (com.hotels.styx.api.extension.service.BackendService)7 Registry (com.hotels.styx.api.extension.service.spi.Registry)7 Eventual (com.hotels.styx.api.Eventual)6 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)6 HttpResponse (com.hotels.styx.api.HttpResponse)5 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)5 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)5 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Mono (reactor.core.publisher.Mono)5 HttpRequestMessageLogger (com.hotels.styx.common.logging.HttpRequestMessageLogger)4 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)4