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.impl.io;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33
34 import org.apache.http.ConnectionClosedException;
35 import org.apache.http.Consts;
36 import org.apache.http.impl.SessionInputBufferMock;
37 import org.apache.http.io.SessionInputBuffer;
38 import org.junit.Assert;
39 import org.junit.Test;
40
41 public class TestContentLengthInputStream {
42
43 @Test
44 public void testConstructors() throws Exception {
45 final ContentLengthInputStream in = new ContentLengthInputStream(
46 new SessionInputBufferMock(new byte[] {}), 0);
47 in.close();
48 try {
49 new ContentLengthInputStream(null, 10);
50 Assert.fail("IllegalArgumentException should have been thrown");
51 } catch (final IllegalArgumentException ex) {
52
53 }
54 try {
55 new ContentLengthInputStream(new SessionInputBufferMock(new byte[] {}), -10);
56 Assert.fail("IllegalArgumentException should have been thrown");
57 } catch (final IllegalArgumentException ex) {
58
59 }
60 }
61
62 @Test
63 public void testBasics() throws IOException {
64 final String correct = "1234567890123456";
65 final InputStream in = new ContentLengthInputStream(
66 new SessionInputBufferMock(correct, Consts.ISO_8859_1), 10L);
67 final ByteArrayOutputStream out = new ByteArrayOutputStream();
68
69 final byte[] buffer = new byte[50];
70 int len = in.read(buffer, 0, 2);
71 out.write(buffer, 0, len);
72 len = in.read(buffer);
73 out.write(buffer, 0, len);
74
75 final String result = new String(out.toByteArray(), Consts.ISO_8859_1);
76 Assert.assertEquals(result, "1234567890");
77 in.close();
78 }
79
80 @Test
81 public void testSkip() throws IOException {
82 InputStream in = new ContentLengthInputStream(new SessionInputBufferMock(new byte[20]), 10L);
83 Assert.assertEquals(10, in.skip(10));
84 Assert.assertTrue(in.read() == -1);
85
86 in = new ContentLengthInputStream(new SessionInputBufferMock(new byte[20]), 10L);
87 in.read();
88 Assert.assertEquals(9, in.skip(10));
89 Assert.assertTrue(in.read() == -1);
90 in.close();
91
92 in = new ContentLengthInputStream(new SessionInputBufferMock(new byte[20]), 2L);
93 in.read();
94 in.read();
95 Assert.assertTrue(in.skip(10) <= 0);
96 Assert.assertTrue(in.skip(-1) == 0);
97 Assert.assertTrue(in.read() == -1);
98 in.close();
99
100 in = new ContentLengthInputStream(new SessionInputBufferMock(new byte[20]), 10L);
101 Assert.assertEquals(5,in.skip(5));
102 Assert.assertEquals(5, in.read(new byte[20]));
103 in.close();
104 }
105
106 @Test
107 public void testAvailable() throws IOException {
108 final InputStream in = new ContentLengthInputStream(
109 new SessionInputBufferMock(new byte[] {1, 2, 3}), 3L);
110 Assert.assertEquals(0, in.available());
111 in.read();
112 Assert.assertEquals(2, in.available());
113 in.close();
114 }
115
116 @Test
117 public void testClose() throws IOException {
118 final String correct = "1234567890123456-";
119 final SessionInputBuffer inBuffer = new SessionInputBufferMock(correct, Consts.ISO_8859_1);
120 final InputStream in = new ContentLengthInputStream(inBuffer, 16L);
121 in.close();
122 in.close();
123 try {
124 in.read();
125 Assert.fail("IOException should have been thrown");
126 } catch (final IOException ex) {
127
128 }
129 final byte[] tmp = new byte[10];
130 try {
131 in.read(tmp);
132 Assert.fail("IOException should have been thrown");
133 } catch (final IOException ex) {
134
135 }
136 try {
137 in.read(tmp, 0, tmp.length);
138 Assert.fail("IOException should have been thrown");
139 } catch (final IOException ex) {
140
141 }
142 Assert.assertEquals('-', inBuffer.read());
143 }
144
145 @Test
146 public void testTruncatedContent() throws IOException {
147 final String correct = "1234567890123456";
148 final SessionInputBuffer inBuffer = new SessionInputBufferMock(correct, Consts.ISO_8859_1);
149 final InputStream in = new ContentLengthInputStream(inBuffer, 32L);
150 final byte[] tmp = new byte[32];
151 final int byteRead = in.read(tmp);
152 Assert.assertEquals(16, byteRead);
153 try {
154 in.read(tmp);
155 Assert.fail("ConnectionClosedException should have been closed");
156 } catch (final ConnectionClosedException ex) {
157 }
158 try {
159 in.read();
160 Assert.fail("ConnectionClosedException should have been closed");
161 } catch (final ConnectionClosedException ex) {
162 }
163 try {
164 in.close();
165 Assert.fail("ConnectionClosedException should have been closed");
166 } catch (final ConnectionClosedException ex) {
167 }
168 }
169
170 }
171