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.ByteArrayOutputStream;
32  import java.nio.charset.StandardCharsets;
33  
34  import org.junit.Assert;
35  import org.junit.Test;
36  
37  /**
38   * Unit tests for {@link BufferedHttpEntity}.
39   *
40   */
41  public class TestBufferedHttpEntity {
42  
43      @Test
44      public void testBufferingEntity() throws Exception {
45          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
46          final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1);
47          final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
48          Assert.assertEquals(bytes.length, bufentity.getContentLength());
49          Assert.assertTrue(bufentity.isRepeatable());
50          Assert.assertFalse(bufentity.isChunked());
51          Assert.assertFalse(bufentity.isStreaming());
52  
53          // test if we can obtain contain multiple times
54          Assert.assertNotNull(bufentity.getContent ());
55          Assert.assertNotNull(bufentity.getContent ());
56      }
57  
58      @Test
59      public void testWrappingEntity() throws Exception {
60          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
61          final ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
62          httpentity.setChunked(true);
63          final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
64          Assert.assertEquals(bytes.length, bufentity.getContentLength());
65          Assert.assertTrue(bufentity.isRepeatable());
66          Assert.assertTrue(bufentity.isChunked());
67          Assert.assertFalse(bufentity.isStreaming());
68  
69          // test if we can obtain contain multiple times
70          Assert.assertNotNull(bufentity.getContent ());
71          Assert.assertNotNull(bufentity.getContent ());
72      }
73  
74      @Test
75      public void testIllegalConstructor() throws Exception {
76          try {
77              new BufferedHttpEntity(null);
78              Assert.fail("IllegalArgumentException should have been thrown");
79          } catch (final IllegalArgumentException ex) {
80              // expected
81          }
82      }
83  
84      @Test
85      public void testWriteToBuffered() throws Exception {
86          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
87          final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1);
88          final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
89  
90          ByteArrayOutputStream out = new ByteArrayOutputStream();
91          bufentity.writeTo(out);
92          byte[] bytes2 = out.toByteArray();
93          Assert.assertNotNull(bytes2);
94          Assert.assertEquals(bytes.length, bytes2.length);
95          for (int i = 0; i < bytes.length; i++) {
96              Assert.assertEquals(bytes[i], bytes2[i]);
97          }
98  
99          out = new ByteArrayOutputStream();
100         bufentity.writeTo(out);
101         bytes2 = out.toByteArray();
102         Assert.assertNotNull(bytes2);
103         Assert.assertEquals(bytes.length, bytes2.length);
104         for (int i = 0; i < bytes.length; i++) {
105             Assert.assertEquals(bytes[i], bytes2[i]);
106         }
107 
108         try {
109             bufentity.writeTo(null);
110             Assert.fail("IllegalArgumentException should have been thrown");
111         } catch (final IllegalArgumentException ex) {
112             // expected
113         }
114     }
115 
116     @Test
117     public void testWriteToWrapped() throws Exception {
118         final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
119         final ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
120         final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
121 
122         ByteArrayOutputStream out = new ByteArrayOutputStream();
123         bufentity.writeTo(out);
124         byte[] bytes2 = out.toByteArray();
125         Assert.assertNotNull(bytes2);
126         Assert.assertEquals(bytes.length, bytes2.length);
127         for (int i = 0; i < bytes.length; i++) {
128             Assert.assertEquals(bytes[i], bytes2[i]);
129         }
130 
131         out = new ByteArrayOutputStream();
132         bufentity.writeTo(out);
133         bytes2 = out.toByteArray();
134         Assert.assertNotNull(bytes2);
135         Assert.assertEquals(bytes.length, bytes2.length);
136         for (int i = 0; i < bytes.length; i++) {
137             Assert.assertEquals(bytes[i], bytes2[i]);
138         }
139 
140         try {
141             bufentity.writeTo(null);
142             Assert.fail("IllegalArgumentException should have been thrown");
143         } catch (final IllegalArgumentException ex) {
144             // expected
145         }
146     }
147 
148 }