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.junit.Assert;
36  import org.junit.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  
47          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
48          final InputStream content = new ByteArrayInputStream(bytes);
49          final BasicHttpEntity httpentity = new BasicHttpEntity();
50          httpentity.setContent(content);
51          httpentity.setContentLength(bytes.length);
52  
53          Assert.assertEquals(bytes.length, httpentity.getContentLength());
54          Assert.assertFalse(httpentity.isRepeatable());
55          Assert.assertTrue(httpentity.isStreaming());
56      }
57  
58      @Test
59      public void testToString() throws Exception {
60          final BasicHttpEntity httpentity = new BasicHttpEntity();
61          httpentity.setContentType("blah");
62          httpentity.setContentEncoding("yada");
63          httpentity.setContentLength(10);
64          httpentity.setChunked(true);
65          Assert.assertEquals("[Content-Type: blah,Content-Encoding: yada,Content-Length: 10,Chunked: true]",
66                  httpentity.toString());
67      }
68  
69      @Test
70      public void testContent() throws Exception {
71          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
72          final InputStream content = new ByteArrayInputStream(bytes);
73          final BasicHttpEntity httpentity = new BasicHttpEntity();
74          try {
75              httpentity.getContent();
76              Assert.fail("IllegalStateException should have been thrown");
77          } catch (final IllegalStateException ex) {
78              // expected
79          }
80          httpentity.setContent(content);
81          Assert.assertEquals(content, httpentity.getContent());
82  
83          httpentity.setContent(null);
84          try {
85              httpentity.getContent();
86              Assert.fail("IllegalStateException should have been thrown");
87          } catch (final IllegalStateException ex) {
88              // expected
89          }
90      }
91  
92      @Test
93      public void testWriteTo() throws Exception {
94          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
95          final InputStream content = new ByteArrayInputStream(bytes);
96          final BasicHttpEntity httpentity = new BasicHttpEntity();
97          httpentity.setContent(content);
98  
99          ByteArrayOutputStream out = new ByteArrayOutputStream();
100         httpentity.writeTo(out);
101         final byte[] bytes2 = out.toByteArray();
102         Assert.assertNotNull(bytes2);
103         Assert.assertEquals(bytes.length, bytes2.length);
104         for (int i = 0; i < bytes.length; i++) {
105             Assert.assertEquals(bytes[i], bytes2[i]);
106         }
107         httpentity.setContent(null);
108         out = new ByteArrayOutputStream();
109         try {
110             httpentity.writeTo(out);
111             Assert.fail("IllegalStateException should have been thrown");
112         } catch (final IllegalStateException ex) {
113             // expected
114         }
115 
116         try {
117             httpentity.writeTo(null);
118             Assert.fail("IllegalArgumentException should have been thrown");
119         } catch (final IllegalArgumentException ex) {
120             // expected
121         }
122     }
123 
124 }