1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 class TestMultipartFormHttpEntity {
44
45 @Test
46 void testExplicitContractorParams() {
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 void testImplicitContractorParams() {
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
83 final NameValuePair p2 = elem.getParameterByName("charset");
84 Assertions.assertNull(p2);
85 }
86
87 @Test
88 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 void testNonRepeatable() {
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 }