@Override public String getContentEncoding() { return wrapped.getContentEncoding(); }
@Override public String getContentEncoding() { return wrapped.getContentEncoding(); }
@Test public void testTextContent() throws Exception { final AsyncEntityProducer producer = new StringAsyncEntityProducer( "abc", ContentType.TEXT_PLAIN); Assert.assertEquals(-1, producer.getContentLength()); Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII)); }
@Test public void testBinaryContent() throws Exception { final AsyncEntityProducer producer = new BasicAsyncEntityProducer( new byte[] { 'a', 'b', 'c' }, ContentType.DEFAULT_BINARY); Assert.assertEquals(3, producer.getContentLength()); Assert.assertEquals(ContentType.DEFAULT_BINARY.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII)); }
@Test public void testTextContent() throws Exception { final AsyncEntityProducer producer = new BasicAsyncEntityProducer( "abc", ContentType.TEXT_PLAIN); Assert.assertEquals(3, producer.getContentLength()); Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII)); }
@Test public void testTextContent() throws Exception { final AsyncEntityProducer producer = new FileEntityProducer(tempFile, ContentType.TEXT_PLAIN); Assert.assertEquals(6, producer.getContentLength()); Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("abcdef", byteChannel.dump(StandardCharsets.US_ASCII)); }
@Test public void testTextContentRepeatable() throws Exception { final AsyncEntityProducer producer = new StringAsyncEntityProducer( "abc", ContentType.TEXT_PLAIN); Assert.assertEquals(-1, producer.getContentLength()); Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); for (int i = 0; i < 3; i++) { final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII)); producer.releaseResources(); } }
@Test public void testTextContentRepeatable() throws Exception { final AsyncEntityProducer producer = new BasicAsyncEntityProducer( "abc", ContentType.TEXT_PLAIN); Assert.assertEquals(3, producer.getContentLength()); Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); for (int i = 0; i < 3; i++) { final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII)); producer.releaseResources(); } }
@Test public void testTextContentRepeatable() throws Exception { final AsyncEntityProducer producer = new FileEntityProducer(tempFile, ContentType.TEXT_PLAIN); Assert.assertEquals(6, producer.getContentLength()); Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); for (int i = 0; i < 3; i++) { final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("abcdef", byteChannel.dump(StandardCharsets.US_ASCII)); producer.releaseResources(); } }
@Test public void testProduceDataNoBuffering() throws Exception { final AsyncEntityProducer producer = new ChunkCharAsyncEntityProducer( 256, 0, ContentType.TEXT_PLAIN, "this", "this and that"); Assert.assertEquals(-1, producer.getContentLength()); Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); Assert.assertTrue(byteChannel.isOpen()); Assert.assertEquals("this", byteChannel.dump(StandardCharsets.US_ASCII)); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("this and that", byteChannel.dump(StandardCharsets.US_ASCII)); }
@Test public void testProduceDataNoBuffering() throws Exception { final AsyncEntityProducer producer = new ChunkByteAsyncEntityProducer( 0, ContentType.TEXT_PLAIN, new byte[] { '1', '2', '3' }, new byte[] { '4', '5', '6' }); Assert.assertEquals(-1, producer.getContentLength()); Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType()); Assert.assertEquals(null, producer.getContentEncoding()); final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024); final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel); producer.produce(streamChannel); Assert.assertTrue(byteChannel.isOpen()); Assert.assertEquals("123", byteChannel.dump(StandardCharsets.US_ASCII)); producer.produce(streamChannel); Assert.assertFalse(byteChannel.isOpen()); Assert.assertEquals("456", byteChannel.dump(StandardCharsets.US_ASCII)); }