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.entity;
29  
30  import java.io.ByteArrayOutputStream;
31  
32  import org.apache.http.Consts;
33  import org.apache.http.protocol.HTTP;
34  import org.apache.http.util.EntityUtils;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  /**
39   * Unit tests for {@link HttpEntityWrapper}.
40   *
41   */
42  public class TestHttpEntityWrapper {
43  
44      @Test
45      public void testBasics() throws Exception {
46          final String s = "Message content";
47          final StringEntity httpentity = new StringEntity(s, ContentType.TEXT_PLAIN);
48          httpentity.setContentEncoding(HTTP.IDENTITY_CODING);
49          final HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
50  
51          Assert.assertEquals(httpentity.getContentLength(), wrapped.getContentLength());
52          Assert.assertEquals(httpentity.getContentType(), wrapped.getContentType());
53          Assert.assertEquals(httpentity.getContentEncoding(), wrapped.getContentEncoding());
54          Assert.assertEquals(httpentity.isChunked(), wrapped.isChunked());
55          Assert.assertEquals(httpentity.isRepeatable(), wrapped.isRepeatable());
56          Assert.assertEquals(httpentity.isStreaming(), wrapped.isStreaming());
57          Assert.assertNotNull(wrapped.getContent());
58      }
59  
60      @Test
61      public void testIllegalConstructor() throws Exception {
62          try {
63              new HttpEntityWrapper(null);
64              Assert.fail("IllegalArgumentException should have been thrown");
65          } catch (final IllegalArgumentException ex) {
66              // expected
67          }
68      }
69  
70      @Test
71      public void testWriteTo() throws Exception {
72          final String s = "Message content";
73          final byte[] bytes = s.getBytes(Consts.ISO_8859_1);
74          final StringEntity httpentity = new StringEntity(s);
75          final HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
76  
77          ByteArrayOutputStream out = new ByteArrayOutputStream();
78          wrapped.writeTo(out);
79          byte[] bytes2 = out.toByteArray();
80          Assert.assertNotNull(bytes2);
81          Assert.assertEquals(bytes.length, bytes2.length);
82          for (int i = 0; i < bytes.length; i++) {
83              Assert.assertEquals(bytes[i], bytes2[i]);
84          }
85  
86          out = new ByteArrayOutputStream();
87          wrapped.writeTo(out);
88          bytes2 = out.toByteArray();
89          Assert.assertNotNull(bytes2);
90          Assert.assertEquals(bytes.length, bytes2.length);
91          for (int i = 0; i < bytes.length; i++) {
92              Assert.assertEquals(bytes[i], bytes2[i]);
93          }
94  
95          try {
96              wrapped.writeTo(null);
97              Assert.fail("IllegalArgumentException should have been thrown");
98          } catch (final IllegalArgumentException ex) {
99              // expected
100         }
101     }
102 
103     @Test
104     public void testConsumeContent() throws Exception {
105         final String s = "Message content";
106         final StringEntity httpentity = new StringEntity(s);
107         final HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
108         EntityUtils.consume(wrapped);
109         EntityUtils.consume(wrapped);
110     }
111 
112 }