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 TestInputStreamEntity {
43
44 @Test
45 public void testBasics() throws Exception {
46 final byte[] bytes = "Message content".getBytes(Consts.ISO_8859_1);
47 final InputStream inStream = new ByteArrayInputStream(bytes);
48 final InputStreamEntity httpentity = new InputStreamEntity(inStream, bytes.length);
49
50 Assert.assertEquals(bytes.length, httpentity.getContentLength());
51 Assert.assertEquals(inStream, httpentity.getContent());
52 Assert.assertNotNull(httpentity.getContent());
53 Assert.assertFalse(httpentity.isRepeatable());
54 Assert.assertTrue(httpentity.isStreaming());
55 }
56
57 @Test(expected = IllegalArgumentException.class)
58 public void testIllegalConstructor() throws Exception {
59 new InputStreamEntity(null, 0);
60 }
61
62 @Test
63 public void testUnknownLengthConstructor() throws Exception {
64 final InputStream inStream = new ByteArrayInputStream(new byte[0]);
65 final InputStreamEntity httpentity = new InputStreamEntity(inStream);
66 Assert.assertEquals(-1, httpentity.getContentLength());
67 }
68
69 @Test
70 public void testWriteTo() throws Exception {
71 final String message = "Message content";
72 final byte[] bytes = message.getBytes(Consts.ISO_8859_1);
73 final InputStream inStream = new ByteArrayInputStream(bytes);
74 final InputStreamEntity httpentity = new InputStreamEntity(inStream, bytes.length);
75
76 final ByteArrayOutputStream out = new ByteArrayOutputStream();
77 httpentity.writeTo(out);
78 final byte[] writtenBytes = out.toByteArray();
79 Assert.assertNotNull(writtenBytes);
80 Assert.assertEquals(bytes.length, writtenBytes.length);
81
82 final String s = new String(writtenBytes, Consts.ISO_8859_1.name());
83 Assert.assertEquals(message, s);
84 }
85
86 @Test
87 public void testWriteToPartialContent() throws Exception {
88 final String message = "Message content";
89 final byte[] bytes = message.getBytes(Consts.ISO_8859_1);
90 final InputStream inStream = new ByteArrayInputStream(bytes);
91 final int contentLength = 7;
92 final InputStreamEntity httpentity = new InputStreamEntity(inStream, contentLength);
93
94 final ByteArrayOutputStream out = new ByteArrayOutputStream();
95 httpentity.writeTo(out);
96 final byte[] writtenBytes = out.toByteArray();
97 Assert.assertNotNull(writtenBytes);
98 Assert.assertEquals(contentLength, writtenBytes.length);
99
100 final String s = new String(writtenBytes, Consts.ISO_8859_1.name());
101 Assert.assertEquals(message.substring(0, contentLength), s);
102 }
103
104 @Test
105 public void testWriteToUnknownLength() throws Exception {
106 final String message = "Message content";
107 final byte[] bytes = message.getBytes(Consts.ISO_8859_1);
108 final InputStream inStream = new ByteArrayInputStream(bytes);
109 final InputStreamEntity httpentity = new InputStreamEntity(inStream);
110
111 final ByteArrayOutputStream out = new ByteArrayOutputStream();
112 httpentity.writeTo(out);
113 final byte[] writtenBytes = out.toByteArray();
114 Assert.assertNotNull(writtenBytes);
115 Assert.assertEquals(bytes.length, writtenBytes.length);
116
117 final String s = new String(writtenBytes, Consts.ISO_8859_1.name());
118 Assert.assertEquals(message, s);
119 }
120
121 @Test(expected = IllegalArgumentException.class)
122 public void testWriteToNull() throws Exception {
123 final InputStream inStream = new ByteArrayInputStream(new byte[0]);
124 final InputStreamEntity httpentity = new InputStreamEntity(inStream, 0);
125 httpentity.writeTo(null);
126 }
127 }