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.jupiter.api.Assertions;
35  import org.junit.jupiter.api.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 BufferedHttpEntity entity = new BufferedHttpEntity(
47                  new InputStreamEntity(new ByteArrayInputStream(bytes), -1, null));
48          Assertions.assertEquals(bytes.length, entity.getContentLength());
49          Assertions.assertTrue(entity.isRepeatable());
50          Assertions.assertFalse(entity.isChunked());
51          Assertions.assertFalse(entity.isStreaming());
52  
53          // test if we can obtain contain multiple times
54          Assertions.assertNotNull(entity.getContent ());
55          Assertions.assertNotNull(entity.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, null, true);
62          final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
63          Assertions.assertEquals(bytes.length, bufentity.getContentLength());
64          Assertions.assertTrue(bufentity.isRepeatable());
65          Assertions.assertTrue(bufentity.isChunked());
66          Assertions.assertFalse(bufentity.isStreaming());
67  
68          // test if we can obtain contain multiple times
69          Assertions.assertNotNull(bufentity.getContent ());
70          Assertions.assertNotNull(bufentity.getContent ());
71      }
72  
73      @Test
74      public void testIllegalConstructor() throws Exception {
75          Assertions.assertThrows(NullPointerException.class, () -> new BufferedHttpEntity(null));
76      }
77  
78      @Test
79      public void testWriteToBuffered() throws Exception {
80          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
81          final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1, null);
82          final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
83  
84          ByteArrayOutputStream out = new ByteArrayOutputStream();
85          bufentity.writeTo(out);
86          byte[] bytes2 = out.toByteArray();
87          Assertions.assertNotNull(bytes2);
88          Assertions.assertEquals(bytes.length, bytes2.length);
89          for (int i = 0; i < bytes.length; i++) {
90              Assertions.assertEquals(bytes[i], bytes2[i]);
91          }
92  
93          out = new ByteArrayOutputStream();
94          bufentity.writeTo(out);
95          bytes2 = out.toByteArray();
96          Assertions.assertNotNull(bytes2);
97          Assertions.assertEquals(bytes.length, bytes2.length);
98          for (int i = 0; i < bytes.length; i++) {
99              Assertions.assertEquals(bytes[i], bytes2[i]);
100         }
101 
102         Assertions.assertThrows(NullPointerException.class, () -> bufentity.writeTo(null));
103     }
104 
105     @Test
106     public void testWriteToWrapped() throws Exception {
107         final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
108         final ByteArrayEntity httpentity = new ByteArrayEntity(bytes, null);
109         final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
110 
111         ByteArrayOutputStream out = new ByteArrayOutputStream();
112         bufentity.writeTo(out);
113         byte[] bytes2 = out.toByteArray();
114         Assertions.assertNotNull(bytes2);
115         Assertions.assertEquals(bytes.length, bytes2.length);
116         for (int i = 0; i < bytes.length; i++) {
117             Assertions.assertEquals(bytes[i], bytes2[i]);
118         }
119 
120         out = new ByteArrayOutputStream();
121         bufentity.writeTo(out);
122         bytes2 = out.toByteArray();
123         Assertions.assertNotNull(bytes2);
124         Assertions.assertEquals(bytes.length, bytes2.length);
125         for (int i = 0; i < bytes.length; i++) {
126             Assertions.assertEquals(bytes[i], bytes2[i]);
127         }
128 
129         Assertions.assertThrows(NullPointerException.class, () -> bufentity.writeTo(null));
130     }
131 
132 }