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.Assert;
35 import org.junit.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 Assert.assertEquals(bytes.length, entity.getContentLength());
49 Assert.assertTrue(entity.isRepeatable());
50 Assert.assertFalse(entity.isChunked());
51 Assert.assertFalse(entity.isStreaming());
52
53
54 Assert.assertNotNull(entity.getContent ());
55 Assert.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 Assert.assertEquals(bytes.length, bufentity.getContentLength());
64 Assert.assertTrue(bufentity.isRepeatable());
65 Assert.assertTrue(bufentity.isChunked());
66 Assert.assertFalse(bufentity.isStreaming());
67
68
69 Assert.assertNotNull(bufentity.getContent ());
70 Assert.assertNotNull(bufentity.getContent ());
71 }
72
73 @Test
74 public void testIllegalConstructor() throws Exception {
75 try {
76 new BufferedHttpEntity(null);
77 Assert.fail("NullPointerException should have been thrown");
78 } catch (final NullPointerException ex) {
79
80 }
81 }
82
83 @Test
84 public void testWriteToBuffered() throws Exception {
85 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
86 final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1, null);
87 final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
88
89 ByteArrayOutputStream out = new ByteArrayOutputStream();
90 bufentity.writeTo(out);
91 byte[] bytes2 = out.toByteArray();
92 Assert.assertNotNull(bytes2);
93 Assert.assertEquals(bytes.length, bytes2.length);
94 for (int i = 0; i < bytes.length; i++) {
95 Assert.assertEquals(bytes[i], bytes2[i]);
96 }
97
98 out = new ByteArrayOutputStream();
99 bufentity.writeTo(out);
100 bytes2 = out.toByteArray();
101 Assert.assertNotNull(bytes2);
102 Assert.assertEquals(bytes.length, bytes2.length);
103 for (int i = 0; i < bytes.length; i++) {
104 Assert.assertEquals(bytes[i], bytes2[i]);
105 }
106
107 try {
108 bufentity.writeTo(null);
109 Assert.fail("NullPointerException should have been thrown");
110 } catch (final NullPointerException ex) {
111
112 }
113 }
114
115 @Test
116 public void testWriteToWrapped() throws Exception {
117 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
118 final ByteArrayEntity httpentity = new ByteArrayEntity(bytes, null);
119 final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
120
121 ByteArrayOutputStream out = new ByteArrayOutputStream();
122 bufentity.writeTo(out);
123 byte[] bytes2 = out.toByteArray();
124 Assert.assertNotNull(bytes2);
125 Assert.assertEquals(bytes.length, bytes2.length);
126 for (int i = 0; i < bytes.length; i++) {
127 Assert.assertEquals(bytes[i], bytes2[i]);
128 }
129
130 out = new ByteArrayOutputStream();
131 bufentity.writeTo(out);
132 bytes2 = out.toByteArray();
133 Assert.assertNotNull(bytes2);
134 Assert.assertEquals(bytes.length, bytes2.length);
135 for (int i = 0; i < bytes.length; i++) {
136 Assert.assertEquals(bytes[i], bytes2[i]);
137 }
138
139 try {
140 bufentity.writeTo(null);
141 Assert.fail("NullPointerException should have been thrown");
142 } catch (final NullPointerException ex) {
143
144 }
145 }
146
147 }