use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.
the class ProcessFinalResponseOutputHandlerTest method write_adds_to_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_not_null.
@Test
public void write_adds_to_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_not_null() throws Exception {
// given
HttpContent msgMock = mock(HttpContent.class);
ByteBuf contentMock = mock(ByteBuf.class);
int contentBytes = (int) (Math.random() * 10000);
doReturn(contentMock).when(msgMock).content();
doReturn(contentBytes).when(contentMock).readableBytes();
int initialFinalContentLengthValue = (int) (Math.random() * 10000);
responseInfo.setFinalContentLength((long) initialFinalContentLengthValue);
assertThat(responseInfo.getFinalContentLength()).isEqualTo(initialFinalContentLengthValue);
// when
handler.write(ctxMock, msgMock, promiseMock);
// then
assertThat(responseInfo.getFinalContentLength()).isEqualTo(initialFinalContentLengthValue + contentBytes);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.
the class RequestContentValidationHandlerTest method doChannelRead_does_nothing_if_msg_is_not_LastHttpContent.
@Test
public void doChannelRead_does_nothing_if_msg_is_not_LastHttpContent() throws Exception {
// given
HttpContent notLastContentMsg = mock(HttpContent.class);
// when
PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, notLastContentMsg);
// then
verifyNoInteractions(requestInfoMock);
verifyNoInteractions(endpointMock);
verifyNoInteractions(stateMock);
verifyNoInteractions(requestValidatorMock);
assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.
the class RequestInfoImplTest method addContentChunk_and_getRawConent_and_getRawContentBytes_work_as_expected_for_last_chunk.
@Test
public void addContentChunk_and_getRawConent_and_getRawContentBytes_work_as_expected_for_last_chunk() throws IOException {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
requestInfo.isCompleteRequestWithAllChunks = false;
String chunk1String = UUID.randomUUID().toString();
String lastChunkString = UUID.randomUUID().toString();
byte[] chunk1Bytes = chunk1String.getBytes();
byte[] lastChunkBytes = lastChunkString.getBytes();
HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer(chunk1Bytes));
HttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastChunkBytes));
assertThat(chunk1.refCnt(), is(1));
assertThat(lastChunk.refCnt(), is(1));
assertThat(requestInfo.getRawContentBytes(), nullValue());
assertThat(requestInfo.getRawContent(), nullValue());
// when
requestInfo.addContentChunk(chunk1);
requestInfo.addContentChunk(lastChunk);
// then
assertThat(chunk1.refCnt(), is(2));
assertThat(lastChunk.refCnt(), is(2));
assertThat(requestInfo.contentChunks.size(), is(2));
assertThat(requestInfo.isCompleteRequestWithAllChunks(), is(true));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(chunk1Bytes);
baos.write(lastChunkBytes);
assertThat(requestInfo.getRawContentBytes(), is(baos.toByteArray()));
String rawContentString = requestInfo.getRawContent();
assertThat(requestInfo.getRawContent(), is(chunk1String + lastChunkString));
// Verify that the raw content string is cached the first time it's loaded and reused for subsequent calls
assertThat(requestInfo.getRawContent() == rawContentString, is(true));
assertThat(chunk1.refCnt(), is(1));
assertThat(lastChunk.refCnt(), is(1));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.
the class RequestInfoImplTest method releaseContentChunks_calls_release_on_each_chunk_and_calls_clear_on_chunk_list.
@Test
public void releaseContentChunks_calls_release_on_each_chunk_and_calls_clear_on_chunk_list() {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
List<HttpContent> contentChunkList = Arrays.asList(mock(HttpContent.class), mock(HttpContent.class));
requestInfo.contentChunks.addAll(contentChunkList);
assertThat(requestInfo.contentChunks.size(), is(contentChunkList.size()));
// when
requestInfo.releaseContentChunks();
// then
for (HttpContent chunkMock : contentChunkList) {
verify(chunkMock).release();
}
assertThat(requestInfo.contentChunks.isEmpty(), is(true));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.
the class HttpUtilsTest method convertContentChunksToRawString_and_convertContentChunksToRawBytes_works_with_EmptyByteBuf_chunks.
@Test
public void convertContentChunksToRawString_and_convertContentChunksToRawBytes_works_with_EmptyByteBuf_chunks() throws IOException {
// given
Charset contentCharset = CharsetUtil.UTF_8;
String chunk1Content = UUID.randomUUID().toString();
String chunk2Content = UUID.randomUUID().toString();
byte[] chunk1Bytes = chunk1Content.getBytes(contentCharset);
byte[] chunk2Bytes = chunk2Content.getBytes(contentCharset);
ByteBuf chunk1ByteBuf = Unpooled.copiedBuffer(chunk1Bytes);
ByteBuf chunk2ByteBuf = Unpooled.copiedBuffer(chunk2Bytes);
Collection<HttpContent> chunkCollection = Arrays.asList(new DefaultHttpContent(chunk1ByteBuf), new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)), new DefaultHttpContent(chunk2ByteBuf), new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)));
// when
String resultString = HttpUtils.convertContentChunksToRawString(contentCharset, chunkCollection);
byte[] resultBytes = HttpUtils.convertContentChunksToRawBytes(chunkCollection);
// then
String expectedResultString = chunk1Content + chunk2Content;
assertThat(resultString, is(expectedResultString));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(chunk1Bytes);
baos.write(chunk2Bytes);
assertThat(resultBytes, is(baos.toByteArray()));
}
Aggregations