@Test public void testKnownLengthContentToString() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentLength(bytes.length); final String s = EntityUtils.toString(httpentity, "ISO-8859-1"); Assert.assertEquals("Message content", s); }}
@Test public void testDefaultCharsetContentToString() throws Exception { final String content = constructString(RUSSIAN_HELLO); final byte[] bytes = content.getBytes(Charset.forName("KOI8-R")); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentType("text/plain"); final String s = EntityUtils.toString(httpentity, "KOI8-R"); Assert.assertEquals(content, s); }}
@Test public void testContent() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final InputStream content = new ByteArrayInputStream(bytes); final BasicHttpEntity httpentity = new BasicHttpEntity(); try { httpentity.getContent(); Assert.fail("IllegalStateException should have been thrown"); } catch (final IllegalStateException ex) { // expected } httpentity.setContent(content); Assert.assertEquals(content, httpentity.getContent()); httpentity.setContent(null); try { httpentity.getContent(); Assert.fail("IllegalStateException should have been thrown"); } catch (final IllegalStateException ex) { // expected } }
@Test public void testUnknownLengthContentToString() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentLength(-1L); final String s = EntityUtils.toString(httpentity, "ISO-8859-1"); Assert.assertEquals("Message content", s); }}
@Test public void testContentWithContentTypeToString() throws Exception { final String content = constructString(RUSSIAN_HELLO); final byte[] bytes = content.getBytes(StandardCharsets.UTF_8); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentType("text/plain; charset=UTF-8"); final String s = EntityUtils.toString(httpentity, "ISO-8859-1"); Assert.assertEquals(content, s); }}
@Test public void testWriteTo() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final InputStream content = new ByteArrayInputStream(bytes); final BasicHttpEntity httpentity = new BasicHttpEntity(); httpentity.setContent(content); ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); final byte[] bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals(bytes[i], bytes2[i]); } httpentity.setContent(null); out = new ByteArrayOutputStream(); try { httpentity.writeTo(out); Assert.fail("IllegalStateException should have been thrown"); } catch (final IllegalStateException ex) { // expected } try { httpentity.writeTo(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
@Test public void testMaxIntContentToByteArray() throws Exception { final byte[] content = "Message content".getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(content)); httpentity.setContentLength(Integer.MAX_VALUE + 100L); try { EntityUtils.toByteArray(httpentity); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }}
@Test public void testNoCharsetContentToString() throws Exception { final String content = constructString(SWISS_GERMAN_HELLO); final byte[] bytes = content.getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentType("text/plain"); final String s = EntityUtils.toString(httpentity); Assert.assertEquals(content, s); }}
@Test public void testMaxIntContentToString() throws Exception { final byte[] content = "Message content".getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(content)); httpentity.setContentLength(Integer.MAX_VALUE + 100L); try { EntityUtils.toString(httpentity); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }}
@Test public void testContentWithInvalidContentTypeToString() throws Exception { final String content = constructString(RUSSIAN_HELLO); final byte[] bytes = content.getBytes("UTF-8"); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentType("text/plain; charset=nosuchcharset"); final String s = EntityUtils.toString(httpentity, "UTF-8"); Assert.assertEquals(content, s); }}
@Test public void testKnownLengthContentToByteArray() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentLength(bytes.length); final byte[] bytes2 = EntityUtils.toByteArray(httpentity); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals(bytes[i], bytes2[i]); } }}
@Test public void testUnknownLengthContentToByteArray() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentLength(-1L); final byte[] bytes2 = EntityUtils.toByteArray(httpentity); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals(bytes[i], bytes2[i]); } } }
@Test public void testBasics() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final InputStream content = new ByteArrayInputStream(bytes); final BasicHttpEntity httpentity = new BasicHttpEntity(); httpentity.setContent(content); httpentity.setContentLength(bytes.length); Assert.assertEquals(bytes.length, httpentity.getContentLength()); Assert.assertFalse(httpentity.isRepeatable()); Assert.assertTrue(httpentity.isStreaming()); }