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.junit.Assert;
37 import org.junit.Test;
38
39
40
41
42
43 public class TestInputStreamEntity {
44
45 @Test
46 public void testBasics() throws Exception {
47 final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
48 final InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(bytes), bytes.length, null);
49
50 Assert.assertEquals(bytes.length, entity.getContentLength());
51 Assert.assertNotNull(entity.getContent());
52 Assert.assertFalse(entity.isRepeatable());
53 Assert.assertTrue(entity.isStreaming());
54 }
55
56 @Test(expected = NullPointerException.class)
57 public void testNullConstructor() throws Exception {
58 new InputStreamEntity(null, 0, null);
59 }
60
61 @Test
62 public void testUnknownLengthConstructor() throws Exception {
63 final InputStreamEntity entity = new InputStreamEntity(EmptyInputStream.INSTANCE, null);
64 Assert.assertEquals(-1, entity.getContentLength());
65 }
66
67 @Test
68 public void testWriteTo() throws Exception {
69 final String message = "Message content";
70 final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
71 final InputStream inStream = new ByteArrayInputStream(bytes);
72 final InputStreamEntity entity = new InputStreamEntity(inStream, bytes.length,
73 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
74
75 final ByteArrayOutputStream out = new ByteArrayOutputStream();
76 entity.writeTo(out);
77 final byte[] writtenBytes = out.toByteArray();
78 Assert.assertNotNull(writtenBytes);
79 Assert.assertEquals(bytes.length, writtenBytes.length);
80
81 final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
82 Assert.assertEquals(message, s);
83 }
84
85 @Test
86 public void testWriteToPartialContent() throws Exception {
87 final String message = "Message content";
88 final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
89 final InputStream inStream = new ByteArrayInputStream(bytes);
90 final int contentLength = 7;
91 final InputStreamEntity entity = new InputStreamEntity(inStream, contentLength,
92 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
93
94 final ByteArrayOutputStream out = new ByteArrayOutputStream();
95 entity.writeTo(out);
96 final byte[] writtenBytes = out.toByteArray();
97 Assert.assertNotNull(writtenBytes);
98 Assert.assertEquals(contentLength, writtenBytes.length);
99
100 final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
101 Assert.assertEquals(message.substring(0, contentLength), s);
102 }
103
104 @Test
105 public void testWriteToUnknownLength() throws Exception {
106 final String message = "Message content";
107 final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
108 final InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(bytes),
109 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
110
111 final ByteArrayOutputStream out = new ByteArrayOutputStream();
112 entity.writeTo(out);
113 final byte[] writtenBytes = out.toByteArray();
114 Assert.assertNotNull(writtenBytes);
115 Assert.assertEquals(bytes.length, writtenBytes.length);
116
117 final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
118 Assert.assertEquals(message, s);
119 }
120
121 @Test(expected = NullPointerException.class)
122 public void testWriteToNull() throws Exception {
123 final InputStreamEntity entity = new InputStreamEntity(EmptyInputStream.INSTANCE, 0, null);
124 entity.writeTo(null);
125 }
126 }