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.entity;
29
30 import java.io.ByteArrayOutputStream;
31
32 import org.apache.http.Consts;
33 import org.apache.http.protocol.HTTP;
34 import org.apache.http.util.EntityUtils;
35 import org.junit.Assert;
36 import org.junit.Test;
37
38
39
40
41
42 public class TestHttpEntityWrapper {
43
44 @Test
45 public void testBasics() throws Exception {
46 final String s = "Message content";
47 final StringEntity httpentity = new StringEntity(s, ContentType.TEXT_PLAIN);
48 httpentity.setContentEncoding(HTTP.IDENTITY_CODING);
49 final HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
50
51 Assert.assertEquals(httpentity.getContentLength(), wrapped.getContentLength());
52 Assert.assertEquals(httpentity.getContentType(), wrapped.getContentType());
53 Assert.assertEquals(httpentity.getContentEncoding(), wrapped.getContentEncoding());
54 Assert.assertEquals(httpentity.isChunked(), wrapped.isChunked());
55 Assert.assertEquals(httpentity.isRepeatable(), wrapped.isRepeatable());
56 Assert.assertEquals(httpentity.isStreaming(), wrapped.isStreaming());
57 Assert.assertNotNull(wrapped.getContent());
58 }
59
60 @Test
61 public void testIllegalConstructor() throws Exception {
62 try {
63 new HttpEntityWrapper(null);
64 Assert.fail("IllegalArgumentException should have been thrown");
65 } catch (final IllegalArgumentException ex) {
66
67 }
68 }
69
70 @Test
71 public void testWriteTo() throws Exception {
72 final String s = "Message content";
73 final byte[] bytes = s.getBytes(Consts.ISO_8859_1);
74 final StringEntity httpentity = new StringEntity(s);
75 final HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
76
77 ByteArrayOutputStream out = new ByteArrayOutputStream();
78 wrapped.writeTo(out);
79 byte[] bytes2 = out.toByteArray();
80 Assert.assertNotNull(bytes2);
81 Assert.assertEquals(bytes.length, bytes2.length);
82 for (int i = 0; i < bytes.length; i++) {
83 Assert.assertEquals(bytes[i], bytes2[i]);
84 }
85
86 out = new ByteArrayOutputStream();
87 wrapped.writeTo(out);
88 bytes2 = out.toByteArray();
89 Assert.assertNotNull(bytes2);
90 Assert.assertEquals(bytes.length, bytes2.length);
91 for (int i = 0; i < bytes.length; i++) {
92 Assert.assertEquals(bytes[i], bytes2[i]);
93 }
94
95 try {
96 wrapped.writeTo(null);
97 Assert.fail("IllegalArgumentException should have been thrown");
98 } catch (final IllegalArgumentException ex) {
99
100 }
101 }
102
103 @Test
104 public void testConsumeContent() throws Exception {
105 final String s = "Message content";
106 final StringEntity httpentity = new StringEntity(s);
107 final HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
108 EntityUtils.consume(wrapped);
109 EntityUtils.consume(wrapped);
110 }
111
112 }