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.http.util;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.InputStream;
32 import java.nio.charset.Charset;
33
34 import org.apache.http.Consts;
35 import org.apache.http.entity.BasicHttpEntity;
36 import org.apache.http.message.BasicHeader;
37 import org.junit.Assert;
38 import org.junit.Test;
39
40
41
42
43
44 public class TestEntityUtils {
45
46 @Test
47 public void testNullEntityToByteArray() throws Exception {
48 try {
49 EntityUtils.toByteArray(null);
50 Assert.fail("IllegalArgumentException should have been thrown");
51 } catch (final IllegalArgumentException ex) {
52
53 }
54 }
55
56 @Test
57 public void testEmptyContentToByteArray() throws Exception {
58 final NullHttpEntity httpentity = new NullHttpEntity();
59 final byte[] bytes = EntityUtils.toByteArray(httpentity);
60 Assert.assertNull(bytes);
61 }
62
63 @Test
64 public void testMaxIntContentToByteArray() throws Exception {
65 final byte[] content = "Message content".getBytes(Consts.ISO_8859_1);
66 final BasicHttpEntity httpentity = new BasicHttpEntity();
67 httpentity.setContent(new ByteArrayInputStream(content));
68 httpentity.setContentLength(Integer.MAX_VALUE + 100L);
69 try {
70 EntityUtils.toByteArray(httpentity);
71 Assert.fail("IllegalArgumentException should have been thrown");
72 } catch (final IllegalArgumentException ex) {
73
74 }
75 }
76
77 @Test
78 public void testUnknownLengthContentToByteArray() throws Exception {
79 final byte[] bytes = "Message content".getBytes(Consts.ISO_8859_1);
80 final BasicHttpEntity httpentity = new BasicHttpEntity();
81 httpentity.setContent(new ByteArrayInputStream(bytes));
82 httpentity.setContentLength(-1L);
83 final byte[] bytes2 = EntityUtils.toByteArray(httpentity);
84 Assert.assertNotNull(bytes2);
85 Assert.assertEquals(bytes.length, bytes2.length);
86 for (int i = 0; i < bytes.length; i++) {
87 Assert.assertEquals(bytes[i], bytes2[i]);
88 }
89 }
90
91 @Test
92 public void testKnownLengthContentToByteArray() throws Exception {
93 final byte[] bytes = "Message content".getBytes(Consts.ISO_8859_1);
94 final BasicHttpEntity httpentity = new BasicHttpEntity();
95 httpentity.setContent(new ByteArrayInputStream(bytes));
96 httpentity.setContentLength(bytes.length);
97 final byte[] bytes2 = EntityUtils.toByteArray(httpentity);
98 Assert.assertNotNull(bytes2);
99 Assert.assertEquals(bytes.length, bytes2.length);
100 for (int i = 0; i < bytes.length; i++) {
101 Assert.assertEquals(bytes[i], bytes2[i]);
102 }
103 }
104
105 @Test
106 public void testNullEntityToString() throws Exception {
107 try {
108 EntityUtils.toString(null);
109 Assert.fail("IllegalArgumentException should have been thrown");
110 } catch (final IllegalArgumentException ex) {
111
112 }
113 }
114
115 @Test
116 public void testEmptyContentToString() throws Exception {
117 final NullHttpEntity httpentity = new NullHttpEntity();
118 final String s = EntityUtils.toString(httpentity);
119 Assert.assertNull(s);
120 }
121
122 @Test
123 public void testMaxIntContentToString() throws Exception {
124 final byte[] content = "Message content".getBytes(Consts.ISO_8859_1);
125 final BasicHttpEntity httpentity = new BasicHttpEntity();
126 httpentity.setContent(new ByteArrayInputStream(content));
127 httpentity.setContentLength(Integer.MAX_VALUE + 100L);
128 try {
129 EntityUtils.toString(httpentity);
130 Assert.fail("IllegalArgumentException should have been thrown");
131 } catch (final IllegalArgumentException ex) {
132
133 }
134 }
135
136 @Test
137 public void testUnknownLengthContentToString() throws Exception {
138 final byte[] bytes = "Message content".getBytes(Consts.ISO_8859_1);
139 final BasicHttpEntity httpentity = new BasicHttpEntity();
140 httpentity.setContent(new ByteArrayInputStream(bytes));
141 httpentity.setContentLength(-1L);
142 final String s = EntityUtils.toString(httpentity, "ISO-8859-1");
143 Assert.assertEquals("Message content", s);
144 }
145
146 @Test
147 public void testKnownLengthContentToString() throws Exception {
148 final byte[] bytes = "Message content".getBytes(Consts.ISO_8859_1);
149 final BasicHttpEntity httpentity = new BasicHttpEntity();
150 httpentity.setContent(new ByteArrayInputStream(bytes));
151 httpentity.setContentLength(bytes.length);
152 final String s = EntityUtils.toString(httpentity, "ISO-8859-1");
153 Assert.assertEquals("Message content", s);
154 }
155
156 static final int SWISS_GERMAN_HELLO [] = {
157 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
158 };
159
160 static final int RUSSIAN_HELLO [] = {
161 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
162 0x432, 0x435, 0x442
163 };
164
165 private static String constructString(final int [] unicodeChars) {
166 final StringBuilder buffer = new StringBuilder();
167 if (unicodeChars != null) {
168 for (final int unicodeChar : unicodeChars) {
169 buffer.append((char)unicodeChar);
170 }
171 }
172 return buffer.toString();
173 }
174
175 @Test
176 public void testNoCharsetContentToString() throws Exception {
177 final String content = constructString(SWISS_GERMAN_HELLO);
178 final byte[] bytes = content.getBytes(Consts.ISO_8859_1);
179 final BasicHttpEntity httpentity = new BasicHttpEntity();
180 httpentity.setContent(new ByteArrayInputStream(bytes));
181 httpentity.setContentType(new BasicHeader("Content-Type", "text/plain"));
182 final String s = EntityUtils.toString(httpentity);
183 Assert.assertEquals(content, s);
184 }
185
186 @Test
187 public void testDefaultCharsetContentToString() throws Exception {
188 final String content = constructString(RUSSIAN_HELLO);
189 final byte[] bytes = content.getBytes(Charset.forName("KOI8-R"));
190 final BasicHttpEntity httpentity = new BasicHttpEntity();
191 httpentity.setContent(new ByteArrayInputStream(bytes));
192 httpentity.setContentType(new BasicHeader("Content-Type", "text/plain"));
193 final String s = EntityUtils.toString(httpentity, "KOI8-R");
194 Assert.assertEquals(content, s);
195 }
196
197 @Test
198 public void testContentWithContentTypeToString() throws Exception {
199 final String content = constructString(RUSSIAN_HELLO);
200 final byte[] bytes = content.getBytes(Consts.UTF_8);
201 final BasicHttpEntity httpentity = new BasicHttpEntity();
202 httpentity.setContent(new ByteArrayInputStream(bytes));
203 httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset=UTF-8"));
204 final String s = EntityUtils.toString(httpentity, "ISO-8859-1");
205 Assert.assertEquals(content, s);
206 }
207 @Test
208 public void testContentWithInvalidContentTypeToString() throws Exception {
209 final String content = constructString(RUSSIAN_HELLO);
210 final byte[] bytes = content.getBytes("UTF-8");
211 final BasicHttpEntity httpentity = new BasicHttpEntity();
212 httpentity.setContent(new ByteArrayInputStream(bytes));
213 httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset=nosuchcharset"));
214 final String s = EntityUtils.toString(httpentity, "UTF-8");
215 Assert.assertEquals(content, s);
216 }
217
218
219
220
221 public static class NullHttpEntity extends BasicHttpEntity {
222
223
224
225
226
227
228
229
230 @Override
231 public InputStream getContent() {
232 return null;
233 }
234 }
235
236 }