Search in sources :

Example 51 with LiveHttpRequest

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

the class StyxHttpClient method sendRequestInternal.

// Visible for testing
static Mono<LiveHttpResponse> sendRequestInternal(NettyConnectionFactory connectionFactory, LiveHttpRequest request, Builder params) {
    LiveHttpRequest networkRequest = addUserAgent(params.userAgent(), request);
    Origin origin = originFromRequest(networkRequest, params.https());
    SslContext sslContext = getSslContext(params.https(), params.tlsSettings());
    return connectionFactory.createConnection(origin, new ConnectionSettings(params.connectTimeoutMillis()), sslContext).flatMap(connection -> Mono.from(connection.write(networkRequest).doOnComplete(connection::close).doOnError(e -> connection.close()).map(response -> response.newBuilder().body(it -> it.doOnEnd(x -> connection.close()).doOnCancel(() -> connection.close())).build())));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) HttpResponse(com.hotels.styx.api.HttpResponse) SslContext(io.netty.handler.ssl.SslContext) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) Preconditions.checkArgument(com.hotels.styx.common.Preconditions.checkArgument) NettyExecutor(com.hotels.styx.NettyExecutor) Mono(reactor.core.publisher.Mono) CompletableFuture(java.util.concurrent.CompletableFuture) HostAndPort(com.google.common.net.HostAndPort) HttpRequest(com.hotels.styx.api.HttpRequest) Url(com.hotels.styx.api.Url) TlsSettings(com.hotels.styx.api.extension.service.TlsSettings) TimeUnit(java.util.concurrent.TimeUnit) HttpConfig.newHttpConfigBuilder(com.hotels.styx.client.HttpConfig.newHttpConfigBuilder) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) SslContextFactory(com.hotels.styx.client.ssl.SslContextFactory) Origin(com.hotels.styx.api.extension.Origin) Objects.requireNonNull(java.util.Objects.requireNonNull) USER_AGENT(com.hotels.styx.api.HttpHeaderNames.USER_AGENT) Optional(java.util.Optional) HOST(com.hotels.styx.api.HttpHeaderNames.HOST) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) NettyConnectionFactory(com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory) Builder.httpRequestOperationFactoryBuilder(com.hotels.styx.client.HttpRequestOperationFactory.Builder.httpRequestOperationFactoryBuilder) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) SslContext(io.netty.handler.ssl.SslContext)

Example 52 with LiveHttpRequest

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

the class EarlyReturnExamplePluginTest method returnsEarlyWhenHeaderIsPresent.

@Test
public void returnsEarlyWhenHeaderIsPresent() {
    EarlyReturnExamplePlugin plugin = new EarlyReturnExamplePlugin();
    LiveHttpRequest request = LiveHttpRequest.get("/").header("X-Respond", "foo").build();
    HttpInterceptor.Chain chain = request1 -> Eventual.of(LiveHttpResponse.response().build());
    Eventual<LiveHttpResponse> eventualLive = plugin.intercept(request, chain);
    Eventual<HttpResponse> eventual = eventualLive.flatMap(response -> response.aggregate(100));
    HttpResponse response = Mono.from(eventual).block();
    assertThat(response.bodyAs(UTF_8), is("Responding from plugin"));
}
Also used : HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Mono(reactor.core.publisher.Mono) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 53 with LiveHttpRequest

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

the class ModifyContentByAggregationExamplePluginTest method modifiesContent.

@Test
public void modifiesContent() {
    // Set up
    Config config = new Config("MyExtraText");
    ModifyContentByAggregationExamplePlugin plugin = new ModifyContentByAggregationExamplePlugin(config);
    LiveHttpRequest request = get("/").build();
    HttpInterceptor.Chain chain = anyRequest -> Eventual.of(response().body("OriginalBody", UTF_8).build().stream());
    // Execution
    HttpResponse response = Mono.from(plugin.intercept(request, chain).flatMap(liveResponse -> liveResponse.aggregate(100))).block();
    // Assertion
    assertThat(response.bodyAs(UTF_8), is("OriginalBodyMyExtraText"));
}
Also used : HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Config(com.hotels.styx.ModifyContentByAggregationExamplePlugin.Config) HttpResponse.response(com.hotels.styx.api.HttpResponse.response) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) Mono(reactor.core.publisher.Mono) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Config(com.hotels.styx.ModifyContentByAggregationExamplePlugin.Config) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) HttpResponse(com.hotels.styx.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 54 with LiveHttpRequest

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

the class ModifyHeadersExamplePluginTest method addsExtraHeaders.

/**
 * This tests the behaviours added in the ModifyHeadersExamplePlugin.
 */
@Test
public void addsExtraHeaders() {
    // a simple way to mock the downstream system
    HttpInterceptor.Chain chain = request -> {
        assertThat(request.header("myRequestHeader").orElse(null), is("foo"));
        return Eventual.of(response(OK).build());
    };
    // an example request you expect your plugin to receive
    LiveHttpRequest request = get("/foo").build();
    // since this is a test, we want to wait for the response
    LiveHttpResponse response = Mono.from(plugin.intercept(request, chain)).block();
    assertThat(response.header("myResponseHeader").orElse(null), is("bar"));
}
Also used : HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Eventual(com.hotels.styx.api.Eventual) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) OK(com.hotels.styx.api.HttpResponseStatus.OK) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) Mono(reactor.core.publisher.Mono) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 55 with LiveHttpRequest

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

the class ReplaceLiveContentExamplePluginTest method replacesLiveContent.

@Test
public void replacesLiveContent() {
    // Set up
    ReplaceLiveContentExampleConfig config = new ReplaceLiveContentExampleConfig("myNewContent");
    ReplaceLiveContentExamplePlugin plugin = new ReplaceLiveContentExamplePlugin(config);
    LiveHttpRequest request = get("/").build();
    HttpInterceptor.Chain chain = request1 -> Eventual.of(LiveHttpResponse.response().build());
    // Execution
    HttpResponse response = Mono.from(plugin.intercept(request, chain).flatMap(liveResponse -> liveResponse.aggregate(100))).block();
    // Assertion
    assertThat(response.bodyAs(UTF_8), is("myNewContent"));
}
Also used : HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) Mono(reactor.core.publisher.Mono) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) 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