Search in sources :

Example 6 with HttpRequest

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

the class UrlRequestHealthCheck method check.

@Override
public void check(HttpClient client, Origin origin, OriginHealthCheckFunction.Callback responseCallback) {
    HttpRequest request = newHealthCheckRequestFor(origin);
    client.send(request).handle((response, cause) -> {
        if (response != null) {
            if (response.status().equals(OK)) {
                responseCallback.originStateResponse(HEALTHY);
            } else {
                meterCache.get(origin).increment();
                responseCallback.originStateResponse(UNHEALTHY);
            }
        } else if (cause != null) {
            meterCache.get(origin).increment();
            responseCallback.originStateResponse(UNHEALTHY);
        }
        return null;
    });
}
Also used : HttpRequest(com.hotels.styx.api.HttpRequest)

Example 7 with HttpRequest

use of com.hotels.styx.api.HttpRequest 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 8 with HttpRequest

use of com.hotels.styx.api.HttpRequest 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)

Example 9 with HttpRequest

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

the class StaticFileOnRealServerIT method shouldWorkInRealServer.

@Test
public void shouldWorkInRealServer() throws Exception {
    writeFile("index.html", "Hello World");
    writeFile("foo.js", "some js");
    mkdir("some/dir");
    writeFile("some/dir/content1.txt", "some txt");
    HttpRequest request = new HttpRequest.Builder(GET, "/index.html").header("Host", serverEndpoint).build();
    HttpResponse response = await(client.sendRequest(request));
    assertThat(response.bodyAs(UTF_8), is("Hello World"));
}
Also used : HttpRequest(com.hotels.styx.api.HttpRequest) HttpResponse(com.hotels.styx.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 10 with HttpRequest

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

the class ProtocolMetricsTest method doRequest.

private static HttpResponse doRequest(HttpClient client, String protocol, int port, String path) {
    String url = format("%s://localhost:%s%s", protocol, port, startWithSlash(path));
    HttpRequest request = get(url).body("foobarbaz", UTF_8).build();
    return await(client.sendRequest(request));
}
Also used : HttpRequest(com.hotels.styx.api.HttpRequest)

Aggregations

HttpRequest (com.hotels.styx.api.HttpRequest)22 Test (org.junit.jupiter.api.Test)19 HttpResponse (com.hotels.styx.api.HttpResponse)16 StyxObjectRecord (com.hotels.styx.StyxObjectRecord)5 WebServiceHandler (com.hotels.styx.api.WebServiceHandler)3 StyxObjectDefinition (com.hotels.styx.routing.config.StyxObjectDefinition)2 StyxObjectStore (com.hotels.styx.routing.db.StyxObjectStore)2 Eventual (com.hotels.styx.api.Eventual)1 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)1 HttpMethod (com.hotels.styx.api.HttpMethod)1 DELETE (com.hotels.styx.api.HttpMethod.DELETE)1 GET (com.hotels.styx.api.HttpMethod.GET)1 POST (com.hotels.styx.api.HttpMethod.POST)1 PUT (com.hotels.styx.api.HttpMethod.PUT)1 HttpResponse.response (com.hotels.styx.api.HttpResponse.response)1 INTERNAL_SERVER_ERROR (com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR)1 NOT_FOUND (com.hotels.styx.api.HttpResponseStatus.NOT_FOUND)1 Support.requestContext (com.hotels.styx.support.Support.requestContext)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1