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.hc.client5.http.entity;
29  
30  import java.io.File;
31  import java.io.InputStream;
32  
33  import org.apache.hc.core5.http.ContentType;
34  import org.apache.hc.core5.http.HttpEntity;
35  import org.apache.hc.core5.http.io.entity.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());
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());
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(-1, entity.getContentLength());
75          Assert.assertEquals("application/octet-stream", entity.getContentType());
76      }
77  
78      @Test
79      public void testBuildSerializableEntity() throws Exception {
80          final HttpEntity entity = EntityBuilder.create().setSerializable(Boolean.TRUE).build();
81          Assert.assertNotNull(entity);
82          Assert.assertNotNull(entity.getContent());
83          Assert.assertNotNull(entity.getContentType());
84          Assert.assertEquals("application/octet-stream", entity.getContentType());
85      }
86  
87      @Test
88      public void testBuildFileEntity() throws Exception {
89          final File file = new File("stuff");
90          final HttpEntity entity = EntityBuilder.create().setFile(file).build();
91          Assert.assertNotNull(entity);
92          Assert.assertNotNull(entity.getContentType());
93          Assert.assertEquals("application/octet-stream", entity.getContentType());
94      }
95  
96      @Test
97      public void testExplicitContentProperties() throws Exception {
98          final HttpEntity entity = EntityBuilder.create()
99              .setContentType(ContentType.APPLICATION_JSON)
100             .setContentEncoding("identity")
101             .setBinary(new byte[] {0, 1, 2})
102             .setText("{\"stuff\"}").build();
103         Assert.assertNotNull(entity);
104         Assert.assertNotNull(entity.getContentType());
105         Assert.assertEquals("application/json; charset=UTF-8", entity.getContentType());
106         Assert.assertNotNull(entity.getContentEncoding());
107         Assert.assertEquals("identity", entity.getContentEncoding());
108         Assert.assertEquals("{\"stuff\"}", EntityUtils.toString(entity));
109     }
110 
111     @Test
112     public void testBuildChunked() throws Exception {
113         final HttpEntity entity = EntityBuilder.create().setText("stuff").chunked().build();
114         Assert.assertNotNull(entity);
115         Assert.assertTrue(entity.isChunked());
116     }
117 
118     @Test
119     public void testBuildGZipped() throws Exception {
120         final HttpEntity entity = EntityBuilder.create().setText("stuff").gzipCompressed().build();
121         Assert.assertNotNull(entity);
122         Assert.assertNotNull(entity.getContentType());
123         Assert.assertEquals("text/plain; charset=ISO-8859-1", entity.getContentType());
124         Assert.assertNotNull(entity.getContentEncoding());
125         Assert.assertEquals("gzip", entity.getContentEncoding());
126     }
127 
128 }