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