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.io.ByteArrayOutputStream;
32  import java.nio.charset.StandardCharsets;
33  
34  import org.apache.hc.core5.http.ContentType;
35  import org.apache.hc.core5.http.HeaderElement;
36  import org.apache.hc.core5.http.HttpEntity;
37  import org.apache.hc.core5.http.NameValuePair;
38  import org.apache.hc.core5.http.message.BasicHeaderValueParser;
39  import org.apache.hc.core5.http.message.ParserCursor;
40  import org.junit.jupiter.api.Assertions;
41  import org.junit.jupiter.api.Test;
42  
43  public class TestMultipartFormHttpEntity {
44  
45      @Test
46      public void testExplictContractorParams() throws Exception {
47          final HttpEntity entity = MultipartEntityBuilder.create()
48                  .setLaxMode()
49                  .setBoundary("whatever")
50                  .setCharset(StandardCharsets.UTF_8)
51                  .build();
52  
53          Assertions.assertNull(entity.getContentEncoding());
54          final String contentType = entity.getContentType();
55          final HeaderElement elem = BasicHeaderValueParser.INSTANCE.parseHeaderElement(contentType,
56                  new ParserCursor(0, contentType.length()));
57          Assertions.assertEquals("multipart/mixed", elem.getName());
58          final NameValuePair p1 = elem.getParameterByName("boundary");
59          Assertions.assertNotNull(p1);
60          Assertions.assertEquals("whatever", p1.getValue());
61          final NameValuePair p2 = elem.getParameterByName("charset");
62          Assertions.assertNull(p2,
63                  "RFC7578 does not mention charset parameter for Content-Type, " +
64                          "so no charset should be present for MultipartEntity.getContentType()");
65      }
66  
67      @Test
68      public void testImplictContractorParams() throws Exception {
69          final HttpEntity entity = MultipartEntityBuilder.create().build();
70          Assertions.assertNull(entity.getContentEncoding());
71          final String contentType = entity.getContentType();
72          final HeaderElement elem = BasicHeaderValueParser.INSTANCE.parseHeaderElement(contentType,
73                  new ParserCursor(0, contentType.length()));
74          Assertions.assertEquals("multipart/mixed", elem.getName());
75          final NameValuePair p1 = elem.getParameterByName("boundary");
76          Assertions.assertNotNull(p1);
77  
78          final String boundary = p1.getValue();
79          Assertions.assertNotNull(boundary);
80  
81          Assertions.assertTrue(boundary.length() >= 30);
82          Assertions.assertTrue(boundary.length() <= 40);
83  
84          final NameValuePair p2 = elem.getParameterByName("charset");
85          Assertions.assertNull(p2);
86      }
87  
88      @Test
89      public void testRepeatable() throws Exception {
90          final HttpEntity entity = MultipartEntityBuilder.create()
91                  .addTextBody("p1", "blah blah", ContentType.DEFAULT_TEXT)
92                  .addTextBody("p2", "yada yada", ContentType.DEFAULT_TEXT)
93                  .build();
94          Assertions.assertTrue(entity.isRepeatable());
95          Assertions.assertFalse(entity.isChunked());
96          Assertions.assertFalse(entity.isStreaming());
97  
98          final long len = entity.getContentLength();
99          Assertions.assertEquals(len, entity.getContentLength());
100 
101         ByteArrayOutputStream out = new ByteArrayOutputStream();
102         entity.writeTo(out);
103         out.close();
104 
105         byte[] bytes = out.toByteArray();
106         Assertions.assertNotNull(bytes);
107         Assertions.assertEquals(bytes.length, len);
108 
109         Assertions.assertEquals(len, entity.getContentLength());
110 
111         out = new ByteArrayOutputStream();
112         entity.writeTo(out);
113         out.close();
114 
115         bytes = out.toByteArray();
116         Assertions.assertNotNull(bytes);
117         Assertions.assertEquals(bytes.length, len);
118     }
119 
120     @Test
121     public void testNonRepeatable() throws Exception {
122         final HttpEntity entity = MultipartEntityBuilder.create()
123             .addPart("p1", new InputStreamBody(
124                 new ByteArrayInputStream("blah blah".getBytes()), ContentType.DEFAULT_BINARY))
125             .addPart("p2", new InputStreamBody(
126                 new ByteArrayInputStream("yada yada".getBytes()), ContentType.DEFAULT_BINARY))
127             .build();
128         Assertions.assertFalse(entity.isRepeatable());
129         Assertions.assertTrue(entity.isChunked());
130         Assertions.assertTrue(entity.isStreaming());
131 
132         Assertions.assertEquals(-1, entity.getContentLength());
133     }
134 
135 }