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 try (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
74 @Test
75 public void testIllegalConstructor() throws Exception {
76 Assertions.assertThrows(NullPointerException.class, () -> new BufferedHttpEntity(null));
77 }
78
79 @Test
80 public void testWriteToBuffered() throws Exception {
81 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
82 final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1, null);
83 try (final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity)) {
84
85 ByteArrayOutputStream out = new ByteArrayOutputStream();
86 bufentity.writeTo(out);
87 byte[] bytes2 = out.toByteArray();
88 Assertions.assertNotNull(bytes2);
89 Assertions.assertEquals(bytes.length, bytes2.length);
90 for (int i = 0; i < bytes.length; i++) {
91 Assertions.assertEquals(bytes[i], bytes2[i]);
92 }
93
94 out = new ByteArrayOutputStream();
95 bufentity.writeTo(out);
96 bytes2 = out.toByteArray();
97 Assertions.assertNotNull(bytes2);
98 Assertions.assertEquals(bytes.length, bytes2.length);
99 for (int i = 0; i < bytes.length; i++) {
100 Assertions.assertEquals(bytes[i], bytes2[i]);
101 }
102
103 Assertions.assertThrows(NullPointerException.class, () -> bufentity.writeTo(null));
104 }
105 }
106
107 @Test
108 public void testWriteToWrapped() throws Exception {
109 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
110 final ByteArrayEntity httpentity = new ByteArrayEntity(bytes, null);
111 try (final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity)) {
112
113 ByteArrayOutputStream out = new ByteArrayOutputStream();
114 bufentity.writeTo(out);
115 byte[] bytes2 = out.toByteArray();
116 Assertions.assertNotNull(bytes2);
117 Assertions.assertEquals(bytes.length, bytes2.length);
118 for (int i = 0; i < bytes.length; i++) {
119 Assertions.assertEquals(bytes[i], bytes2[i]);
120 }
121
122 out = new ByteArrayOutputStream();
123 bufentity.writeTo(out);
124 bytes2 = out.toByteArray();
125 Assertions.assertNotNull(bytes2);
126 Assertions.assertEquals(bytes.length, bytes2.length);
127 for (int i = 0; i < bytes.length; i++) {
128 Assertions.assertEquals(bytes[i], bytes2[i]);
129 }
130
131 Assertions.assertThrows(NullPointerException.class, () -> bufentity.writeTo(null));
132 }
133 }
134
135 }