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.Assert;
37  import org.junit.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          Assert.assertEquals(bytes.length, entity.getContentLength());
51          Assert.assertNotNull(entity.getContent());
52          Assert.assertFalse(entity.isRepeatable());
53          Assert.assertTrue(entity.isStreaming());
54      }
55  
56      @Test(expected = NullPointerException.class)
57      public void testNullConstructor() throws Exception {
58          new InputStreamEntity(null, 0, null);
59      }
60  
61      @Test
62      public void testUnknownLengthConstructor() throws Exception {
63          final InputStreamEntity entity = new InputStreamEntity(EmptyInputStream.INSTANCE, null);
64          Assert.assertEquals(-1, entity.getContentLength());
65      }
66  
67      @Test
68      public void testWriteTo() throws Exception {
69          final String message = "Message content";
70          final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
71          final InputStream inStream = new ByteArrayInputStream(bytes);
72          final InputStreamEntity entity = new InputStreamEntity(inStream, bytes.length,
73                  ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
74  
75          final ByteArrayOutputStream out = new ByteArrayOutputStream();
76          entity.writeTo(out);
77          final byte[] writtenBytes = out.toByteArray();
78          Assert.assertNotNull(writtenBytes);
79          Assert.assertEquals(bytes.length, writtenBytes.length);
80  
81          final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
82          Assert.assertEquals(message, s);
83      }
84  
85      @Test
86      public void testWriteToPartialContent() throws Exception {
87          final String message = "Message content";
88          final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
89          final InputStream inStream = new ByteArrayInputStream(bytes);
90          final int contentLength = 7;
91          final InputStreamEntity entity = new InputStreamEntity(inStream, contentLength,
92                  ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
93  
94          final ByteArrayOutputStream out = new ByteArrayOutputStream();
95          entity.writeTo(out);
96          final byte[] writtenBytes = out.toByteArray();
97          Assert.assertNotNull(writtenBytes);
98          Assert.assertEquals(contentLength, writtenBytes.length);
99  
100         final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
101         Assert.assertEquals(message.substring(0, contentLength), s);
102     }
103 
104     @Test
105     public void testWriteToUnknownLength() throws Exception {
106         final String message = "Message content";
107         final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1);
108         final InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(bytes),
109                 ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1));
110 
111         final ByteArrayOutputStream out = new ByteArrayOutputStream();
112         entity.writeTo(out);
113         final byte[] writtenBytes = out.toByteArray();
114         Assert.assertNotNull(writtenBytes);
115         Assert.assertEquals(bytes.length, writtenBytes.length);
116 
117         final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name());
118         Assert.assertEquals(message, s);
119     }
120 
121     @Test(expected = NullPointerException.class)
122     public void testWriteToNull() throws Exception {
123         final InputStreamEntity entity = new InputStreamEntity(EmptyInputStream.INSTANCE, 0, null);
124         entity.writeTo(null);
125     }
126 }