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