Search in sources :

Example 31 with HttpResponse

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));
}
Also used : HttpHeaders(com.github.tomakehurst.wiremock.http.HttpHeaders) BasicResponseRenderer(com.github.tomakehurst.wiremock.http.BasicResponseRenderer) HttpResponse(com.hotels.styx.api.HttpResponse) ResponseDefinition(com.github.tomakehurst.wiremock.http.ResponseDefinition) Test(org.junit.jupiter.api.Test)

Example 32 with HttpResponse

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));
}
Also used : HttpResponse(com.hotels.styx.api.HttpResponse) WiremockResponseConverter.toStyxResponse(com.hotels.styx.servers.WiremockResponseConverter.toStyxResponse) Response(com.github.tomakehurst.wiremock.http.Response) BasicResponseRenderer(com.github.tomakehurst.wiremock.http.BasicResponseRenderer) HttpResponse(com.hotels.styx.api.HttpResponse) ResponseDefinition(com.github.tomakehurst.wiremock.http.ResponseDefinition) Test(org.junit.jupiter.api.Test)

Example 33 with HttpResponse

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);
}
Also used : Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) Plugin(com.hotels.styx.api.plugins.spi.Plugin) PluginFactory(com.hotels.styx.api.plugins.spi.PluginFactory) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Map(java.util.Map) Collections(java.util.Collections) Function(java.util.function.Function) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 34 with HttpResponse

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());
}
Also used : Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) Logger(org.slf4j.Logger) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) PUT(com.hotels.styx.api.HttpMethod.PUT) HttpResponse.response(com.hotels.styx.api.HttpResponse.response) NOT_FOUND(com.hotels.styx.api.HttpResponseStatus.NOT_FOUND) HttpRequest(com.hotels.styx.api.HttpRequest) ArrayList(java.util.ArrayList) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) List(java.util.List) Matcher(java.util.regex.Matcher) Collectors.toMap(java.util.stream.Collectors.toMap) WebServiceHandler(com.hotels.styx.api.WebServiceHandler) Map(java.util.Map) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Pattern(java.util.regex.Pattern) DELETE(com.hotels.styx.api.HttpMethod.DELETE) LinkedList(java.util.LinkedList) GET(com.hotels.styx.api.HttpMethod.GET) POST(com.hotels.styx.api.HttpMethod.POST) HttpMethod(com.hotels.styx.api.HttpMethod) Matcher(java.util.regex.Matcher)

Example 35 with HttpResponse

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));
}
Also used : HttpRequest(com.hotels.styx.api.HttpRequest) HttpResponse(com.hotels.styx.api.HttpResponse) WebServiceHandler(com.hotels.styx.api.WebServiceHandler) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (com.hotels.styx.api.HttpResponse)107 Test (org.junit.jupiter.api.Test)99 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)30 HttpRequest (com.hotels.styx.api.HttpRequest)18 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Eventual (com.hotels.styx.api.Eventual)10 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)9 StyxHttpClient (com.hotels.styx.client.StyxHttpClient)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 TlsSettings (com.hotels.styx.api.extension.service.TlsSettings)7 StyxObjectRecord (com.hotels.styx.StyxObjectRecord)6 HttpHandler (com.hotels.styx.api.HttpHandler)6 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)6 OK (com.hotels.styx.api.HttpResponseStatus.OK)5 HttpClient (com.hotels.styx.client.HttpClient)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)4 Plugin (com.hotels.styx.api.plugins.spi.Plugin)4 PluginFactory (com.hotels.styx.api.plugins.spi.PluginFactory)4 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)3