use of org.apache.flink.shaded.netty4.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));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project riposte by Nike-Inc.
the class RequestInfoImplTest method uber_constructor_works_for_valid_values.
@Test
public void uber_constructor_works_for_valid_values() {
// given
String uri = "/some/uri/path/%24foobar%26?notused=blah";
HttpMethod method = HttpMethod.PATCH;
Charset contentCharset = CharsetUtil.US_ASCII;
HttpHeaders headers = new DefaultHttpHeaders().add("header1", "val1").add(HttpHeaders.Names.CONTENT_TYPE, "text/text charset=" + contentCharset.displayName());
QueryStringDecoder queryParams = new QueryStringDecoder(uri + "?foo=bar&secondparam=secondvalue");
Set<Cookie> cookies = new HashSet<>(Arrays.asList(new DefaultCookie("cookie1", "val1"), new DefaultCookie("cookie2", "val2")));
Map<String, String> pathParams = Arrays.stream(new String[][] { { "pathParam1", "val1" }, { "pathParam2", "val2" } }).collect(Collectors.toMap(pair -> pair[0], pair -> pair[1]));
String content = UUID.randomUUID().toString();
byte[] contentBytes = content.getBytes();
LastHttpContent chunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(contentBytes));
chunk.trailingHeaders().add("trailingHeader1", "trailingVal1");
List<HttpContent> contentChunks = Collections.singletonList(chunk);
HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
boolean keepAlive = true;
boolean fullRequest = true;
boolean isMultipart = false;
// when
RequestInfoImpl<?> requestInfo = new RequestInfoImpl<>(uri, method, headers, chunk.trailingHeaders(), queryParams, cookies, pathParams, contentChunks, protocolVersion, keepAlive, fullRequest, isMultipart);
// then
assertThat("getUri should return passed in value", 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(chunk.trailingHeaders()));
assertThat(requestInfo.getQueryParams(), is(queryParams));
assertThat(requestInfo.getCookies(), is(cookies));
assertThat(requestInfo.pathTemplate, nullValue());
assertThat(requestInfo.pathParams, is(pathParams));
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(keepAlive));
assertThat(requestInfo.isCompleteRequestWithAllChunks, is(fullRequest));
assertThat(requestInfo.isMultipart, is(isMultipart));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project riposte by Nike-Inc.
the class RiposteHandlerInternalUtilTest method determineFallbackOverallRequestSpanName_works_as_expected.
@DataProvider(value = { "foo | foo", "null | UNKNOWN_HTTP_METHOD", " | UNKNOWN_HTTP_METHOD", "[whitespace] | UNKNOWN_HTTP_METHOD" }, splitBy = "\\|")
@Test
public void determineFallbackOverallRequestSpanName_works_as_expected(String httpMethodStr, String expectedResult) {
// given
if ("[whitespace]".equals(httpMethodStr)) {
httpMethodStr = " \r\n\t ";
}
HttpMethod httpMethodMock = (httpMethodStr == null) ? null : mock(HttpMethod.class);
HttpRequest nettyHttpRequestMock = mock(HttpRequest.class);
doReturn(httpMethodMock).when(nettyHttpRequestMock).method();
if (httpMethodMock != null) {
doReturn(httpMethodStr).when(httpMethodMock).name();
}
// when
String result = implSpy.determineFallbackOverallRequestSpanName(nettyHttpRequestMock);
// then
Assertions.assertThat(result).isEqualTo(expectedResult);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project riposte by Nike-Inc.
the class StreamingAsyncHttpClientTest method getFallbackSpanName_works_as_expected.
@DataProvider(value = { "NORMAL", "NULL_HTTP_METHOD", "UNEXPECTED_EXCEPTION_IS_THROWN" })
@Test
public void getFallbackSpanName_works_as_expected(GetFallbackSpanNameScenario scenario) {
// given
HttpRequest nettyRequestMock = mock(HttpRequest.class);
HttpMethod httpMethodSpy = (scenario.httpMethod == null) ? null : spy(scenario.httpMethod);
if (scenario.throwUnexpectedException) {
doThrow(new RuntimeException("intentional test exception")).when(httpMethodSpy).name();
}
doReturn(httpMethodSpy).when(nettyRequestMock).method();
StreamingAsyncHttpClient impl = new StreamingAsyncHttpClient(200, 200, true, mock(DistributedTracingConfig.class));
// when
String result = impl.getFallbackSpanName(nettyRequestMock);
// then
assertThat(result).isEqualTo(scenario.expectedResult);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project ambry by linkedin.
the class NettyMultipartRequestTest method instantiationTest.
/**
* Tests instantiation of {@link NettyMultipartRequest} with different {@link HttpMethod} types.
* </p>
* Only {@link HttpMethod#POST} should succeed.
* @throws RestServiceException
*/
@Test
public void instantiationTest() throws RestServiceException {
HttpMethod[] successMethods = { HttpMethod.POST, HttpMethod.PUT };
// POST and PUT will succeed.
for (HttpMethod method : successMethods) {
NettyRequest.bufferWatermark = 1;
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, "/");
MockChannel channel = new MockChannel();
RecvByteBufAllocator expected = channel.config().getRecvByteBufAllocator();
NettyMultipartRequest request = new NettyMultipartRequest(httpRequest, channel, NETTY_METRICS, Collections.emptySet(), Long.MAX_VALUE);
assertTrue("Auto-read should not have been changed", channel.config().isAutoRead());
assertEquals("RecvByteBufAllocator should not have changed", expected, channel.config().getRecvByteBufAllocator());
closeRequestAndValidate(request);
}
// Methods that will fail. Can include other methods, but these should be enough.
HttpMethod[] methods = { HttpMethod.GET, HttpMethod.DELETE, HttpMethod.HEAD };
for (HttpMethod method : methods) {
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, "/");
try {
new NettyMultipartRequest(httpRequest, new MockChannel(), NETTY_METRICS, Collections.emptySet(), Long.MAX_VALUE);
fail("Creation of NettyMultipartRequest should have failed for " + method);
} catch (IllegalArgumentException e) {
// expected. Nothing to do.
}
}
}
Aggregations