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 Assertions.assertTrue(boundary.length() <= 40);
83
84 final NameValuePair p2 = elem.getParameterByName("charset");
85 Assertions.assertNull(p2);
86 }
87
88 @Test
89 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 void testNonRepeatable() {
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 }