use of 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));
}
use of 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);
}
use of 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());
}
use of 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());
}
use of io.netty.handler.codec.http.HttpMethod in project riposte by Nike-Inc.
the class RequestInfoImplTest method netty_helper_constructor_populates_request_info_appropriately.
@Test
public void netty_helper_constructor_populates_request_info_appropriately() {
// given
String uri = "/some/uri/path/%24foobar%26?foo=bar&secondparam=secondvalue";
Map<String, List<String>> expectedQueryParamMap = new HashMap<>();
expectedQueryParamMap.put("foo", Arrays.asList("bar"));
expectedQueryParamMap.put("secondparam", Arrays.asList("secondvalue"));
HttpMethod method = HttpMethod.PATCH;
String cookieName = UUID.randomUUID().toString();
String cookieValue = UUID.randomUUID().toString();
String content = UUID.randomUUID().toString();
byte[] contentBytes = content.getBytes();
Charset contentCharset = CharsetUtil.UTF_8;
ByteBuf contentByteBuf = Unpooled.copiedBuffer(contentBytes);
HttpHeaders headers = new DefaultHttpHeaders().add("header1", "val1").add(HttpHeaders.Names.CONTENT_TYPE, contentCharset).add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE).add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookieName, cookieValue));
HttpHeaders trailingHeaders = new DefaultHttpHeaders().add("trailingHeader1", "trailingVal1");
HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
FullHttpRequest nettyRequestMock = mock(FullHttpRequest.class);
doReturn(uri).when(nettyRequestMock).uri();
doReturn(method).when(nettyRequestMock).method();
doReturn(headers).when(nettyRequestMock).headers();
doReturn(trailingHeaders).when(nettyRequestMock).trailingHeaders();
doReturn(contentByteBuf).when(nettyRequestMock).content();
doReturn(protocolVersion).when(nettyRequestMock).protocolVersion();
// when
RequestInfoImpl<?> requestInfo = new RequestInfoImpl<>(nettyRequestMock);
// then
assertThat("getUri was not the same value sent in", requestInfo.getUri(), is(uri));
assertThat("getPath did not decode as expected", requestInfo.getPath(), is("/some/uri/path/$foobar&"));
assertThat(requestInfo.getMethod(), is(method));
assertThat(requestInfo.getHeaders(), is(headers));
assertThat(requestInfo.getTrailingHeaders(), is(trailingHeaders));
assertThat(requestInfo.getQueryParams(), notNullValue());
assertThat(requestInfo.getQueryParams().parameters(), is(expectedQueryParamMap));
assertThat(requestInfo.getCookies(), is(Sets.newHashSet(new DefaultCookie(cookieName, cookieValue))));
assertThat(requestInfo.pathTemplate, nullValue());
assertThat(requestInfo.pathParams.isEmpty(), is(true));
assertThat(requestInfo.getRawContentBytes(), is(contentBytes));
assertThat(requestInfo.getRawContent(), is(content));
assertThat(requestInfo.content, nullValue());
assertThat(requestInfo.getContentCharset(), is(contentCharset));
assertThat(requestInfo.getProtocolVersion(), is(protocolVersion));
assertThat(requestInfo.isKeepAliveRequested(), is(true));
}
Aggregations