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.ByteArrayOutputStream;
31  import java.nio.charset.Charset;
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 StringEntity}.
40   */
41  public class TestStringEntity {
42  
43      @Test
44      public void testBasics() throws Exception {
45          final String s = "Message content";
46          final StringEntity httpentity = new StringEntity(s, ContentType.TEXT_PLAIN);
47  
48          final byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
49          Assertions.assertEquals(bytes.length, httpentity.getContentLength());
50          Assertions.assertNotNull(httpentity.getContent());
51          Assertions.assertTrue(httpentity.isRepeatable());
52          Assertions.assertFalse(httpentity.isStreaming());
53      }
54  
55      @Test
56      public void testNullConstructor() throws Exception {
57          Assertions.assertThrows(NullPointerException.class, () -> new StringEntity(null));
58      }
59  
60      @Test
61      public void testDefaultContent() throws Exception {
62          final String s = "Message content";
63          StringEntity httpentity = new StringEntity(s, ContentType.create("text/csv", "ANSI_X3.4-1968"));
64          Assertions.assertEquals("text/csv; charset=US-ASCII", httpentity.getContentType());
65          httpentity = new StringEntity(s, StandardCharsets.US_ASCII);
66          Assertions.assertEquals("text/plain; charset=US-ASCII", httpentity.getContentType());
67          httpentity = new StringEntity(s);
68          Assertions.assertEquals("text/plain; charset=ISO-8859-1", httpentity.getContentType());
69      }
70  
71      private static String constructString(final int [] unicodeChars) {
72          final StringBuilder buffer = new StringBuilder();
73          if (unicodeChars != null) {
74              for (final int unicodeChar : unicodeChars) {
75                  buffer.append((char)unicodeChar);
76              }
77          }
78          return buffer.toString();
79      }
80  
81      static final int SWISS_GERMAN_HELLO [] = {
82              0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
83          };
84  
85      @Test
86      public void testNullCharset() throws Exception {
87          final String s = constructString(SWISS_GERMAN_HELLO);
88          StringEntity httpentity = new StringEntity(s, ContentType.create("text/plain", (Charset) null));
89          Assertions.assertNotNull(httpentity.getContentType());
90          Assertions.assertEquals("text/plain", httpentity.getContentType());
91          Assertions.assertEquals(s, EntityUtils.toString(httpentity));
92          httpentity = new StringEntity(s, (Charset) null);
93          Assertions.assertNotNull(httpentity.getContentType());
94          Assertions.assertEquals("text/plain", httpentity.getContentType());
95          Assertions.assertEquals(s, EntityUtils.toString(httpentity));
96      }
97  
98      @Test
99      public void testWriteTo() throws Exception {
100         final String s = "Message content";
101         final byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
102         final StringEntity httpentity = new StringEntity(s);
103 
104         ByteArrayOutputStream out = new ByteArrayOutputStream();
105         httpentity.writeTo(out);
106         byte[] bytes2 = out.toByteArray();
107         Assertions.assertNotNull(bytes2);
108         Assertions.assertEquals(bytes.length, bytes2.length);
109         for (int i = 0; i < bytes.length; i++) {
110             Assertions.assertEquals(bytes[i], bytes2[i]);
111         }
112 
113         out = new ByteArrayOutputStream();
114         httpentity.writeTo(out);
115         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         Assertions.assertThrows(NullPointerException.class, () -> httpentity.writeTo(null));
123     }
124 
125 }