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.client.entity;
29  
30  import java.io.File;
31  import java.io.InputStream;
32  
33  import org.apache.http.HttpEntity;
34  import org.apache.http.entity.ContentType;
35  import org.apache.http.util.EntityUtils;
36  import org.junit.Assert;
37  import org.junit.Test;
38  import org.mockito.Mockito;
39  
40  public class TestEntityBuilder {
41  
42      @Test(expected=IllegalStateException.class)
43      public void testBuildEmptyEntity() throws Exception {
44          final HttpEntity entity = EntityBuilder.create().build();
45          Assert.assertNotNull(entity);
46          entity.getContent();
47      }
48  
49      @Test
50      public void testBuildTextEntity() throws Exception {
51          final HttpEntity entity = EntityBuilder.create().setText("stuff").build();
52          Assert.assertNotNull(entity);
53          Assert.assertNotNull(entity.getContent());
54          Assert.assertNotNull(entity.getContentType());
55          Assert.assertEquals("text/plain; charset=ISO-8859-1", entity.getContentType().getValue());
56      }
57  
58      @Test
59      public void testBuildBinaryEntity() throws Exception {
60          final HttpEntity entity = EntityBuilder.create().setBinary(new byte[] {0, 1, 2}).build();
61          Assert.assertNotNull(entity);
62          Assert.assertNotNull(entity.getContent());
63          Assert.assertNotNull(entity.getContentType());
64          Assert.assertEquals("application/octet-stream", entity.getContentType().getValue());
65      }
66  
67      @Test
68      public void testBuildStreamEntity() throws Exception {
69          final InputStream in = Mockito.mock(InputStream.class);
70          final HttpEntity entity = EntityBuilder.create().setStream(in).build();
71          Assert.assertNotNull(entity);
72          Assert.assertNotNull(entity.getContent());
73          Assert.assertNotNull(entity.getContentType());
74          Assert.assertEquals("application/octet-stream", entity.getContentType().getValue());
75      }
76  
77      @Test
78      public void testBuildSerializableEntity() throws Exception {
79          final HttpEntity entity = EntityBuilder.create().setSerializable(Boolean.TRUE).build();
80          Assert.assertNotNull(entity);
81          Assert.assertNotNull(entity.getContent());
82          Assert.assertNotNull(entity.getContentType());
83          Assert.assertEquals("application/octet-stream", entity.getContentType().getValue());
84      }
85  
86      @Test
87      public void testBuildFileEntity() throws Exception {
88          final File file = new File("stuff");
89          final HttpEntity entity = EntityBuilder.create().setFile(file).build();
90          Assert.assertNotNull(entity);
91          Assert.assertNotNull(entity.getContentType());
92          Assert.assertEquals("application/octet-stream", entity.getContentType().getValue());
93      }
94  
95      @Test
96      public void testExplicitContentProperties() throws Exception {
97          final HttpEntity entity = EntityBuilder.create()
98              .setContentType(ContentType.APPLICATION_JSON)
99              .setContentEncoding("identity")
100             .setBinary(new byte[] {0, 1, 2})
101             .setText("{\"stuff\"}").build();
102         Assert.assertNotNull(entity);
103         Assert.assertNotNull(entity.getContentType());
104         Assert.assertEquals("application/json; charset=UTF-8", entity.getContentType().getValue());
105         Assert.assertNotNull(entity.getContentEncoding());
106         Assert.assertEquals("identity", entity.getContentEncoding().getValue());
107         Assert.assertEquals("{\"stuff\"}", EntityUtils.toString(entity));
108     }
109 
110     @Test
111     public void testBuildChunked() throws Exception {
112         final HttpEntity entity = EntityBuilder.create().setText("stuff").chunked().build();
113         Assert.assertNotNull(entity);
114         Assert.assertTrue(entity.isChunked());
115     }
116 
117     @Test
118     public void testBuildGZipped() throws Exception {
119         final HttpEntity entity = EntityBuilder.create().setText("stuff").gzipCompress().build();
120         Assert.assertNotNull(entity);
121         Assert.assertNotNull(entity.getContentType());
122         Assert.assertEquals("text/plain; charset=ISO-8859-1", entity.getContentType().getValue());
123         Assert.assertNotNull(entity.getContentEncoding());
124         Assert.assertEquals("gzip", entity.getContentEncoding().getValue());
125     }
126 
127 }