use of com.hotels.styx.api.HttpResponse in project styx by ExpediaGroup.
the class WiremockResponseConverterTest method convertsResponseWithBody.
@Test
public void convertsResponseWithBody() {
ResponseDefinition response = new ResponseDefinition(HTTP_OK, "{ \"count\" : 0, \"requestJournalDisabled\" : false}");
response.setHeaders(new HttpHeaders(httpHeader("Transfer-Encoding", "chunked"), httpHeader("Content-Type", "application/json")));
HttpResponse styxResponse = toStyxResponse(new BasicResponseRenderer().render(response));
assertThat(styxResponse.status(), is(OK));
Map<String, String> actual = headersAsMap(styxResponse);
assertThat(actual, is(Map.of("Transfer-Encoding", "chunked", "Content-Type", "application/json")));
assertThat(styxResponse.bodyAs(UTF_8), is("{ \"count\" : 0, \"requestJournalDisabled\" : false}"));
assertThat(headerCount(styxResponse.headers()), is(2));
}
use of com.hotels.styx.api.HttpResponse in project styx by ExpediaGroup.
the class WiremockResponseConverterTest method convertsCreatedResponse.
@Test
public void convertsCreatedResponse() {
ResponseDefinition created = ResponseDefinition.created();
Response render = new BasicResponseRenderer().render(created);
HttpResponse styxResponse = toStyxResponse(render);
assertThat(styxResponse.status(), is(CREATED));
assertThat(styxResponse.bodyAs(UTF_8), is(""));
assertThat(headerCount(styxResponse.headers()), is(0));
}
use of com.hotels.styx.api.HttpResponse 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.HttpResponse 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.HttpResponse in project styx by ExpediaGroup.
the class HttpMethodFilteringHandlerTest method failsIfRequestMethodIsNotSupported.
@Test
public void failsIfRequestMethodIsNotSupported() throws Exception {
WebServiceHandler handler = mock(WebServiceHandler.class);
HttpMethodFilteringHandler post = new HttpMethodFilteringHandler(GET, handler);
HttpRequest request = post("/some-uri").build();
HttpResponse response = Mono.from(post.handle(request, requestContext())).block();
assertThat(response.status(), is(METHOD_NOT_ALLOWED));
}
Aggregations