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