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.io.InputStream;
33  import java.nio.charset.StandardCharsets;
34  
35  import org.apache.hc.core5.http.ContentType;
36  import org.junit.jupiter.api.Assertions;
37  import org.junit.jupiter.api.Test;
38  
39  /**
40   * Unit tests for {@link InputStreamEntity}.
41   *
42   */
43  public class TestInputStreamEntity {
44  
45      @Test
46      public void testBasics() throws Exception {
47          final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1);
48          final InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(bytes), bytes.length, null);
49  
50          Assertions.assertEquals(bytes.length, entity.getContentLength());
51          Assertions.assertNotNull(entity.getContent());
52          Assertions.assertFalse(entity.isRepeatable());
53          Assertions.assertTrue(entity.isStreaming());
54      }
55  
56      @Test
57      public void testNullConstructor() throws Exception {
58          Assertions.assertThrows(NullPointerException.class, () ->
59                  new InputStreamEntity(null, 0, null));
60      }
61  
62      @Test
63      public void testUnknownLengthConstructor() throws Exception {
64          try (final InputStreamEntity entity = new InputStreamEntity(EmptyInputStream.INSTANCE, null)) {
65              Assertions.assertEquals(-1, entity.getContentLength());
66          }
67      }
68  
69      @Test
70      public void testWriteTo() throws Exception {
71          final String message = "Message content";
72          final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
73          final InputStream inStream = new ByteArrayInputStream(bytes);
74          try (final InputStreamEntity entity = new InputStreamEntity(inStream, bytes.length,
75                  ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1))) {
76              final ByteArrayOutputStream out = new ByteArrayOutputStream();
77              entity.writeTo(out);
78              final byte[] writtenBytes = out.toByteArray();
79              Assertions.assertNotNull(writtenBytes);
80              Assertions.assertEquals(bytes.length, writtenBytes.length);
81  
82              final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
83              Assertions.assertEquals(message, s);
84          }
85      }
86  
87      @Test
88      public void testWriteToPartialContent() throws Exception {
89          final String message = "Message content";
90          final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
91          final InputStream inStream = new ByteArrayInputStream(bytes);
92          final int contentLength = 7;
93          try (final InputStreamEntity entity = new InputStreamEntity(inStream, contentLength,
94                  ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1))) {
95  
96              final ByteArrayOutputStream out = new ByteArrayOutputStream();
97              entity.writeTo(out);
98              final byte[] writtenBytes = out.toByteArray();
99              Assertions.assertNotNull(writtenBytes);
100             Assertions.assertEquals(contentLength, writtenBytes.length);
101 
102             final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
103             Assertions.assertEquals(message.substring(0, contentLength), s);
104         }
105     }
106 
107     @Test
108     public void testWriteToUnknownLength() throws Exception {
109         final String message = "Message content";
110         final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
111         final InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(bytes),
112                 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
113 
114         final ByteArrayOutputStream out = new ByteArrayOutputStream();
115         entity.writeTo(out);
116         final byte[] writtenBytes = out.toByteArray();
117         Assertions.assertNotNull(writtenBytes);
118         Assertions.assertEquals(bytes.length, writtenBytes.length);
119 
120         final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
121         Assertions.assertEquals(message, s);
122     }
123 
124     @Test
125     public void testWriteToNull() throws Exception {
126         try (final InputStreamEntity entity = new InputStreamEntity(EmptyInputStream.INSTANCE, 0, null)) {
127             Assertions.assertThrows(NullPointerException.class, () -> entity.writeTo(null));
128         }}
129 }