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