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.client5.http.entity.mime;
29  
30  import java.io.ByteArrayInputStream;
31  import java.nio.charset.StandardCharsets;
32  
33  import org.apache.hc.core5.http.ContentType;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.Test;
36  
37  public class TestMultipartContentBody {
38  
39      @Test
40      public void testStringBody() throws Exception {
41          final StringBody b1 = new StringBody("text", ContentType.DEFAULT_TEXT);
42          Assertions.assertEquals(4, b1.getContentLength());
43  
44          Assertions.assertEquals("ISO-8859-1", b1.getCharset());
45  
46          Assertions.assertNull(b1.getFilename());
47          Assertions.assertEquals("text/plain", b1.getMimeType());
48          Assertions.assertEquals("text", b1.getMediaType());
49          Assertions.assertEquals("plain", b1.getSubType());
50  
51          final StringBody b2 = new StringBody("more text",
52                  ContentType.create("text/other", StandardCharsets.ISO_8859_1));
53          Assertions.assertEquals(9, b2.getContentLength());
54          Assertions.assertEquals(StandardCharsets.ISO_8859_1.name(), b2.getCharset());
55  
56          Assertions.assertNull(b2.getFilename());
57          Assertions.assertEquals("text/other", b2.getMimeType());
58          Assertions.assertEquals("text", b2.getMediaType());
59          Assertions.assertEquals("other", b2.getSubType());
60      }
61  
62      @Test
63      public void testInputStreamBody() throws Exception {
64          final byte[] stuff = "Stuff".getBytes(StandardCharsets.US_ASCII);
65          final InputStreamBody b1 = new InputStreamBody(new ByteArrayInputStream(stuff), "stuff");
66          Assertions.assertEquals(-1, b1.getContentLength());
67  
68          Assertions.assertNull(b1.getCharset());
69          Assertions.assertEquals("stuff", b1.getFilename());
70          Assertions.assertEquals("application/octet-stream", b1.getMimeType());
71          Assertions.assertEquals("application", b1.getMediaType());
72          Assertions.assertEquals("octet-stream", b1.getSubType());
73  
74          final InputStreamBody b2 = new InputStreamBody(
75                  new ByteArrayInputStream(stuff), ContentType.create("some/stuff"), "stuff");
76          Assertions.assertEquals(-1, b2.getContentLength());
77          Assertions.assertNull(b2.getCharset());
78          Assertions.assertEquals("stuff", b2.getFilename());
79          Assertions.assertEquals("some/stuff", b2.getMimeType());
80          Assertions.assertEquals("some", b2.getMediaType());
81          Assertions.assertEquals("stuff", b2.getSubType());
82      }
83  
84  }