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.nio.charset.StandardCharsets;
32
33 import org.apache.hc.core5.http.ContentType;
34 import org.junit.Assert;
35 import org.junit.Test;
36
37 public class TestMultipartContentBody {
38
39 @Test
40 public void testStringBody() throws Exception {
41 final StringBody b1 = new StringBody("text", ContentType.DEFAULT_TEXT);
42 Assert.assertEquals(4, b1.getContentLength());
43
44 Assert.assertEquals("ISO-8859-1", b1.getCharset());
45
46 Assert.assertNull(b1.getFilename());
47 Assert.assertEquals("text/plain", b1.getMimeType());
48 Assert.assertEquals("text", b1.getMediaType());
49 Assert.assertEquals("plain", b1.getSubType());
50
51 final StringBody b2 = new StringBody("more text",
52 ContentType.create("text/other", StandardCharsets.ISO_8859_1));
53 Assert.assertEquals(9, b2.getContentLength());
54 Assert.assertEquals(StandardCharsets.ISO_8859_1.name(), b2.getCharset());
55
56 Assert.assertNull(b2.getFilename());
57 Assert.assertEquals("text/other", b2.getMimeType());
58 Assert.assertEquals("text", b2.getMediaType());
59 Assert.assertEquals("other", b2.getSubType());
60 }
61
62 @Test
63 public void testInputStreamBody() throws Exception {
64 final byte[] stuff = "Stuff".getBytes(StandardCharsets.US_ASCII);
65 final InputStreamBody b1 = new InputStreamBody(new ByteArrayInputStream(stuff), "stuff");
66 Assert.assertEquals(-1, b1.getContentLength());
67
68 Assert.assertNull(b1.getCharset());
69 Assert.assertEquals("stuff", b1.getFilename());
70 Assert.assertEquals("application/octet-stream", b1.getMimeType());
71 Assert.assertEquals("application", b1.getMediaType());
72 Assert.assertEquals("octet-stream", b1.getSubType());
73
74 final InputStreamBody b2 = new InputStreamBody(
75 new ByteArrayInputStream(stuff), ContentType.create("some/stuff"), "stuff");
76 Assert.assertEquals(-1, b2.getContentLength());
77 Assert.assertNull(b2.getCharset());
78 Assert.assertEquals("stuff", b2.getFilename());
79 Assert.assertEquals("some/stuff", b2.getMimeType());
80 Assert.assertEquals("some", b2.getMediaType());
81 Assert.assertEquals("stuff", b2.getSubType());
82 }
83
84 }