View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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)); // avoid unused warning
71      }
72  
73      @Test(expected=IllegalArgumentException.class)
74      public void testStringBodyInvalidConstruction2() throws Exception {
75          Assert.assertNotNull(new StringBody("stuff", (ContentType) null)); // avoid unused warning
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 }