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.nio.charset.StandardCharsets;
33
34 import org.junit.jupiter.api.Assertions;
35 import org.junit.jupiter.api.Test;
36
37
38
39
40
41 public class TestBufferedHttpEntity {
42
43 @Test
44 public void testBufferingEntity() throws Exception {
45 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
46 final BufferedHttpEntity entity = new BufferedHttpEntity(
47 new InputStreamEntity(new ByteArrayInputStream(bytes), -1, null));
48 Assertions.assertEquals(bytes.length, entity.getContentLength());
49 Assertions.assertTrue(entity.isRepeatable());
50 Assertions.assertFalse(entity.isChunked());
51 Assertions.assertFalse(entity.isStreaming());
52
53
54 Assertions.assertNotNull(entity.getContent ());
55 Assertions.assertNotNull(entity.getContent ());
56 }
57
58 @Test
59 public void testWrappingEntity() throws Exception {
60 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
61 final ByteArrayEntity httpentity = new ByteArrayEntity(bytes, null, true);
62 final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
63 Assertions.assertEquals(bytes.length, bufentity.getContentLength());
64 Assertions.assertTrue(bufentity.isRepeatable());
65 Assertions.assertTrue(bufentity.isChunked());
66 Assertions.assertFalse(bufentity.isStreaming());
67
68
69 Assertions.assertNotNull(bufentity.getContent ());
70 Assertions.assertNotNull(bufentity.getContent ());
71 }
72
73 @Test
74 public void testIllegalConstructor() throws Exception {
75 Assertions.assertThrows(NullPointerException.class, () -> new BufferedHttpEntity(null));
76 }
77
78 @Test
79 public void testWriteToBuffered() throws Exception {
80 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
81 final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1, null);
82 final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
83
84 ByteArrayOutputStream out = new ByteArrayOutputStream();
85 bufentity.writeTo(out);
86 byte[] bytes2 = out.toByteArray();
87 Assertions.assertNotNull(bytes2);
88 Assertions.assertEquals(bytes.length, bytes2.length);
89 for (int i = 0; i < bytes.length; i++) {
90 Assertions.assertEquals(bytes[i], bytes2[i]);
91 }
92
93 out = new ByteArrayOutputStream();
94 bufentity.writeTo(out);
95 bytes2 = out.toByteArray();
96 Assertions.assertNotNull(bytes2);
97 Assertions.assertEquals(bytes.length, bytes2.length);
98 for (int i = 0; i < bytes.length; i++) {
99 Assertions.assertEquals(bytes[i], bytes2[i]);
100 }
101
102 Assertions.assertThrows(NullPointerException.class, () -> bufentity.writeTo(null));
103 }
104
105 @Test
106 public void testWriteToWrapped() throws Exception {
107 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
108 final ByteArrayEntity httpentity = new ByteArrayEntity(bytes, null);
109 final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
110
111 ByteArrayOutputStream out = new ByteArrayOutputStream();
112 bufentity.writeTo(out);
113 byte[] bytes2 = out.toByteArray();
114 Assertions.assertNotNull(bytes2);
115 Assertions.assertEquals(bytes.length, bytes2.length);
116 for (int i = 0; i < bytes.length; i++) {
117 Assertions.assertEquals(bytes[i], bytes2[i]);
118 }
119
120 out = new ByteArrayOutputStream();
121 bufentity.writeTo(out);
122 bytes2 = out.toByteArray();
123 Assertions.assertNotNull(bytes2);
124 Assertions.assertEquals(bytes.length, bytes2.length);
125 for (int i = 0; i < bytes.length; i++) {
126 Assertions.assertEquals(bytes[i], bytes2[i]);
127 }
128
129 Assertions.assertThrows(NullPointerException.class, () -> bufentity.writeTo(null));
130 }
131
132 }