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.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   * Unit tests for {@link EntityUtils}.
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              // expected
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              // expected
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             // expected
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             // expected
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      * Helper class that returns {@code null} as the content.
220      */
221     public static class NullHttpEntity extends BasicHttpEntity {
222 
223         // default constructor
224         /**
225          * Obtains no content.
226          * This method disables the state checks in the base class.
227          *
228          * @return {@code null}
229          */
230         @Override
231         public InputStream getContent() {
232             return null;
233         }
234     } // class NullEntity
235 
236 } // class TestEntityUtils