View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.io.entity;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.nio.charset.Charset;
34  import java.nio.charset.StandardCharsets;
35  import java.util.ArrayList;
36  import java.util.Arrays;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import org.apache.hc.core5.http.ContentType;
42  import org.apache.hc.core5.http.HttpEntity;
43  import org.apache.hc.core5.http.NameValuePair;
44  import org.apache.hc.core5.http.ParseException;
45  import org.apache.hc.core5.http.message.BasicNameValuePair;
46  import org.apache.hc.core5.net.WWWFormCodec;
47  import org.junit.Assert;
48  import org.junit.Test;
49  
50  /**
51   * Unit tests for {@link EntityUtils}.
52   *
53   */
54  public class TestEntityUtils {
55  
56      @Test(expected = NullPointerException.class)
57      public void testNullEntityToByteArray() throws Exception {
58          EntityUtils.toByteArray(null);
59      }
60  
61      @Test(expected = IllegalArgumentException.class)
62      public void testMaxIntContentToByteArray() throws Exception {
63          final byte[] content = "Message content".getBytes(StandardCharsets.ISO_8859_1);
64          final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(content),
65                  Integer.MAX_VALUE + 100L, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
66          EntityUtils.toByteArray(entity);
67      }
68  
69      @Test
70      public void testUnknownLengthContentToByteArray() throws Exception {
71          final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
72          final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), -1, null);
73          final byte[] bytes2 = EntityUtils.toByteArray(entity);
74          Assert.assertNotNull(bytes2);
75          Assert.assertEquals(bytes.length, bytes2.length);
76          for (int i = 0; i < bytes.length; i++) {
77              Assert.assertEquals(bytes[i], bytes2[i]);
78          }
79      }
80  
81      @Test
82      public void testKnownLengthContentToByteArray() throws Exception {
83          final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
84          final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length, null);
85          final byte[] bytes2 = EntityUtils.toByteArray(entity);
86          Assert.assertNotNull(bytes2);
87          Assert.assertEquals(bytes.length, bytes2.length);
88          for (int i = 0; i < bytes.length; i++) {
89              Assert.assertEquals(bytes[i], bytes2[i]);
90          }
91      }
92  
93      @Test
94      public void testNullEntityToString() throws Exception {
95          try {
96              EntityUtils.toString(null);
97              Assert.fail("NullPointerException should have been thrown");
98          } catch (final NullPointerException ex) {
99              // expected
100         }
101     }
102 
103     @Test(expected = IllegalArgumentException.class)
104     public void testMaxIntContentToString() throws Exception {
105         final byte[] content = "Message content".getBytes(StandardCharsets.ISO_8859_1);
106         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(content),
107                 Integer.MAX_VALUE + 100L, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
108         EntityUtils.toString(entity);
109     }
110 
111     @Test
112     public void testUnknownLengthContentToString() throws Exception {
113         final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
114         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), -1, null);
115         final String s = EntityUtils.toString(entity, "ISO-8859-1");
116         Assert.assertEquals("Message content", s);
117     }
118 
119     @Test
120     public void testKnownLengthContentToString() throws Exception {
121         final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
122         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length,
123                 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
124         final String s = EntityUtils.toString(entity, StandardCharsets.ISO_8859_1);
125         Assert.assertEquals("Message content", s);
126     }
127 
128     static final int SWISS_GERMAN_HELLO [] = {
129         0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
130     };
131 
132     static final int RUSSIAN_HELLO [] = {
133         0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
134         0x432, 0x435, 0x442
135     };
136 
137     private static String constructString(final int [] unicodeChars) {
138         final StringBuilder buffer = new StringBuilder();
139         if (unicodeChars != null) {
140             for (final int unicodeChar : unicodeChars) {
141                 buffer.append((char)unicodeChar);
142             }
143         }
144         return buffer.toString();
145     }
146 
147     @Test
148     public void testNoCharsetContentToString() throws Exception {
149         final String content = constructString(SWISS_GERMAN_HELLO);
150         final byte[] bytes = content.getBytes(StandardCharsets.ISO_8859_1);
151         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), ContentType.TEXT_PLAIN);
152         final String s = EntityUtils.toString(entity);
153     }
154 
155     @Test
156     public void testDefaultCharsetContentToString() throws Exception {
157         final String content = constructString(RUSSIAN_HELLO);
158         final byte[] bytes = content.getBytes(Charset.forName("KOI8-R"));
159         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes),
160                 ContentType.parse("text/plain"));
161         final String s = EntityUtils.toString(entity, Charset.forName("KOI8-R"));
162         Assert.assertEquals(content, s);
163     }
164 
165     @Test
166     public void testContentWithContentTypeToString() throws Exception {
167         final String content = constructString(RUSSIAN_HELLO);
168         final byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
169         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes),
170                 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8));
171         final String s = EntityUtils.toString(entity, "ISO-8859-1");
172         Assert.assertEquals(content, s);
173     }
174 
175     @Test
176     public void testContentWithInvalidContentTypeToString() throws Exception {
177         final String content = constructString(RUSSIAN_HELLO);
178         final byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
179         final HttpEntity entity = new AbstractHttpEntity("text/plain; charset=nosuchcharset", null) {
180 
181             @Override
182             public InputStream getContent() throws IOException, UnsupportedOperationException {
183                 return new ByteArrayInputStream(bytes);
184             }
185 
186             @Override
187             public boolean isStreaming() {
188                 return false;
189             }
190 
191             @Override
192             public long getContentLength() {
193                 return bytes.length;
194             }
195 
196             @Override
197             public void close() throws IOException {
198             }
199 
200         };
201         final String s = EntityUtils.toString(entity, "UTF-8");
202         Assert.assertEquals(content, s);
203     }
204 
205     private static void assertNameValuePair (
206             final NameValuePair parameter,
207             final String expectedName,
208             final String expectedValue) {
209         Assert.assertEquals(parameter.getName(), expectedName);
210         Assert.assertEquals(parameter.getValue(), expectedValue);
211     }
212 
213     @Test
214     public void testParseEntity() throws Exception {
215         final StringEntity entity1 = new StringEntity("Name1=Value1", ContentType.APPLICATION_FORM_URLENCODED);
216         final List<NameValuePair> result = EntityUtils.parse(entity1);
217         Assert.assertEquals(1, result.size());
218         assertNameValuePair(result.get(0), "Name1", "Value1");
219 
220         final StringEntity entity2 = new StringEntity("Name1=Value1", ContentType.parse("text/test"));
221         Assert.assertTrue(EntityUtils.parse(entity2).isEmpty());
222     }
223 
224     @Test
225     public void testParseUTF8Entity() throws Exception {
226         final String ru_hello = constructString(RUSSIAN_HELLO);
227         final String ch_hello = constructString(SWISS_GERMAN_HELLO);
228         final List <NameValuePair> parameters = new ArrayList<>();
229         parameters.add(new BasicNameValuePair("russian", ru_hello));
230         parameters.add(new BasicNameValuePair("swiss", ch_hello));
231 
232         final String s = WWWFormCodec.format(parameters, StandardCharsets.UTF_8);
233 
234         Assert.assertEquals("russian=%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82" +
235                 "&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", s);
236         final StringEntity entity = new StringEntity(s,
237                 ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8));
238         final List<NameValuePair> result = EntityUtils.parse(entity);
239         Assert.assertEquals(2, result.size());
240         assertNameValuePair(result.get(0), "russian", ru_hello);
241         assertNameValuePair(result.get(1), "swiss", ch_hello);
242     }
243 
244     @Test
245     public void testByteArrayMaxResultLength() throws IOException {
246         final byte[] allBytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
247         final Map<Integer, byte[]> testCases = new HashMap<>();
248         testCases.put(0, new byte[]{});
249         testCases.put(2, Arrays.copyOfRange(allBytes, 0, 2));
250         testCases.put(allBytes.length, allBytes);
251         testCases.put(Integer.MAX_VALUE, allBytes);
252 
253         for (final Map.Entry<Integer, byte[]> tc : testCases.entrySet()) {
254             final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(allBytes), allBytes.length, null);
255 
256             final byte[] bytes = EntityUtils.toByteArray(entity, tc.getKey());
257             final byte[] expectedBytes = tc.getValue();
258             Assert.assertNotNull(bytes);
259             Assert.assertEquals(expectedBytes.length, bytes.length);
260             for (int i = 0; i < expectedBytes.length; i++) {
261                 Assert.assertEquals(expectedBytes[i], bytes[i]);
262             }
263         }
264     }
265 
266     @Test
267     public void testStringMaxResultLength() throws IOException, ParseException {
268         final String allMessage = "Message content";
269         final byte[] allBytes = allMessage.getBytes(StandardCharsets.ISO_8859_1);
270         final Map<Integer, String> testCases = new HashMap<>();
271         testCases.put(7, allMessage.substring(0, 7));
272         testCases.put(allMessage.length(), allMessage);
273         testCases.put(Integer.MAX_VALUE, allMessage);
274 
275         for (final Map.Entry<Integer, String> tc : testCases.entrySet()) {
276             final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(allBytes), allBytes.length, null);
277             final String string = EntityUtils.toString(entity, StandardCharsets.ISO_8859_1, tc.getKey());
278             final String expectedString = tc.getValue();
279             Assert.assertNotNull(string);
280             Assert.assertEquals(expectedString, string);
281         }
282     }
283 
284 }