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.apache.hc.core5.http.ContentType;
35  import org.junit.jupiter.api.Assertions;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Unit tests for {@link BasicHttpEntity}.
40   *
41   */
42  public class TestBasicHttpEntity {
43  
44      @Test
45      public void testBasics() throws Exception {
46          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
47          final BasicHttpEntity httpentity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length, null);
48          Assertions.assertEquals(bytes.length, httpentity.getContentLength());
49          // always false
50          Assertions.assertFalse(httpentity.isRepeatable());
51          // always true
52          Assertions.assertTrue(httpentity.isStreaming());
53      }
54  
55      @Test
56      public void testConstructorNullContent() throws Exception {
57          // BasicHttpEntity(InputStream, ContentType)
58          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, ContentType.APPLICATION_ATOM_XML));
59          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, null));
60          // BasicHttpEntity(InputStream, ContentType, boolean)
61          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, ContentType.APPLICATION_ATOM_XML, false));
62          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, null, false));
63          // BasicHttpEntity(InputStream, ContentType, String)
64          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, ContentType.APPLICATION_ATOM_XML, ""));
65          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, null, ""));
66          // BasicHttpEntity(InputStream, long, ContentType)
67          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, ContentType.APPLICATION_ATOM_XML));
68          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, null));
69          // BasicHttpEntity(InputStream, long, ContentType, String)
70          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, ContentType.APPLICATION_ATOM_XML, ""));
71          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, null, ""));
72          // BasicHttpEntity(InputStream, long, ContentType, String, boolean)
73          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, ContentType.APPLICATION_ATOM_XML, "", false));
74          Assertions.assertThrows(NullPointerException.class, () -> new BasicHttpEntity(null, 0, null, "", false));
75      }
76  
77      @Test
78      public void testToString() throws Exception {
79          try (final BasicHttpEntity httpentity = new BasicHttpEntity(EmptyInputStream.INSTANCE, 10,
80                  ContentType.parseLenient("blah"), "yada", true)) {
81              Assertions.assertEquals(
82                      "[Entity-Class: BasicHttpEntity, Content-Type: blah, Content-Encoding: yada, chunked: true]",
83                      httpentity.toString());
84          }
85      }
86  
87      @Test
88      public void testWriteTo() throws Exception {
89          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
90          final BasicHttpEntity httpentity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length,
91                  ContentType.TEXT_PLAIN);
92          // always false
93          Assertions.assertFalse(httpentity.isRepeatable());
94          // always true
95          Assertions.assertTrue(httpentity.isStreaming());
96          final ByteArrayOutputStream out = new ByteArrayOutputStream();
97          httpentity.writeTo(out);
98          final byte[] bytes2 = out.toByteArray();
99          Assertions.assertNotNull(bytes2);
100         Assertions.assertEquals(bytes.length, bytes2.length);
101         for (int i = 0; i < bytes.length; i++) {
102             Assertions.assertEquals(bytes[i], bytes2[i]);
103         }
104     }
105 
106 }