Search in sources :

Example 76 with HttpContent

use of 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);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 77 with HttpContent

use of 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);
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 78 with HttpContent

use of 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));
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.Test)

Example 79 with HttpContent

use of 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));
}
Also used : LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.Test)

Example 80 with HttpContent

use of 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()));
}
Also used : EmptyByteBuf(io.netty.buffer.EmptyByteBuf) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Charset(java.nio.charset.Charset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Test(org.junit.Test)

Aggregations

HttpContent (io.netty.handler.codec.http.HttpContent)273 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)202 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)85 ByteBuf (io.netty.buffer.ByteBuf)81 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)76 HttpResponse (io.netty.handler.codec.http.HttpResponse)65 Test (org.junit.Test)65 HttpRequest (io.netty.handler.codec.http.HttpRequest)56 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)43 ArrayList (java.util.ArrayList)37 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)33 HttpObject (io.netty.handler.codec.http.HttpObject)33 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)29 IOException (java.io.IOException)29 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)26 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)25 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)23 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)22 List (java.util.List)21 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)19