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.Assert;
41 import org.junit.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 Assert.assertNull(entity.getContentEncoding());
54 final String contentType = entity.getContentType();
55 final HeaderElement elem = BasicHeaderValueParser.INSTANCE.parseHeaderElement(contentType,
56 new ParserCursor(0, contentType.length()));
57 Assert.assertEquals("multipart/mixed", elem.getName());
58 final NameValuePair p1 = elem.getParameterByName("boundary");
59 Assert.assertNotNull(p1);
60 Assert.assertEquals("whatever", p1.getValue());
61 final NameValuePair p2 = elem.getParameterByName("charset");
62 Assert.assertNotNull(p2);
63 Assert.assertEquals("UTF-8", p2.getValue());
64 }
65
66 @Test
67 public void testImplictContractorParams() throws Exception {
68 final HttpEntity entity = MultipartEntityBuilder.create().build();
69 Assert.assertNull(entity.getContentEncoding());
70 final String contentType = entity.getContentType();
71 final HeaderElement elem = BasicHeaderValueParser.INSTANCE.parseHeaderElement(contentType,
72 new ParserCursor(0, contentType.length()));
73 Assert.assertEquals("multipart/mixed", elem.getName());
74 final NameValuePair p1 = elem.getParameterByName("boundary");
75 Assert.assertNotNull(p1);
76
77 final String boundary = p1.getValue();
78 Assert.assertNotNull(boundary);
79
80 Assert.assertTrue(boundary.length() >= 30);
81 Assert.assertTrue(boundary.length() <= 40);
82
83 final NameValuePair p2 = elem.getParameterByName("charset");
84 Assert.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 Assert.assertTrue(entity.isRepeatable());
94 Assert.assertFalse(entity.isChunked());
95 Assert.assertFalse(entity.isStreaming());
96
97 final long len = entity.getContentLength();
98 Assert.assertTrue(len == entity.getContentLength());
99
100 ByteArrayOutputStream out = new ByteArrayOutputStream();
101 entity.writeTo(out);
102 out.close();
103
104 byte[] bytes = out.toByteArray();
105 Assert.assertNotNull(bytes);
106 Assert.assertTrue(bytes.length == len);
107
108 Assert.assertTrue(len == entity.getContentLength());
109
110 out = new ByteArrayOutputStream();
111 entity.writeTo(out);
112 out.close();
113
114 bytes = out.toByteArray();
115 Assert.assertNotNull(bytes);
116 Assert.assertTrue(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 Assert.assertFalse(entity.isRepeatable());
128 Assert.assertTrue(entity.isChunked());
129 Assert.assertTrue(entity.isStreaming());
130
131 Assert.assertTrue(entity.getContentLength() == -1);
132 }
133
134 }