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.assertNotNull(p2);
63          Assertions.assertEquals("UTF-8", p2.getValue());
64      }
65  
66      @Test
67      public void testImplictContractorParams() throws Exception {
68          final HttpEntity entity = MultipartEntityBuilder.create().build();
69          Assertions.assertNull(entity.getContentEncoding());
70          final String contentType = entity.getContentType();
71          final HeaderElement elem = BasicHeaderValueParser.INSTANCE.parseHeaderElement(contentType,
72                  new ParserCursor(0, contentType.length()));
73          Assertions.assertEquals("multipart/mixed", elem.getName());
74          final NameValuePair p1 = elem.getParameterByName("boundary");
75          Assertions.assertNotNull(p1);
76  
77          final String boundary = p1.getValue();
78          Assertions.assertNotNull(boundary);
79  
80          Assertions.assertTrue(boundary.length() >= 30);
81          Assertions.assertTrue(boundary.length() <= 40);
82  
83          final NameValuePair p2 = elem.getParameterByName("charset");
84          Assertions.assertNull(p2);
85      }
86  
87      @Test
88      public void testRepeatable() throws Exception {
89          final HttpEntity entity = MultipartEntityBuilder.create()
90                  .addTextBody("p1", "blah blah", ContentType.DEFAULT_TEXT)
91                  .addTextBody("p2", "yada yada", ContentType.DEFAULT_TEXT)
92                  .build();
93          Assertions.assertTrue(entity.isRepeatable());
94          Assertions.assertFalse(entity.isChunked());
95          Assertions.assertFalse(entity.isStreaming());
96  
97          final long len = entity.getContentLength();
98          Assertions.assertEquals(len, entity.getContentLength());
99  
100         ByteArrayOutputStream out = new ByteArrayOutputStream();
101         entity.writeTo(out);
102         out.close();
103 
104         byte[] bytes = out.toByteArray();
105         Assertions.assertNotNull(bytes);
106         Assertions.assertEquals(bytes.length, len);
107 
108         Assertions.assertEquals(len, entity.getContentLength());
109 
110         out = new ByteArrayOutputStream();
111         entity.writeTo(out);
112         out.close();
113 
114         bytes = out.toByteArray();
115         Assertions.assertNotNull(bytes);
116         Assertions.assertEquals(bytes.length, len);
117     }
118 
119     @Test
120     public void testNonRepeatable() throws Exception {
121         final HttpEntity entity = MultipartEntityBuilder.create()
122             .addPart("p1", new InputStreamBody(
123                 new ByteArrayInputStream("blah blah".getBytes()), ContentType.DEFAULT_BINARY))
124             .addPart("p2", new InputStreamBody(
125                 new ByteArrayInputStream("yada yada".getBytes()), ContentType.DEFAULT_BINARY))
126             .build();
127         Assertions.assertFalse(entity.isRepeatable());
128         Assertions.assertTrue(entity.isChunked());
129         Assertions.assertTrue(entity.isStreaming());
130 
131         Assertions.assertEquals(-1, entity.getContentLength());
132     }
133 
134 }