Search in sources :

Example 16 with DefaultHttpContent

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

the class RequestInfoImplTest method addContentChunk_does_not_add_chunk_to_contentChunks_list_if_contentChunksWillBeReleasedExternally_is_true.

@Test
public void addContentChunk_does_not_add_chunk_to_contentChunks_list_if_contentChunksWillBeReleasedExternally_is_true() {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    requestInfo.isCompleteRequestWithAllChunks = false;
    requestInfo.contentChunksWillBeReleasedExternally();
    HttpContent chunk = new DefaultHttpContent(Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8));
    // when
    requestInfo.addContentChunk(chunk);
    // then
    Assertions.assertThat(requestInfo.contentChunks).isEmpty();
}
Also used : 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 17 with DefaultHttpContent

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

the class HttpUtilsTest method convertContentChunksToRawString_and_convertContentChunksToRawBytes_works.

@Test
@DataProvider(value = { "somecontent1234!@#$%^&*/?.,<>;:'\"{}[]()     | alsosomecontent1234!@#$%^&*/?.,<>;:'\"{}[]()    |   UTF-8", "somecontent1234!@#$%^&*/?.,<>;:'\"{}[]()     | alsosomecontent1234!@#$%^&*/?.,<>;:'\"{}[]()    |   UTF-16", "somecontent1234!@#$%^&*/?.,<>;:'\"{}[]()     | alsosomecontent1234!@#$%^&*/?.,<>;:'\"{}[]()    |   ISO-8859-1" }, splitBy = "\\|")
public void convertContentChunksToRawString_and_convertContentChunksToRawBytes_works(String chunk1Base, String chunk2Base, String charsetString) throws IOException {
    // given
    Charset contentCharset = Charset.forName(charsetString);
    String chunk1Content = chunk1Base + "-" + UUID.randomUUID().toString();
    String chunk2Content = chunk2Base + "-" + 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(chunk2ByteBuf));
    // 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 : 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) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 18 with DefaultHttpContent

use of io.netty.handler.codec.http.DefaultHttpContent 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 19 with DefaultHttpContent

use of io.netty.handler.codec.http.DefaultHttpContent in project asterixdb by apache.

the class ChunkedNettyOutputStream method flush.

private void flush(byte[] buf, int offset, int len) throws IOException {
    ensureWritable();
    ByteBuf aBuffer = ctx.alloc().buffer(len);
    aBuffer.writeBytes(buf, offset, len);
    if (response.status() == HttpResponseStatus.OK) {
        response.beforeFlush();
        ctx.write(new DefaultHttpContent(aBuffer), ctx.channel().voidPromise());
    } else {
        response.error(aBuffer);
    }
}
Also used : DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)19 ByteBuf (io.netty.buffer.ByteBuf)11 HttpContent (io.netty.handler.codec.http.HttpContent)10 Test (org.junit.Test)10 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)9 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)7 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)4 EmptyByteBuf (io.netty.buffer.EmptyByteBuf)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 IOException (java.io.IOException)2 Charset (java.nio.charset.Charset)2 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelPromise (io.netty.channel.ChannelPromise)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1