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.Assert;
36 import org.junit.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 Assert.assertEquals(bytes.length, httpentity.getContentLength());
50 Assert.assertNotNull(httpentity.getContent());
51 Assert.assertTrue(httpentity.isRepeatable());
52 Assert.assertFalse(httpentity.isStreaming());
53 }
54
55 @Test
56 public void testNullConstructor() throws Exception {
57 try {
58 new StringEntity(null);
59 Assert.fail("NullPointerException should have been thrown");
60 } catch (final NullPointerException ex) {
61
62 }
63 }
64
65 @Test
66 public void testDefaultContent() throws Exception {
67 final String s = "Message content";
68 StringEntity httpentity = new StringEntity(s, ContentType.create("text/csv", "ANSI_X3.4-1968"));
69 Assert.assertEquals("text/csv; charset=US-ASCII", httpentity.getContentType());
70 httpentity = new StringEntity(s, StandardCharsets.US_ASCII);
71 Assert.assertEquals("text/plain; charset=US-ASCII", httpentity.getContentType());
72 httpentity = new StringEntity(s);
73 Assert.assertEquals("text/plain; charset=ISO-8859-1", httpentity.getContentType());
74 }
75
76 private static String constructString(final int [] unicodeChars) {
77 final StringBuilder buffer = new StringBuilder();
78 if (unicodeChars != null) {
79 for (final int unicodeChar : unicodeChars) {
80 buffer.append((char)unicodeChar);
81 }
82 }
83 return buffer.toString();
84 }
85
86 static final int SWISS_GERMAN_HELLO [] = {
87 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
88 };
89
90 @Test
91 public void testNullCharset() throws Exception {
92 final String s = constructString(SWISS_GERMAN_HELLO);
93 StringEntity httpentity = new StringEntity(s, ContentType.create("text/plain", (Charset) null));
94 Assert.assertNotNull(httpentity.getContentType());
95 Assert.assertEquals("text/plain", httpentity.getContentType());
96 Assert.assertEquals(s, EntityUtils.toString(httpentity));
97 httpentity = new StringEntity(s, (Charset) null);
98 Assert.assertNotNull(httpentity.getContentType());
99 Assert.assertEquals("text/plain", httpentity.getContentType());
100 Assert.assertEquals(s, EntityUtils.toString(httpentity));
101 }
102
103 @Test
104 public void testWriteTo() throws Exception {
105 final String s = "Message content";
106 final byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
107 final StringEntity httpentity = new StringEntity(s);
108
109 ByteArrayOutputStream out = new ByteArrayOutputStream();
110 httpentity.writeTo(out);
111 byte[] bytes2 = out.toByteArray();
112 Assert.assertNotNull(bytes2);
113 Assert.assertEquals(bytes.length, bytes2.length);
114 for (int i = 0; i < bytes.length; i++) {
115 Assert.assertEquals(bytes[i], bytes2[i]);
116 }
117
118 out = new ByteArrayOutputStream();
119 httpentity.writeTo(out);
120 bytes2 = out.toByteArray();
121 Assert.assertNotNull(bytes2);
122 Assert.assertEquals(bytes.length, bytes2.length);
123 for (int i = 0; i < bytes.length; i++) {
124 Assert.assertEquals(bytes[i], bytes2[i]);
125 }
126
127 try {
128 httpentity.writeTo(null);
129 Assert.fail("NullPointerException should have been thrown");
130 } catch (final NullPointerException ex) {
131
132 }
133 }
134
135 }