1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 class TestEntityBuilder {
41
42 @Test
43 void testBuildEmptyEntity() {
44 Assertions.assertThrows(IllegalStateException.class, () ->
45 EntityBuilder.create().build());
46 }
47
48 @Test
49 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=UTF-8", entity.getContentType());
55 }
56
57 @Test
58 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 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 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 void testBuildFileEntity() {
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 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 void testBuildChunked() {
112 final HttpEntity entity = EntityBuilder.create().setText("stuff").chunked().build();
113 Assertions.assertNotNull(entity);
114 Assertions.assertTrue(entity.isChunked());
115 }
116
117 @Test
118 void testBuildGZipped() {
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=UTF-8", entity.getContentType());
123 Assertions.assertNotNull(entity.getContentEncoding());
124 Assertions.assertEquals("gzip", entity.getContentEncoding());
125 }
126
127 }