use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.
the class RequestInfoForLoggingRiposteAdapterTest method getHeaderMapIgnoresHeadersWhereNettyGetHeadersMethodReturnsNull.
@Test
public void getHeaderMapIgnoresHeadersWhereNettyGetHeadersMethodReturnsNull() {
Map<String, List<String>> expectedHeaderMap = new TreeMap<>(MapBuilder.<String, List<String>>builder().put("header1", Arrays.asList("h1val1")).build());
HttpHeaders headersMock = mock(HttpHeaders.class);
doReturn(new HashSet<>(Arrays.asList("header1", "header2"))).when(headersMock).names();
doReturn(expectedHeaderMap.get("header1")).when(headersMock).getAll("header1");
doReturn(null).when(headersMock).getAll("header2");
setFieldOnRequestInfo("headers", headersMock);
assertThat(adapter.getHeadersMap(), is(expectedHeaderMap));
}
use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.
the class RequestInfoImpl method dummyInstanceForUnknownRequests.
/**
* Creates a new RequestInfo that represents unknown requests. Usually only needed in error situations. The URI,
* query params, and headers will be tagged with {@link #NONE_OR_UNKNOWN_TAG} to indicate that the request was
* unknown.
*/
public static RequestInfoImpl<?> dummyInstanceForUnknownRequests() {
HttpHeaders headers = new DefaultHttpHeaders().set(NONE_OR_UNKNOWN_TAG, "true");
QueryStringDecoder queryParams = new QueryStringDecoder("/?" + NONE_OR_UNKNOWN_TAG + "=true");
return new RequestInfoImpl(NONE_OR_UNKNOWN_TAG, null, headers, null, queryParams, null, null, null, null, false, true, false);
}
use of io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.
the class RequestInfoImpl method addContentChunk.
/**
* {@inheritDoc}
*/
@Override
public int addContentChunk(HttpContent chunk) {
if (isCompleteRequestWithAllChunks) {
throw new IllegalStateException("Cannot add new content chunk - this RequestInfo is already marked as " + "representing the complete request with all chunks");
}
chunk.retain();
rawContentLengthInBytes += chunk.content().readableBytes();
// If content chunks will be released externally then there's no point in us holding on to them
if (!contentChunksWillBeReleasedExternally)
contentChunks.add(chunk);
if (chunk instanceof LastHttpContent) {
// to be set to true if content chunks are released externally.
if (!contentChunksWillBeReleasedExternally)
isCompleteRequestWithAllChunks = true;
HttpHeaders chunkTrailingHeaders = ((LastHttpContent) chunk).trailingHeaders();
//noinspection StatementWithEmptyBody
if (trailingHeaders == chunkTrailingHeaders) {
// Can happen during the constructor. We've already set the trailing headers to the chunk's trailing headers, so nothing to do here.
} else {
if (!trailingHeaders.isEmpty()) {
throw new IllegalStateException("Received the final chunk, but trailingHeaders was already " + "populated. This should not be possible.");
}
trailingHeaders.add(chunkTrailingHeaders);
}
}
return rawContentLengthInBytes;
}
use of io.netty.handler.codec.http.HttpHeaders 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 io.netty.handler.codec.http.HttpHeaders in project riposte by Nike-Inc.
the class BaseResponseInfoBuilderTest method builder_stores_values_as_expected.
@Test
public void builder_stores_values_as_expected() {
// given
String content = UUID.randomUUID().toString();
int httpStatusCode = 200;
HttpHeaders headers = new DefaultHttpHeaders();
String mimeType = "text/text";
Charset contentCharset = CharsetUtil.ISO_8859_1;
Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2"));
boolean preventCompressedOutput = true;
// when
BaseResponseInfoBuilder<String> responseInfoBuilder = new BaseResponseInfoBuilder<String>() {
}.withHttpStatusCode(httpStatusCode).withHeaders(headers).withDesiredContentWriterMimeType(mimeType).withDesiredContentWriterEncoding(contentCharset).withCookies(cookies).withPreventCompressedOutput(preventCompressedOutput);
// then
assertThat(responseInfoBuilder.getHttpStatusCode(), is(httpStatusCode));
assertThat(responseInfoBuilder.getHeaders(), is(headers));
assertThat(responseInfoBuilder.getDesiredContentWriterMimeType(), is(mimeType));
assertThat(responseInfoBuilder.getDesiredContentWriterEncoding(), is(contentCharset));
assertThat(responseInfoBuilder.getCookies(), is(cookies));
assertThat(responseInfoBuilder.isPreventCompressedOutput(), is(preventCompressedOutput));
}
Aggregations