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.ByteArrayOutputStream;
31 import java.nio.charset.Charset;
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 public class TestStringEntity {
42
43 @Test
44 public void testBasics() throws Exception {
45 final String s = "Message content";
46 final StringEntity httpentity = new StringEntity(s, ContentType.TEXT_PLAIN);
47
48 final byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
49 Assertions.assertEquals(bytes.length, httpentity.getContentLength());
50 Assertions.assertNotNull(httpentity.getContent());
51 Assertions.assertTrue(httpentity.isRepeatable());
52 Assertions.assertFalse(httpentity.isStreaming());
53 }
54
55 @Test
56 public void testNullConstructor() throws Exception {
57 Assertions.assertThrows(NullPointerException.class, () -> new StringEntity(null));
58 }
59
60 @Test
61 public void testDefaultContent() throws Exception {
62 final String s = "Message content";
63 StringEntity httpentity = new StringEntity(s, ContentType.create("text/csv", "ANSI_X3.4-1968"));
64 Assertions.assertEquals("text/csv; charset=US-ASCII", httpentity.getContentType());
65 httpentity = new StringEntity(s, StandardCharsets.US_ASCII);
66 Assertions.assertEquals("text/plain; charset=US-ASCII", httpentity.getContentType());
67 httpentity = new StringEntity(s);
68 Assertions.assertEquals("text/plain; charset=ISO-8859-1", httpentity.getContentType());
69 }
70
71 private static String constructString(final int [] unicodeChars) {
72 final StringBuilder buffer = new StringBuilder();
73 if (unicodeChars != null) {
74 for (final int unicodeChar : unicodeChars) {
75 buffer.append((char)unicodeChar);
76 }
77 }
78 return buffer.toString();
79 }
80
81 static final int SWISS_GERMAN_HELLO [] = {
82 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
83 };
84
85 @Test
86 public void testNullCharset() throws Exception {
87 final String s = constructString(SWISS_GERMAN_HELLO);
88 StringEntity httpentity = new StringEntity(s, ContentType.create("text/plain", (Charset) null));
89 Assertions.assertNotNull(httpentity.getContentType());
90 Assertions.assertEquals("text/plain", httpentity.getContentType());
91 Assertions.assertEquals(s, EntityUtils.toString(httpentity));
92 httpentity = new StringEntity(s, (Charset) null);
93 Assertions.assertNotNull(httpentity.getContentType());
94 Assertions.assertEquals("text/plain", httpentity.getContentType());
95 Assertions.assertEquals(s, EntityUtils.toString(httpentity));
96 }
97
98 @Test
99 public void testWriteTo() throws Exception {
100 final String s = "Message content";
101 final byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
102 final StringEntity httpentity = new StringEntity(s);
103
104 ByteArrayOutputStream out = new ByteArrayOutputStream();
105 httpentity.writeTo(out);
106 byte[] bytes2 = out.toByteArray();
107 Assertions.assertNotNull(bytes2);
108 Assertions.assertEquals(bytes.length, bytes2.length);
109 for (int i = 0; i < bytes.length; i++) {
110 Assertions.assertEquals(bytes[i], bytes2[i]);
111 }
112
113 out = new ByteArrayOutputStream();
114 httpentity.writeTo(out);
115 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 Assertions.assertThrows(NullPointerException.class, () -> httpentity.writeTo(null));
123 }
124
125 }