use of com.hotels.styx.api.Eventual in project styx by ExpediaGroup.
the class TestPlugin method intercept.
@Override
public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) {
String header = xHcomPluginsHeader(request);
LiveHttpRequest newRequest = request.newBuilder().header(X_HCOM_PLUGINS_HEADER, header).header("X-Hcom-Styx-Started", styxStarted).header("X-Hcom-Styx-Stopped", styxStopped).build();
Function<ByteBuf, String> byteBufStringFunction = byteBuf -> byteBuf.toString(UTF_8);
return chain.proceed(newRequest).flatMap(response -> response.aggregate(1 * 1024 * 1024)).map(response -> response.newBuilder().header(X_HCOM_PLUGINS_HEADER, header).header("X-Hcom-Styx-Started", styxStarted).header("X-Hcom-Styx-Stopped", styxStopped).addHeader("X-Plugin-Identifier", config.getId()).build()).map(HttpResponse::stream);
}
use of com.hotels.styx.api.Eventual in project styx by ExpediaGroup.
the class UrlPatternRouter method handle.
@Override
public Eventual<HttpResponse> handle(HttpRequest request, HttpInterceptor.Context context) {
for (RouteDescriptor route : alternatives) {
if (request.method().equals(route.method())) {
Matcher match = route.uriPattern().matcher(request.path());
LOGGER.debug("Request path '{}' matching against route pattern '{}' matches: {}", new Object[] { request.path(), route.uriPattern(), match.matches() });
if (match.matches()) {
Map<String, String> placeholders = route.placeholderNames().stream().collect(toMap(name -> name, match::group));
context.add(PLACEHOLDERS_KEY, placeholders);
try {
return route.handler().handle(request, context);
} catch (Exception cause) {
LOGGER.error("ERROR: {} {}", new Object[] { request.method(), request.path(), cause });
return Eventual.of(response(INTERNAL_SERVER_ERROR).build());
}
}
}
}
return Eventual.of(response(NOT_FOUND).build());
}
use of com.hotels.styx.api.Eventual in project styx by ExpediaGroup.
the class StandardHttpPipelineTest method sendsExceptionUponExtraSubscriptionInsideInterceptor.
@ParameterizedTest
@MethodSource("multipleSubscriptionInterceptors")
public void sendsExceptionUponExtraSubscriptionInsideInterceptor(HttpInterceptor interceptor) throws Exception {
HttpHandler handler = (request, context) -> Eventual.of(response(OK).build());
List<HttpInterceptor> interceptors = singletonList(interceptor);
StandardHttpPipeline pipeline = new StandardHttpPipeline(interceptors, handler, RequestTracker.NO_OP);
Eventual<LiveHttpResponse> responseObservable = pipeline.handle(get("/").build(), requestContext());
assertThrows(IllegalStateException.class, () -> Mono.from(responseObservable).block());
}
use of com.hotels.styx.api.Eventual 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());
}
use of com.hotels.styx.api.Eventual 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"));
}
Aggregations