Search in sources :

Example 1 with NOT_FOUND

use of com.hotels.styx.api.HttpResponseStatus.NOT_FOUND 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 2 with NOT_FOUND

use of com.hotels.styx.api.HttpResponseStatus.NOT_FOUND in project styx by ExpediaGroup.

the class UrlRequestHealthCheckTest method declaresOriginUnhealthyOnNon200Ok.

@Test
public void declaresOriginUnhealthyOnNon200Ok() {
    HttpClient client = request -> respondWith(NOT_FOUND);
    new UrlRequestHealthCheck("/version.txt", metrics).check(client, someOrigin, state -> this.originState = state);
    assertThat(originState, is(UNHEALTHY));
    assertThat(metrics.getRegistry().find("proxy.client.originHealthCheckFailures").tags("originId", someOrigin.id().toString(), "appId", someOrigin.applicationId().toString()).counter().count(), is(1.0));
    assertThat(meters(id -> id.getName().equals("proxy.client.originHealthCheckFailures")).size(), is(1));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpResponse(com.hotels.styx.api.HttpResponse) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) OriginState(com.hotels.styx.client.healthcheck.OriginHealthCheckFunction.OriginState) Predicate(java.util.function.Predicate) HttpResponse.response(com.hotels.styx.api.HttpResponse.response) NOT_FOUND(com.hotels.styx.api.HttpResponseStatus.NOT_FOUND) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) Meter(io.micrometer.core.instrument.Meter) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClient(com.hotels.styx.client.HttpClient) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) Collectors(java.util.stream.Collectors) UNHEALTHY(com.hotels.styx.client.healthcheck.OriginHealthCheckFunction.OriginState.UNHEALTHY) Test(org.junit.jupiter.api.Test) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Origin(com.hotels.styx.api.extension.Origin) OK(com.hotels.styx.api.HttpResponseStatus.OK) CentralisedMetrics(com.hotels.styx.metrics.CentralisedMetrics) Matchers.is(org.hamcrest.Matchers.is) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) HEALTHY(com.hotels.styx.client.healthcheck.OriginHealthCheckFunction.OriginState.HEALTHY) HttpClient(com.hotels.styx.client.HttpClient) Test(org.junit.jupiter.api.Test)

Example 3 with NOT_FOUND

use of com.hotels.styx.api.HttpResponseStatus.NOT_FOUND in project styx by ExpediaGroup.

the class UrlRequestHealthCheckTest method sendsTheHealthCheckRequestToTheGivenUrl.

@Test
public void sendsTheHealthCheckRequestToTheGivenUrl() {
    HttpClient client = request -> {
        requestedUrl = request.url().path();
        return respondWith(NOT_FOUND);
    };
    new UrlRequestHealthCheck("/version-foo.txt", metrics).check(client, someOrigin, state -> {
    });
    assertThat(requestedUrl, is("/version-foo.txt"));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpResponse(com.hotels.styx.api.HttpResponse) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) OriginState(com.hotels.styx.client.healthcheck.OriginHealthCheckFunction.OriginState) Predicate(java.util.function.Predicate) HttpResponse.response(com.hotels.styx.api.HttpResponse.response) NOT_FOUND(com.hotels.styx.api.HttpResponseStatus.NOT_FOUND) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) Meter(io.micrometer.core.instrument.Meter) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClient(com.hotels.styx.client.HttpClient) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) Collectors(java.util.stream.Collectors) UNHEALTHY(com.hotels.styx.client.healthcheck.OriginHealthCheckFunction.OriginState.UNHEALTHY) Test(org.junit.jupiter.api.Test) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Origin(com.hotels.styx.api.extension.Origin) OK(com.hotels.styx.api.HttpResponseStatus.OK) CentralisedMetrics(com.hotels.styx.metrics.CentralisedMetrics) Matchers.is(org.hamcrest.Matchers.is) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) HEALTHY(com.hotels.styx.client.healthcheck.OriginHealthCheckFunction.OriginState.HEALTHY) HttpClient(com.hotels.styx.client.HttpClient) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (com.hotels.styx.api.HttpResponse)3 HttpResponse.response (com.hotels.styx.api.HttpResponse.response)3 NOT_FOUND (com.hotels.styx.api.HttpResponseStatus.NOT_FOUND)3 List (java.util.List)3 HttpResponseStatus (com.hotels.styx.api.HttpResponseStatus)2 OK (com.hotels.styx.api.HttpResponseStatus.OK)2 MicrometerRegistry (com.hotels.styx.api.MicrometerRegistry)2 Origin (com.hotels.styx.api.extension.Origin)2 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)2 HttpClient (com.hotels.styx.client.HttpClient)2 OriginState (com.hotels.styx.client.healthcheck.OriginHealthCheckFunction.OriginState)2 HEALTHY (com.hotels.styx.client.healthcheck.OriginHealthCheckFunction.OriginState.HEALTHY)2 UNHEALTHY (com.hotels.styx.client.healthcheck.OriginHealthCheckFunction.OriginState.UNHEALTHY)2 CentralisedMetrics (com.hotels.styx.metrics.CentralisedMetrics)2 Meter (io.micrometer.core.instrument.Meter)2 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Predicate (java.util.function.Predicate)2 Collectors (java.util.stream.Collectors)2 Collectors.toList (java.util.stream.Collectors.toList)2