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          try (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  
74      @Test
75      public void testIllegalConstructor() throws Exception {
76          Assertions.assertThrows(NullPointerException.class, () -> new BufferedHttpEntity(null));
77      }
78  
79      @Test
80      public void testWriteToBuffered() throws Exception {
81          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
82          final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1, null);
83          try (final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity)) {
84  
85              ByteArrayOutputStream out = new ByteArrayOutputStream();
86              bufentity.writeTo(out);
87              byte[] bytes2 = out.toByteArray();
88              Assertions.assertNotNull(bytes2);
89              Assertions.assertEquals(bytes.length, bytes2.length);
90              for (int i = 0; i < bytes.length; i++) {
91                  Assertions.assertEquals(bytes[i], bytes2[i]);
92              }
93  
94              out = new ByteArrayOutputStream();
95              bufentity.writeTo(out);
96              bytes2 = out.toByteArray();
97              Assertions.assertNotNull(bytes2);
98              Assertions.assertEquals(bytes.length, bytes2.length);
99              for (int i = 0; i < bytes.length; i++) {
100                 Assertions.assertEquals(bytes[i], bytes2[i]);
101             }
102 
103             Assertions.assertThrows(NullPointerException.class, () -> bufentity.writeTo(null));
104         }
105     }
106 
107     @Test
108     public void testWriteToWrapped() throws Exception {
109         final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
110         final ByteArrayEntity httpentity = new ByteArrayEntity(bytes, null);
111         try (final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity)) {
112 
113             ByteArrayOutputStream out = new ByteArrayOutputStream();
114             bufentity.writeTo(out);
115             byte[] bytes2 = out.toByteArray();
116             Assertions.assertNotNull(bytes2);
117             Assertions.assertEquals(bytes.length, bytes2.length);
118             for (int i = 0; i < bytes.length; i++) {
119                 Assertions.assertEquals(bytes[i], bytes2[i]);
120             }
121 
122             out = new ByteArrayOutputStream();
123             bufentity.writeTo(out);
124             bytes2 = out.toByteArray();
125             Assertions.assertNotNull(bytes2);
126             Assertions.assertEquals(bytes.length, bytes2.length);
127             for (int i = 0; i < bytes.length; i++) {
128                 Assertions.assertEquals(bytes[i], bytes2[i]);
129             }
130 
131             Assertions.assertThrows(NullPointerException.class, () -> bufentity.writeTo(null));
132         }
133     }
134 
135 }