@Test public void convertContentChunksToRawString_and_convertContentChunksToRawBytes_returns_null_if_chunks_arg_is_empty() { // when String resultString = HttpUtils.convertContentChunksToRawString(CharsetUtil.UTF_8, Collections.emptyList()); byte[] resultBytes = HttpUtils.convertContentChunksToRawBytes(Collections.emptyList()); // then assertThat(resultString, nullValue()); assertThat(resultBytes, nullValue()); }
@Test public void convertContentChunksToRawString_and_convertContentChunksToRawBytes_returns_null_if_chunks_arg_is_null() { // when String resultString = HttpUtils.convertContentChunksToRawString(CharsetUtil.UTF_8, null); byte[] resultBytes = HttpUtils.convertContentChunksToRawBytes(null); // then assertThat(resultString, nullValue()); assertThat(resultBytes, nullValue()); }
@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())); }
@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())); }