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