Search in sources :

Example 81 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project riposte by Nike-Inc.

the class EndpointMetricsHandlerDefaultImplTest method handleRequest_works_as_expected_for_request_that_hit_an_endpoint.

@DataProvider(value = { "GET    |   99", "GET    |   142", "GET    |   242", "GET    |   342", "GET    |   404", "GET    |   405", "GET    |   442", "GET    |   500", "GET    |   542", "GET    |   600", "POST   |   99", "POST   |   142", "POST   |   242", "POST   |   342", "POST   |   404", "POST   |   405", "POST   |   442", "POST   |   500", "POST   |   542", "POST   |   600", "PUT    |   99", "PUT    |   142", "PUT    |   242", "PUT    |   342", "PUT    |   404", "PUT    |   405", "PUT    |   442", "PUT    |   500", "PUT    |   542", "PUT    |   600", "DELETE |   99", "DELETE |   142", "DELETE |   242", "DELETE |   342", "DELETE |   404", "DELETE |   405", "DELETE |   442", "DELETE |   500", "DELETE |   542", "DELETE |   600", "PATCH  |   99", "PATCH  |   142", "PATCH  |   242", "PATCH  |   342", "PATCH  |   404", "PATCH  |   405", "PATCH  |   442", "PATCH  |   500", "PATCH  |   542", "PATCH  |   600", "null   |   99", "null   |   142", "null   |   242", "null   |   342", "null   |   404", "null   |   405", "null   |   442", "null   |   500", "null   |   542", "null   |   600" }, splitBy = "\\|")
@Test
public void handleRequest_works_as_expected_for_request_that_hit_an_endpoint(String requestMethodStr, int responseStatusCode) throws InterruptedException {
    // given
    HttpMethod requestMethod = (requestMethodStr == null) ? null : HttpMethod.valueOf(requestMethodStr);
    doReturn(requestMethod).when(requestInfoMock).getMethod();
    Endpoint<?> endpoint = serverConfig.appEndpoints().iterator().next();
    String matchingPathTemplate = "/" + UUID.randomUUID().toString();
    state.setEndpointForExecution(endpoint, matchingPathTemplate);
    String endpointTimerAndMeterKey = instance.getTimerAndMeterMapKeyForEndpoint(endpoint);
    int responseHttpStatusCodeXXValue = responseStatusCode / 100;
    long elapsedTimeMillis = 42;
    // when
    instance.handleRequest(requestInfoMock, responseInfoMock, state, responseStatusCode, responseHttpStatusCodeXXValue, elapsedTimeMillis);
    // then
    // The all-requests timer should be updated with the elapsed time of the request
    verify(instance.requests).update(elapsedTimeMillis, TimeUnit.MILLISECONDS);
    // The timer for the relevant HTTP method for this request should be updated with the elapsed time of the request
    verify(expectedRequestTimer(requestMethod, instance)).update(elapsedTimeMillis, TimeUnit.MILLISECONDS);
    {
        // The timer for the endpoint for this request should be updated with the elapsed time of the request
        Timer expectedEndpointTimerUsed = instance.endpointRequestsTimers.get(endpointTimerAndMeterKey);
        assertThat(expectedEndpointTimerUsed).isNotNull();
        verify(expectedEndpointTimerUsed).update(elapsedTimeMillis, TimeUnit.MILLISECONDS);
    }
    final int httpStatusCodeXXValue = responseStatusCode / 100;
    if (httpStatusCodeXXValue >= 1 && httpStatusCodeXXValue <= 5) {
        // Inside the normal 1xx-5xx response codes.
        // The correct 1xx, 2xx, 3xx, 4xx, or 5xx meter for all requests should be marked.
        verify(instance.responses[httpStatusCodeXXValue - 1]).mark();
        // The correct 1xx, 2xx, 3xx, 4xx, or 5xx meter for this request's endpoint should be marked.
        Meter[] endpointResponseMeterArray = instance.endpointResponsesMeters.get(endpointTimerAndMeterKey);
        assertThat(endpointResponseMeterArray).isNotNull();
        verify(endpointResponseMeterArray[httpStatusCodeXXValue - 1]).mark();
    } else {
        // Outside the normal 1xx-5xx response codes, so none of the response meters should have been modified.
        instance.endpointResponsesMeters.values().forEach(meterArray -> Stream.of(meterArray).forEach(Mockito::verifyNoInteractions));
    }
}
Also used : Timer(com.codahale.metrics.Timer) Meter(com.codahale.metrics.Meter) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HttpMethod(io.netty.handler.codec.http.HttpMethod) Endpoint(com.nike.riposte.server.http.Endpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 82 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project riposte by Nike-Inc.

the class CodahaleMetricsListenerTest method onEvent_works_as_expected_for_RESPONSE_SENT_with_endpoint.

@DataProvider(value = { "GET    |   99", "GET    |   142", "GET    |   242", "GET    |   342", "GET    |   404", "GET    |   405", "GET    |   442", "GET    |   500", "GET    |   542", "GET    |   600", "POST   |   99", "POST   |   142", "POST   |   242", "POST   |   342", "POST   |   404", "POST   |   405", "POST   |   442", "POST   |   500", "POST   |   542", "POST   |   600", "PUT    |   99", "PUT    |   142", "PUT    |   242", "PUT    |   342", "PUT    |   404", "PUT    |   405", "PUT    |   442", "PUT    |   500", "PUT    |   542", "PUT    |   600", "DELETE |   99", "DELETE |   142", "DELETE |   242", "DELETE |   342", "DELETE |   404", "DELETE |   405", "DELETE |   442", "DELETE |   500", "DELETE |   542", "DELETE |   600", "PATCH  |   99", "PATCH  |   142", "PATCH  |   242", "PATCH  |   342", "PATCH  |   404", "PATCH  |   405", "PATCH  |   442", "PATCH  |   500", "PATCH  |   542", "PATCH  |   600", "null   |   99", "null   |   142", "null   |   242", "null   |   342", "null   |   404", "null   |   405", "null   |   442", "null   |   500", "null   |   542", "null   |   600" }, splitBy = "\\|")
@Test
public void onEvent_works_as_expected_for_RESPONSE_SENT_with_endpoint(String requestMethodStr, int responseStatusCode) throws InterruptedException {
    // given
    ServerMetricsEvent event = ServerMetricsEvent.RESPONSE_SENT;
    HttpMethod requestMethod = (requestMethodStr == null) ? null : HttpMethod.valueOf(requestMethodStr);
    doReturn(requestMethod).when(requestInfoMock).getMethod();
    Endpoint<?> endpoint = serverConfig.appEndpoints().iterator().next();
    String matchingPathTemplate = "/" + UUID.randomUUID().toString();
    state.setEndpointForExecution(endpoint, matchingPathTemplate);
    doReturn(responseStatusCode).when(responseInfoMock).getHttpStatusCodeWithDefault(ResponseSender.DEFAULT_HTTP_STATUS_CODE);
    int requestRawContentLengthBytes = (int) (Math.random() * 10000);
    doReturn(requestRawContentLengthBytes).when(requestInfoMock).getRawContentLengthInBytes();
    long finalResponseContentLength = (long) (Math.random() * 10000);
    doReturn(finalResponseContentLength).when(responseInfoMock).getFinalContentLength();
    Thread.sleep((long) (Math.random() * 25));
    // when
    long beforeCallTimeNanos = System.nanoTime();
    listener.onEvent(event, state);
    long afterCallTimeNanos = System.nanoTime();
    // then
    // Inflight requests counter decremented
    verify(listener.inflightRequests).dec();
    // Processed requests counter incremented
    verify(listener.processedRequests).inc();
    // If response code is greater than or equal to 400, then the failed requests counter should be incremented.
    if (responseStatusCode >= 400)
        verify(listener.failedRequests).inc();
    // Request and response size histograms should be updated with the relevant values from the request and response.
    verify(listener.requestSizes).update(requestRawContentLengthBytes);
    verify(listener.responseSizes).update(finalResponseContentLength);
    // Response end time should be set correctly
    assertThat(state.getResponseEndTimeNanos()).isBetween(beforeCallTimeNanos, afterCallTimeNanos);
    // The EndpointMetricsHandler should have been notified
    int responseHttpStatusCodeXXValue = responseStatusCode / 100;
    ArgumentCaptor<Long> elapsedTimeMillisArgCaptor = ArgumentCaptor.forClass(Long.class);
    verify(endpointMetricsHandlerMock).handleRequest(eq(requestInfoMock), eq(responseInfoMock), eq(state), eq(responseStatusCode), eq(responseHttpStatusCodeXXValue), elapsedTimeMillisArgCaptor.capture());
    long expectedElapsedTimeNanos = state.getResponseEndTimeNanos() - state.getRequestStartTimeNanos();
    assertThat(elapsedTimeMillisArgCaptor.getValue()).isEqualTo(NANOSECONDS.toMillis(expectedElapsedTimeNanos));
}
Also used : ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ServerMetricsEvent(com.nike.riposte.server.metrics.ServerMetricsEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HttpMethod(io.netty.handler.codec.http.HttpMethod) Endpoint(com.nike.riposte.server.http.Endpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 83 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project riposte by Nike-Inc.

the class HttpServletRequestWrapperForRequestInfoTest method getMethod_delegates_to_requestInfo.

@DataProvider(value = { "null", "GET", "POST", "OPTIONS" }, splitBy = "\\|")
@Test
public void getMethod_delegates_to_requestInfo(String methodName) {
    // given
    HttpMethod method = (methodName == null) ? null : HttpMethod.valueOf(methodName);
    doReturn(method).when(requestInfoMock).getMethod();
    // when
    String result = wrapper.getMethod();
    // then
    assertThat(result).isEqualTo(methodName);
}
Also used : HttpMethod(io.netty.handler.codec.http.HttpMethod) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 84 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project riposte by Nike-Inc.

the class RiposteWingtipsNettyClientTagAdapterTest method getRequestHttpMethod_works_as_expected.

@Test
public void getRequestHttpMethod_works_as_expected() {
    // given
    HttpMethod expectedResult = HttpMethod.valueOf(UUID.randomUUID().toString());
    doReturn(expectedResult).when(requestMock).method();
    // when
    String result = adapterSpy.getRequestHttpMethod(requestMock);
    // then
    assertThat(result).isEqualTo(expectedResult.name());
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Example 85 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project riposte by Nike-Inc.

the class RiposteWingtipsServerTagAdapterTest method getRequestHttpMethod_works_as_expected.

@Test
public void getRequestHttpMethod_works_as_expected() {
    // given
    HttpMethod expectedResult = HttpMethod.valueOf(UUID.randomUUID().toString());
    doReturn(expectedResult).when(requestMock).getMethod();
    // when
    String result = adapterSpy.getRequestHttpMethod(requestMock);
    // then
    assertThat(result).isEqualTo(expectedResult.name());
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Aggregations

HttpMethod (io.netty.handler.codec.http.HttpMethod)95 Test (org.junit.Test)51 HttpRequest (io.netty.handler.codec.http.HttpRequest)28 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)25 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)21 URI (java.net.URI)15 IOException (java.io.IOException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)11 HttpVersion (io.netty.handler.codec.http.HttpVersion)11 DefaultHttpClient (org.jocean.http.client.impl.DefaultHttpClient)11 TestChannelCreator (org.jocean.http.client.impl.TestChannelCreator)11 TestChannelPool (org.jocean.http.client.impl.TestChannelPool)11 DefaultSignalClient (org.jocean.http.rosa.impl.DefaultSignalClient)11 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)11 Subscription (rx.Subscription)11 Action2 (rx.functions.Action2)11 ByteBuf (io.netty.buffer.ByteBuf)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)10 Map (java.util.Map)9