use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.
the class BaseResponseInfoTest method uber_constructor_for_full_response_sets_fields_as_expected.
@Test
public void uber_constructor_for_full_response_sets_fields_as_expected() {
// given
int httpStatusCode = 200;
HttpHeaders headers = new DefaultHttpHeaders();
String mimeType = "text/text";
Charset contentCharset = CharsetUtil.UTF_8;
Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2"));
boolean preventCompressedResponse = true;
// when
BaseResponseInfo<?> responseInfo = createNewBaseResponseInfoForTesting(httpStatusCode, headers, mimeType, contentCharset, cookies, preventCompressedResponse);
// then
assertThat(responseInfo.getHttpStatusCode(), is(httpStatusCode));
assertThat(responseInfo.getHeaders(), is(headers));
assertThat(responseInfo.getDesiredContentWriterMimeType(), is(mimeType));
assertThat(responseInfo.getDesiredContentWriterEncoding(), is(contentCharset));
assertThat(responseInfo.getCookies(), is(cookies));
assertThat(responseInfo.getUncompressedRawContentLength(), nullValue());
assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedResponse));
assertThat(responseInfo.isResponseSendingStarted(), is(false));
assertThat(responseInfo.isResponseSendingLastChunkSent(), is(false));
}
use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.
the class ChunkedResponseInfoTest method builder_works_as_expected_for_all_fields.
@Test
public void builder_works_as_expected_for_all_fields() {
// given
ChunkedResponseInfo.ChunkedResponseInfoBuilder builder = ResponseInfo.newChunkedResponseBuilder();
int statusCode = 42;
HttpHeaders headers = mock(HttpHeaders.class);
String mimeType = UUID.randomUUID().toString();
Charset encoding = CharsetUtil.US_ASCII;
Set<Cookie> cookies = mock(Set.class);
boolean preventCompressedOutput = Math.random() > 0.5;
// when
ChunkedResponseInfo responseInfo = builder.withHttpStatusCode(statusCode).withHeaders(headers).withDesiredContentWriterMimeType(mimeType).withDesiredContentWriterEncoding(encoding).withCookies(cookies).withPreventCompressedOutput(preventCompressedOutput).build();
// then
assertThat(responseInfo.getHttpStatusCode(), is(statusCode));
assertThat(responseInfo.getHeaders(), is(headers));
assertThat(responseInfo.getDesiredContentWriterMimeType(), is(mimeType));
assertThat(responseInfo.getDesiredContentWriterEncoding(), is(encoding));
assertThat(responseInfo.getCookies(), is(cookies));
assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedOutput));
}
use of io.netty.handler.codec.http.HttpHeaders 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).getUri();
doReturn(method).when(nettyRequestMock).getMethod();
doReturn(headers).when(nettyRequestMock).headers();
doReturn(trailingHeaders).when(nettyRequestMock).trailingHeaders();
doReturn(contentByteBuf).when(nettyRequestMock).content();
doReturn(protocolVersion).when(nettyRequestMock).getProtocolVersion();
// 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 io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.
the class HttpUtilsTest method determineCharsetFromContentType_throws_InvalidCharsetInContentTypeHeaderException_if_passed_header_with_invalid_charset.
@Test(expected = InvalidCharsetInContentTypeHeaderException.class)
public void determineCharsetFromContentType_throws_InvalidCharsetInContentTypeHeaderException_if_passed_header_with_invalid_charset() {
// given
HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.CONTENT_TYPE, "text/text charset=garbagio");
// expect
HttpUtils.determineCharsetFromContentType(headers, CharsetUtil.UTF_8);
fail("Expected an exception but none was thrown");
}
use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.
the class HttpUtilsTest method extractCookies_handles_cookie_values_leniently.
@Test
public void extractCookies_handles_cookie_values_leniently() {
// given
//these are cookie values seen in the wild...
Cookie cookie1 = new DefaultCookie(UUID.randomUUID().toString(), "2094%3Az%7C2021%3Ab");
Cookie cookie2 = new DefaultCookie(UUID.randomUUID().toString(), "geoloc=cc=US,rc=OR,tp=vhigh,tz=PST,la=45.4978,lo=-122.6937,bw=5000");
Cookie cookie3 = new DefaultCookie(UUID.randomUUID().toString(), "\"dm=n.com&si=27431295-a282-4745-8cd5-542e7fce" + "429e&ss=1477551008358&sl=76&tt=437632&obo=12&sh=1477552753923%3D76%3A12%3A437632%2C1477552698670%3D75%3" + "A12%3A429879%2C1477552677137%3D74%3A12%3A426596%2C1477552672564%3D73%3A12%3A425585%2C1477552669893%3D72" + "%3A12%3A423456&bcn=%2F%2F3408178b.mpstat.us%2F&ld=1477552753923&r=http%3A%2F%2Fwww.nike.com%2Fbe%2Fde_de%" + "2F&ul=1477552756811\"");
HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookie1, cookie2, cookie3));
HttpRequest nettyRequestMock = mock(HttpRequest.class);
doReturn(headers).when(nettyRequestMock).headers();
// when
Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock);
// then
assertThat(extractedCookies.contains(cookie1), is(true));
assertThat(extractedCookies.contains(cookie2), is(true));
assertThat(extractedCookies.contains(cookie3), is(true));
}
Aggregations