@Test(expected = IllegalArgumentException.class) public void testWriteToNull() throws Exception { final InputStream inStream = new ByteArrayInputStream(new byte[0]); final InputStreamEntity httpentity = new InputStreamEntity(inStream, 0); httpentity.writeTo(null); } }
@Test public void testWriteTo() throws Exception { final String message = "Message content"; final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1); final InputStream inStream = new ByteArrayInputStream(bytes); final InputStreamEntity httpentity = new InputStreamEntity(inStream, bytes.length); final ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); final byte[] writtenBytes = out.toByteArray(); Assert.assertNotNull(writtenBytes); Assert.assertEquals(bytes.length, writtenBytes.length); final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name()); Assert.assertEquals(message, s); }
@Test public void testWriteToPartialContent() throws Exception { final String message = "Message content"; final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1); final InputStream inStream = new ByteArrayInputStream(bytes); final int contentLength = 7; final InputStreamEntity httpentity = new InputStreamEntity(inStream, contentLength); final ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); final byte[] writtenBytes = out.toByteArray(); Assert.assertNotNull(writtenBytes); Assert.assertEquals(contentLength, writtenBytes.length); final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name()); Assert.assertEquals(message.substring(0, contentLength), s); }
@Test public void testWriteToUnknownLength() throws Exception { final String message = "Message content"; final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1); final InputStream inStream = new ByteArrayInputStream(bytes); final InputStreamEntity httpentity = new InputStreamEntity(inStream); final ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); final byte[] writtenBytes = out.toByteArray(); Assert.assertNotNull(writtenBytes); Assert.assertEquals(bytes.length, writtenBytes.length); final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name()); Assert.assertEquals(message, s); }