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.http.entity.mime;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.File;
32 import java.util.List;
33
34 import org.apache.http.Consts;
35 import org.apache.http.Header;
36 import org.apache.http.entity.ContentType;
37 import org.apache.http.message.BasicNameValuePair;
38 import org.junit.Assert;
39 import org.junit.Test;
40
41 public class TestMultipartEntityBuilder {
42
43 @Test
44 public void testBasics() throws Exception {
45 final MultipartFormEntity entity = MultipartEntityBuilder.create().buildEntity();
46 Assert.assertNotNull(entity);
47 Assert.assertTrue(entity.getMultipart() instanceof HttpStrictMultipart);
48 Assert.assertEquals(0, entity.getMultipart().getBodyParts().size());
49 }
50
51 @Test
52 public void testMultipartOptions() throws Exception {
53 final MultipartFormEntity entity = MultipartEntityBuilder.create()
54 .setBoundary("blah-blah")
55 .setCharset(Consts.UTF_8)
56 .setLaxMode()
57 .buildEntity();
58 Assert.assertNotNull(entity);
59 Assert.assertTrue(entity.getMultipart() instanceof HttpBrowserCompatibleMultipart);
60 Assert.assertEquals("blah-blah", entity.getMultipart().boundary);
61 Assert.assertEquals(Consts.UTF_8, entity.getMultipart().charset);
62 }
63
64 @Test
65 public void testAddBodyParts() throws Exception {
66 final MultipartFormEntity entity = MultipartEntityBuilder.create()
67 .addTextBody("p1", "stuff")
68 .addBinaryBody("p2", new File("stuff"))
69 .addBinaryBody("p3", new byte[]{})
70 .addBinaryBody("p4", new ByteArrayInputStream(new byte[]{}))
71 .buildEntity();
72 Assert.assertNotNull(entity);
73 final List<FormBodyPart> bodyParts = entity.getMultipart().getBodyParts();
74 Assert.assertNotNull(bodyParts);
75 Assert.assertEquals(4, bodyParts.size());
76 }
77
78 @Test
79 public void testMultipartCustomContentType() throws Exception {
80 final MultipartFormEntity entity = MultipartEntityBuilder.create()
81 .setContentType(ContentType.APPLICATION_XML)
82 .setBoundary("blah-blah")
83 .setCharset(Consts.UTF_8)
84 .setLaxMode()
85 .buildEntity();
86 Assert.assertNotNull(entity);
87 final Header contentType = entity.getContentType();
88 Assert.assertNotNull(contentType);
89 Assert.assertEquals("application/xml; boundary=blah-blah; charset=UTF-8", contentType.getValue());
90 }
91
92 @Test
93 public void testMultipartContentTypeParameter() throws Exception {
94 final MultipartFormEntity entity = MultipartEntityBuilder.create()
95 .setContentType(ContentType.MULTIPART_FORM_DATA.withParameters(
96 new BasicNameValuePair("boundary", "yada-yada"),
97 new BasicNameValuePair("charset", "ascii")))
98 .buildEntity();
99 Assert.assertNotNull(entity);
100 final Header contentType = entity.getContentType();
101 Assert.assertNotNull(contentType);
102 Assert.assertEquals("multipart/form-data; boundary=yada-yada; charset=US-ASCII",
103 contentType.getValue());
104 Assert.assertEquals("yada-yada", entity.getMultipart().boundary);
105 Assert.assertEquals(Consts.ASCII, entity.getMultipart().charset);
106 }
107
108 @Test
109 public void testMultipartCustomContentTypeParameterOverrides() throws Exception {
110 final MultipartFormEntity entity = MultipartEntityBuilder.create()
111 .setContentType(ContentType.MULTIPART_FORM_DATA.withParameters(
112 new BasicNameValuePair("boundary", "yada-yada"),
113 new BasicNameValuePair("charset", "ascii"),
114 new BasicNameValuePair("my", "stuff")))
115 .setBoundary("blah-blah")
116 .setCharset(Consts.UTF_8)
117 .setLaxMode()
118 .buildEntity();
119 Assert.assertNotNull(entity);
120 final Header contentType = entity.getContentType();
121 Assert.assertNotNull(contentType);
122 Assert.assertEquals("multipart/form-data; boundary=blah-blah; charset=UTF-8; my=stuff",
123 contentType.getValue());
124 }
125
126 }