1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.http.io.entity;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.InputStream;
33 import java.nio.charset.StandardCharsets;
34
35 import org.apache.hc.core5.http.ContentType;
36 import org.apache.hc.core5.http.impl.io.EmptyInputStream;
37 import org.junit.Assert;
38 import org.junit.Test;
39
40
41
42
43
44 public class TestInputStreamEntity {
45
46 @Test
47 public void testBasics() throws Exception {
48 final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
49 final InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(bytes), bytes.length, null);
50
51 Assert.assertEquals(bytes.length, entity.getContentLength());
52 Assert.assertNotNull(entity.getContent());
53 Assert.assertFalse(entity.isRepeatable());
54 Assert.assertTrue(entity.isStreaming());
55 }
56
57 @Test(expected = NullPointerException.class)
58 public void testNullConstructor() throws Exception {
59 new InputStreamEntity(null, 0, null);
60 }
61
62 @Test
63 public void testUnknownLengthConstructor() throws Exception {
64 final InputStreamEntity entity = new InputStreamEntity(EmptyInputStream.INSTANCE, null);
65 Assert.assertEquals(-1, entity.getContentLength());
66 }
67
68 @Test
69 public void testWriteTo() throws Exception {
70 final String message = "Message content";
71 final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
72 final InputStream inStream = new ByteArrayInputStream(bytes);
73 final InputStreamEntity entity = new InputStreamEntity(inStream, bytes.length,
74 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
75
76 final ByteArrayOutputStream out = new ByteArrayOutputStream();
77 entity.writeTo(out);
78 final byte[] writtenBytes = out.toByteArray();
79 Assert.assertNotNull(writtenBytes);
80 Assert.assertEquals(bytes.length, writtenBytes.length);
81
82 final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
83 Assert.assertEquals(message, s);
84 }
85
86 @Test
87 public void testWriteToPartialContent() throws Exception {
88 final String message = "Message content";
89 final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
90 final InputStream inStream = new ByteArrayInputStream(bytes);
91 final int contentLength = 7;
92 final InputStreamEntity entity = new InputStreamEntity(inStream, contentLength,
93 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
94
95 final ByteArrayOutputStream out = new ByteArrayOutputStream();
96 entity.writeTo(out);
97 final byte[] writtenBytes = out.toByteArray();
98 Assert.assertNotNull(writtenBytes);
99 Assert.assertEquals(contentLength, writtenBytes.length);
100
101 final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
102 Assert.assertEquals(message.substring(0, contentLength), s);
103 }
104
105 @Test
106 public void testWriteToUnknownLength() throws Exception {
107 final String message = "Message content";
108 final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
109 final InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(bytes),
110 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
111
112 final ByteArrayOutputStream out = new ByteArrayOutputStream();
113 entity.writeTo(out);
114 final byte[] writtenBytes = out.toByteArray();
115 Assert.assertNotNull(writtenBytes);
116 Assert.assertEquals(bytes.length, writtenBytes.length);
117
118 final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
119 Assert.assertEquals(message, s);
120 }
121
122 @Test(expected = NullPointerException.class)
123 public void testWriteToNull() throws Exception {
124 final InputStreamEntity entity = new InputStreamEntity(EmptyInputStream.INSTANCE, 0, null);
125 entity.writeTo(null);
126 }
127 }