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.apache.hc.core5.http.ContentType;
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41
42 public class TestBasicHttpEntity {
43
44 @Test
45 public void testBasics() throws Exception {
46 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
47 final BasicHttpEntity httpentity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length, null);
48 Assertions.assertEquals(bytes.length, httpentity.getContentLength());
49
50 Assertions.assertFalse(httpentity.isRepeatable());
51
52 Assertions.assertTrue(httpentity.isStreaming());
53 }
54
55 @Test
56 public void testConstructorNullContent() throws Exception {
57
58 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, ContentType.APPLICATION_ATOM_XML));
59 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, null));
60
61 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, ContentType.APPLICATION_ATOM_XML, false));
62 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, null, false));
63
64 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, ContentType.APPLICATION_ATOM_XML, ""));
65 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, null, ""));
66
67 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, ContentType.APPLICATION_ATOM_XML));
68 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, null));
69
70 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, ContentType.APPLICATION_ATOM_XML, ""));
71 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, null, ""));
72
73 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, ContentType.APPLICATION_ATOM_XML, "", false));
74 Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, null, "", false));
75 }
76
77 @Test
78 public void testToString() throws Exception {
79 try (final BasicHttpEntity httpentity = new BasicHttpEntity(EmptyInputStream.INSTANCE, 10,
80 ContentType.parseLenient("blah"), "yada", true)) {
81 Assertions.assertEquals(
82 "[Entity-Class: BasicHttpEntity, Content-Type: blah, Content-Encoding: yada, chunked: true]",
83 httpentity.toString());
84 }
85 }
86
87 @Test
88 public void testWriteTo() throws Exception {
89 final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
90 final BasicHttpEntity httpentity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length,
91 ContentType.TEXT_PLAIN);
92
93 Assertions.assertFalse(httpentity.isRepeatable());
94
95 Assertions.assertTrue(httpentity.isStreaming());
96 final ByteArrayOutputStream out = new ByteArrayOutputStream();
97 httpentity.writeTo(out);
98 final byte[] bytes2 = out.toByteArray();
99 Assertions.assertNotNull(bytes2);
100 Assertions.assertEquals(bytes.length, bytes2.length);
101 for (int i = 0; i < bytes.length; i++) {
102 Assertions.assertEquals(bytes[i], bytes2[i]);
103 }
104 }
105
106 }