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.jupiter.api.Assertions;
48  import org.junit.jupiter.api.Test;
49  
50  /**
51   * Unit tests for {@link EntityUtils}.
52   *
53   */
54  public class TestEntityUtils {
55  
56      @Test
57      public void testNullEntityToByteArray() throws Exception {
58          Assertions.assertThrows(NullPointerException.class, () ->
59                  EntityUtils.toByteArray(null));
60      }
61  
62      @Test
63      public void testMaxIntContentToByteArray() throws Exception {
64          final byte[] content = "Message content".getBytes(StandardCharsets.ISO_8859_1);
65          final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(content),
66                  Integer.MAX_VALUE + 100L, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
67          Assertions.assertThrows(IllegalArgumentException.class, () ->
68                  EntityUtils.toByteArray(entity));
69      }
70  
71      @Test
72      public void testUnknownLengthContentToByteArray() throws Exception {
73          final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
74          final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), -1, null);
75          final byte[] bytes2 = EntityUtils.toByteArray(entity);
76          Assertions.assertNotNull(bytes2);
77          Assertions.assertEquals(bytes.length, bytes2.length);
78          for (int i = 0; i < bytes.length; i++) {
79              Assertions.assertEquals(bytes[i], bytes2[i]);
80          }
81      }
82  
83      @Test
84      public void testKnownLengthContentToByteArray() throws Exception {
85          final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
86          final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length, null);
87          final byte[] bytes2 = EntityUtils.toByteArray(entity);
88          Assertions.assertNotNull(bytes2);
89          Assertions.assertEquals(bytes.length, bytes2.length);
90          for (int i = 0; i < bytes.length; i++) {
91              Assertions.assertEquals(bytes[i], bytes2[i]);
92          }
93      }
94  
95      @Test
96      public void testNullEntityToString() throws Exception {
97          Assertions.assertThrows(NullPointerException.class, () -> EntityUtils.toString(null));
98      }
99  
100     @Test
101     public void testMaxIntContentToString() throws Exception {
102         final byte[] content = "Message content".getBytes(StandardCharsets.ISO_8859_1);
103         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(content),
104                 Integer.MAX_VALUE + 100L, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
105         Assertions.assertThrows(IllegalArgumentException.class, () ->
106                 EntityUtils.toString(entity));
107     }
108 
109     @Test
110     public void testUnknownLengthContentToString() throws Exception {
111         final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
112         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), -1, null);
113         final String s = EntityUtils.toString(entity, "ISO-8859-1");
114         Assertions.assertEquals("Message content", s);
115     }
116 
117     @Test
118     public void testKnownLengthContentToString() throws Exception {
119         final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
120         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length,
121                 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
122         final String s = EntityUtils.toString(entity, StandardCharsets.ISO_8859_1);
123         Assertions.assertEquals("Message content", s);
124     }
125 
126     static final int SWISS_GERMAN_HELLO [] = {
127         0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
128     };
129 
130     static final int RUSSIAN_HELLO [] = {
131         0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
132         0x432, 0x435, 0x442
133     };
134 
135     private static String constructString(final int [] unicodeChars) {
136         final StringBuilder buffer = new StringBuilder();
137         if (unicodeChars != null) {
138             for (final int unicodeChar : unicodeChars) {
139                 buffer.append((char)unicodeChar);
140             }
141         }
142         return buffer.toString();
143     }
144 
145     @Test
146     public void testNoCharsetContentToString() throws Exception {
147         final String content = constructString(SWISS_GERMAN_HELLO);
148         final byte[] bytes = content.getBytes(StandardCharsets.ISO_8859_1);
149         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), ContentType.TEXT_PLAIN);
150         final String s = EntityUtils.toString(entity);
151     }
152 
153     @Test
154     public void testDefaultCharsetContentToString() throws Exception {
155         final String content = constructString(RUSSIAN_HELLO);
156         final byte[] bytes = content.getBytes(Charset.forName("KOI8-R"));
157         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes),
158                 ContentType.parse("text/plain"));
159         final String s = EntityUtils.toString(entity, Charset.forName("KOI8-R"));
160         Assertions.assertEquals(content, s);
161     }
162 
163     @Test
164     public void testContentWithContentTypeToString() throws Exception {
165         final String content = constructString(RUSSIAN_HELLO);
166         final byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
167         final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes),
168                 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8));
169         final String s = EntityUtils.toString(entity, "ISO-8859-1");
170         Assertions.assertEquals(content, s);
171     }
172 
173     @Test
174     public void testContentWithInvalidContentTypeToString() throws Exception {
175         final String content = constructString(RUSSIAN_HELLO);
176         final byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
177         final HttpEntity entity = new AbstractHttpEntity("text/plain; charset=nosuchcharset", null) {
178 
179             @Override
180             public InputStream getContent() throws IOException, UnsupportedOperationException {
181                 return new ByteArrayInputStream(bytes);
182             }
183 
184             @Override
185             public boolean isStreaming() {
186                 return false;
187             }
188 
189             @Override
190             public long getContentLength() {
191                 return bytes.length;
192             }
193 
194             @Override
195             public void close() throws IOException {
196             }
197 
198         };
199         final String s = EntityUtils.toString(entity, "UTF-8");
200         Assertions.assertEquals(content, s);
201     }
202 
203     private static void assertNameValuePair (
204             final NameValuePair parameter,
205             final String expectedName,
206             final String expectedValue) {
207         Assertions.assertEquals(parameter.getName(), expectedName);
208         Assertions.assertEquals(parameter.getValue(), expectedValue);
209     }
210 
211     @Test
212     public void testParseEntity() throws Exception {
213         final StringEntity entity1 = new StringEntity("Name1=Value1", ContentType.APPLICATION_FORM_URLENCODED);
214         final List<NameValuePair> result = EntityUtils.parse(entity1);
215         Assertions.assertEquals(1, result.size());
216         assertNameValuePair(result.get(0), "Name1", "Value1");
217 
218         final StringEntity entity2 = new StringEntity("Name1=Value1", ContentType.parse("text/test"));
219         Assertions.assertTrue(EntityUtils.parse(entity2).isEmpty());
220     }
221 
222     @Test
223     public void testParseUTF8Entity() throws Exception {
224         final String ru_hello = constructString(RUSSIAN_HELLO);
225         final String ch_hello = constructString(SWISS_GERMAN_HELLO);
226         final List <NameValuePair> parameters = new ArrayList<>();
227         parameters.add(new BasicNameValuePair("russian", ru_hello));
228         parameters.add(new BasicNameValuePair("swiss", ch_hello));
229 
230         final String s = WWWFormCodec.format(parameters, StandardCharsets.UTF_8);
231 
232         Assertions.assertEquals("russian=%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82" +
233                 "&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", s);
234         final StringEntity entity = new StringEntity(s,
235                 ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8));
236         final List<NameValuePair> result = EntityUtils.parse(entity);
237         Assertions.assertEquals(2, result.size());
238         assertNameValuePair(result.get(0), "russian", ru_hello);
239         assertNameValuePair(result.get(1), "swiss", ch_hello);
240     }
241 
242     @Test
243     public void testByteArrayMaxResultLength() throws IOException {
244         final byte[] allBytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
245         final Map<Integer, byte[]> testCases = new HashMap<>();
246         testCases.put(0, new byte[]{});
247         testCases.put(2, Arrays.copyOfRange(allBytes, 0, 2));
248         testCases.put(allBytes.length, allBytes);
249         testCases.put(Integer.MAX_VALUE, allBytes);
250 
251         for (final Map.Entry<Integer, byte[]> tc : testCases.entrySet()) {
252             final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(allBytes), allBytes.length, null);
253 
254             final byte[] bytes = EntityUtils.toByteArray(entity, tc.getKey());
255             final byte[] expectedBytes = tc.getValue();
256             Assertions.assertNotNull(bytes);
257             Assertions.assertEquals(expectedBytes.length, bytes.length);
258             for (int i = 0; i < expectedBytes.length; i++) {
259                 Assertions.assertEquals(expectedBytes[i], bytes[i]);
260             }
261         }
262     }
263 
264     @Test
265     public void testByteArrayMaxResultLengthWithNoContentLength() throws IOException {
266         final byte b = 'b';
267         final byte[] allBytes = new byte[5000];
268         Arrays.fill(allBytes, b);
269         final Map<Integer, byte[]> testCases = new HashMap<>();
270         testCases.put(0, new byte[]{});
271         testCases.put(2, Arrays.copyOfRange(allBytes, 0, 2));
272         testCases.put(allBytes.length, allBytes);
273         testCases.put(Integer.MAX_VALUE, allBytes);
274 
275         for (final Map.Entry<Integer, byte[]> tc : testCases.entrySet()) {
276             final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(allBytes), null);
277 
278             final byte[] bytes = EntityUtils.toByteArray(entity, tc.getKey());
279             final byte[] expectedBytes = tc.getValue();
280             Assertions.assertNotNull(bytes);
281             Assertions.assertEquals(expectedBytes.length, bytes.length);
282             for (int i = 0; i < expectedBytes.length; i++) {
283                 Assertions.assertEquals(expectedBytes[i], bytes[i]);
284             }
285         }
286     }
287 
288     @Test
289     public void testStringMaxResultLength() throws IOException, ParseException {
290         final String allMessage = "Message content";
291         final byte[] allBytes = allMessage.getBytes(StandardCharsets.ISO_8859_1);
292         final Map<Integer, String> testCases = new HashMap<>();
293         testCases.put(7, allMessage.substring(0, 7));
294         testCases.put(allMessage.length(), allMessage);
295         testCases.put(Integer.MAX_VALUE, allMessage);
296 
297         for (final Map.Entry<Integer, String> tc : testCases.entrySet()) {
298             final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(allBytes), allBytes.length, null);
299             final String string = EntityUtils.toString(entity, StandardCharsets.ISO_8859_1, tc.getKey());
300             final String expectedString = tc.getValue();
301             Assertions.assertNotNull(string);
302             Assertions.assertEquals(expectedString, string);
303         }
304     }
305 
306 }