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.entity.mime.content.InputStreamBody;
33  import org.apache.http.entity.mime.content.StringBody;
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          StringBody b1 = new StringBody("text");
42          Assert.assertEquals(4, b1.getContentLength());
43  
44          Assert.assertEquals("US-ASCII", 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          Assert.assertEquals(MIME.ENC_8BIT, b1.getTransferEncoding());
52  
53          StringBody b2 = new StringBody("more text", "text/other", MIME.DEFAULT_CHARSET);
54          Assert.assertEquals(9, b2.getContentLength());
55          Assert.assertEquals(MIME.DEFAULT_CHARSET.name(), b2.getCharset());
56  
57          Assert.assertNull(b2.getFilename());
58          Assert.assertEquals("text/other", b2.getMimeType());
59          Assert.assertEquals("text", b2.getMediaType());
60          Assert.assertEquals("other", b2.getSubType());
61  
62          Assert.assertEquals(MIME.ENC_8BIT, b2.getTransferEncoding());
63      }
64  
65      @Test
66      public void testInputStreamBody() throws Exception {
67          byte[] stuff = "Stuff".getBytes("US-ASCII");
68          InputStreamBody b1 = new InputStreamBody(new ByteArrayInputStream(stuff), "stuff");
69          Assert.assertEquals(-1, b1.getContentLength());
70  
71          Assert.assertNull(b1.getCharset());
72          Assert.assertEquals("stuff", b1.getFilename());
73          Assert.assertEquals("application/octet-stream", b1.getMimeType());
74          Assert.assertEquals("application", b1.getMediaType());
75          Assert.assertEquals("octet-stream", b1.getSubType());
76  
77          Assert.assertEquals(MIME.ENC_BINARY, b1.getTransferEncoding());
78  
79          InputStreamBody b2 = new InputStreamBody(
80                  new ByteArrayInputStream(stuff), "some/stuff", "stuff");
81          Assert.assertEquals(-1, b2.getContentLength());
82          Assert.assertNull(b2.getCharset());
83          Assert.assertEquals("stuff", b2.getFilename());
84          Assert.assertEquals("some/stuff", b2.getMimeType());
85          Assert.assertEquals("some", b2.getMediaType());
86          Assert.assertEquals("stuff", b2.getSubType());
87  
88          Assert.assertEquals(MIME.ENC_BINARY, b2.getTransferEncoding());
89      }
90  
91  }