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
32 import org.apache.http.Consts;
33 import org.apache.http.entity.ContentType;
34 import org.apache.http.entity.mime.content.InputStreamBody;
35 import org.apache.http.entity.mime.content.StringBody;
36 import org.junit.Assert;
37 import org.junit.Test;
38
39 public class TestMultipartContentBody {
40
41 @Test
42 public void testStringBody() throws Exception {
43 final StringBody b1 = new StringBody("text", ContentType.DEFAULT_TEXT);
44 Assert.assertEquals(4, b1.getContentLength());
45
46 Assert.assertEquals("ISO-8859-1", b1.getCharset());
47
48 Assert.assertNull(b1.getFilename());
49 Assert.assertEquals("text/plain", b1.getMimeType());
50 Assert.assertEquals("text", b1.getMediaType());
51 Assert.assertEquals("plain", b1.getSubType());
52
53 Assert.assertEquals(MIME.ENC_8BIT, b1.getTransferEncoding());
54
55 final StringBody b2 = new StringBody("more text",
56 ContentType.create("text/other", MIME.DEFAULT_CHARSET));
57 Assert.assertEquals(9, b2.getContentLength());
58 Assert.assertEquals(MIME.DEFAULT_CHARSET.name(), b2.getCharset());
59
60 Assert.assertNull(b2.getFilename());
61 Assert.assertEquals("text/other", b2.getMimeType());
62 Assert.assertEquals("text", b2.getMediaType());
63 Assert.assertEquals("other", b2.getSubType());
64
65 Assert.assertEquals(MIME.ENC_8BIT, b2.getTransferEncoding());
66 }
67
68 @Test(expected=IllegalArgumentException.class)
69 public void testStringBodyInvalidConstruction1() throws Exception {
70 Assert.assertNotNull(new StringBody(null, ContentType.DEFAULT_TEXT));
71 }
72
73 @Test(expected=IllegalArgumentException.class)
74 public void testStringBodyInvalidConstruction2() throws Exception {
75 Assert.assertNotNull(new StringBody("stuff", (ContentType) null));
76 }
77
78 @Test
79 public void testInputStreamBody() throws Exception {
80 final byte[] stuff = "Stuff".getBytes(Consts.ASCII);
81 final InputStreamBody b1 = new InputStreamBody(new ByteArrayInputStream(stuff), "stuff");
82 Assert.assertEquals(-1, b1.getContentLength());
83
84 Assert.assertNull(b1.getCharset());
85 Assert.assertEquals("stuff", b1.getFilename());
86 Assert.assertEquals("application/octet-stream", b1.getMimeType());
87 Assert.assertEquals("application", b1.getMediaType());
88 Assert.assertEquals("octet-stream", b1.getSubType());
89
90 Assert.assertEquals(MIME.ENC_BINARY, b1.getTransferEncoding());
91
92 final InputStreamBody b2 = new InputStreamBody(
93 new ByteArrayInputStream(stuff), ContentType.create("some/stuff"), "stuff");
94 Assert.assertEquals(-1, b2.getContentLength());
95 Assert.assertNull(b2.getCharset());
96 Assert.assertEquals("stuff", b2.getFilename());
97 Assert.assertEquals("some/stuff", b2.getMimeType());
98 Assert.assertEquals("some", b2.getMediaType());
99 Assert.assertEquals("stuff", b2.getSubType());
100
101 Assert.assertEquals(MIME.ENC_BINARY, b2.getTransferEncoding());
102 }
103
104 }