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.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(-1, entity.getContentLength());
75 Assert.assertEquals("application/octet-stream", entity.getContentType().getValue());
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().getValue());
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().getValue());
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().getValue());
106 Assert.assertNotNull(entity.getContentEncoding());
107 Assert.assertEquals("identity", entity.getContentEncoding().getValue());
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").gzipCompress().build();
121 Assert.assertNotNull(entity);
122 Assert.assertNotNull(entity.getContentType());
123 Assert.assertEquals("text/plain; charset=ISO-8859-1", entity.getContentType().getValue());
124 Assert.assertNotNull(entity.getContentEncoding());
125 Assert.assertEquals("gzip", entity.getContentEncoding().getValue());
126 }
127
128 }