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.jupiter.api.Assertions;
37  import org.junit.jupiter.api.Test;
38  import org.mockito.Mockito;
39  
40  public class TestEntityBuilder {
41  
42      @Test
43      public void testBuildEmptyEntity() throws Exception {
44          Assertions.assertThrows(IllegalStateException.class, () ->
45                  EntityBuilder.create().build());
46      }
47  
48      @Test
49      public void testBuildTextEntity() throws Exception {
50          final HttpEntity entity = EntityBuilder.create().setText("stuff").build();
51          Assertions.assertNotNull(entity);
52          Assertions.assertNotNull(entity.getContent());
53          Assertions.assertNotNull(entity.getContentType());
54          Assertions.assertEquals("text/plain; charset=ISO-8859-1", entity.getContentType());
55      }
56  
57      @Test
58      public void testBuildBinaryEntity() throws Exception {
59          final HttpEntity entity = EntityBuilder.create().setBinary(new byte[] {0, 1, 2}).build();
60          Assertions.assertNotNull(entity);
61          Assertions.assertNotNull(entity.getContent());
62          Assertions.assertNotNull(entity.getContentType());
63          Assertions.assertEquals("application/octet-stream", entity.getContentType());
64      }
65  
66      @Test
67      public void testBuildStreamEntity() throws Exception {
68          final InputStream in = Mockito.mock(InputStream.class);
69          final HttpEntity entity = EntityBuilder.create().setStream(in).build();
70          Assertions.assertNotNull(entity);
71          Assertions.assertNotNull(entity.getContent());
72          Assertions.assertNotNull(entity.getContentType());
73          Assertions.assertEquals(-1, entity.getContentLength());
74          Assertions.assertEquals("application/octet-stream", entity.getContentType());
75      }
76  
77      @Test
78      public void testBuildSerializableEntity() throws Exception {
79          final HttpEntity entity = EntityBuilder.create().setSerializable(Boolean.TRUE).build();
80          Assertions.assertNotNull(entity);
81          Assertions.assertNotNull(entity.getContent());
82          Assertions.assertNotNull(entity.getContentType());
83          Assertions.assertEquals("application/octet-stream", entity.getContentType());
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          Assertions.assertNotNull(entity);
91          Assertions.assertNotNull(entity.getContentType());
92          Assertions.assertEquals("application/octet-stream", entity.getContentType());
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         Assertions.assertNotNull(entity);
103         Assertions.assertNotNull(entity.getContentType());
104         Assertions.assertEquals("application/json; charset=UTF-8", entity.getContentType());
105         Assertions.assertNotNull(entity.getContentEncoding());
106         Assertions.assertEquals("identity", entity.getContentEncoding());
107         Assertions.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         Assertions.assertNotNull(entity);
114         Assertions.assertTrue(entity.isChunked());
115     }
116 
117     @Test
118     public void testBuildGZipped() throws Exception {
119         final HttpEntity entity = EntityBuilder.create().setText("stuff").gzipCompressed().build();
120         Assertions.assertNotNull(entity);
121         Assertions.assertNotNull(entity.getContentType());
122         Assertions.assertEquals("text/plain; charset=ISO-8859-1", entity.getContentType());
123         Assertions.assertNotNull(entity.getContentEncoding());
124         Assertions.assertEquals("gzip", entity.getContentEncoding());
125     }
126 
127 }