Search in sources :

Example 46 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class RequestInfoImplTest method addContentChunk_adds_chunk_content_length_to_rawContentLengthInBytes.

@Test
public void addContentChunk_adds_chunk_content_length_to_rawContentLengthInBytes() 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));
    // when
    requestInfo.addContentChunk(chunk1);
    requestInfo.addContentChunk(lastChunk);
    // then
    assertThat(requestInfo.contentChunks.size(), is(2));
    assertThat(requestInfo.isCompleteRequestWithAllChunks(), is(true));
    assertThat(requestInfo.getRawContentLengthInBytes(), is(chunk1Bytes.length + lastChunkBytes.length));
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) 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 47 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class RequestInfoImplTest method addContentChunk_adds_chunk_to_list_and_retains_it_for_chunks_that_are_not_the_last.

@Test
public void addContentChunk_adds_chunk_to_list_and_retains_it_for_chunks_that_are_not_the_last() {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    requestInfo.isCompleteRequestWithAllChunks = false;
    HttpContent chunk1Mock = mock(HttpContent.class);
    HttpContent chunk2Mock = mock(HttpContent.class);
    doReturn(mock(ByteBuf.class)).when(chunk1Mock).content();
    doReturn(mock(ByteBuf.class)).when(chunk2Mock).content();
    // when
    requestInfo.addContentChunk(chunk1Mock);
    requestInfo.addContentChunk(chunk2Mock);
    // then
    assertThat(requestInfo.contentChunks.size(), is(2));
    assertThat(requestInfo.contentChunks.get(0), is(chunk1Mock));
    assertThat(requestInfo.contentChunks.get(1), is(chunk2Mock));
    assertThat(requestInfo.isCompleteRequestWithAllChunks(), is(false));
    assertThat(requestInfo.getRawContentBytes(), nullValue());
    assertThat(requestInfo.getRawContent(), nullValue());
    verify(chunk1Mock).retain();
    verify(chunk1Mock).content();
    verifyNoMoreInteractions(chunk1Mock);
    verify(chunk2Mock).retain();
    verify(chunk2Mock).content();
    verifyNoMoreInteractions(chunk2Mock);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) 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 48 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class HttpUtilsTest method extractContentChunks_returns_null_for_request_that_is_not_HttpContent.

@Test
public void extractContentChunks_returns_null_for_request_that_is_not_HttpContent() {
    // given
    HttpRequest request = mock(HttpRequest.class);
    // when
    List<HttpContent> result = HttpUtils.extractContentChunks(request);
    // then
    assertThat(result, nullValue());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Test(org.junit.Test)

Example 49 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class HttpUtilsTest method extractContentChunks_works_for_request_that_is_also_HttpContent.

@Test
public void extractContentChunks_works_for_request_that_is_also_HttpContent() {
    // given
    HttpRequest request = mock(FullHttpRequest.class);
    // when
    List<HttpContent> result = HttpUtils.extractContentChunks(request);
    // then
    assertThat(result, notNullValue());
    assertThat(result.size(), is(1));
    assertThat(result.get(0), is(request));
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Test(org.junit.Test)

Example 50 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project riposte by Nike-Inc.

the class ProcessFinalResponseOutputHandlerTest method write_does_nothing_to_finalContentLength_if_msg_is_HttpContent_but_responseInfo_is_null.

@Test
public void write_does_nothing_to_finalContentLength_if_msg_is_HttpContent_but_responseInfo_is_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();
    doReturn(null).when(stateMock).getResponseInfo();
    assertThat(responseInfo.getFinalContentLength()).isNull();
    // when
    handler.write(ctxMock, msgMock, promiseMock);
    // then
    assertThat(responseInfo.getFinalContentLength()).isNull();
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Aggregations

HttpContent (io.netty.handler.codec.http.HttpContent)53 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)38 Test (org.junit.Test)23 ByteBuf (io.netty.buffer.ByteBuf)22 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)20 HttpResponse (io.netty.handler.codec.http.HttpResponse)16 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)13 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 Map (java.util.Map)7 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)6 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)5 ChannelFuture (io.netty.channel.ChannelFuture)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 List (java.util.List)4 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)3 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)3 RequestInfo (com.nike.riposte.server.http.RequestInfo)3