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())));
}
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"));
}
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"));
}
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"));
}
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"));
}
Aggregations