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
29
30
31 package org.apache.commons.httpclient;
32
33 import java.io.ByteArrayInputStream;
34 import java.io.ByteArrayOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38
39 import junit.framework.Test;
40 import junit.framework.TestCase;
41 import junit.framework.TestSuite;
42
43 import org.apache.commons.httpclient.methods.GetMethod;
44 import org.apache.commons.httpclient.util.EncodingUtil;
45
46
47 public class TestStreams extends TestCase {
48
49 private static final String CONTENT_CHARSET = "ISO-8859-1";
50
51 public TestStreams(String testName) {
52 super(testName);
53 }
54
55 public void testChunkedInputStream() throws IOException {
56 String correctInput = "10;key=\"value\r\nnewline\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
57 String correctResult = "123456789012345612345";
58 HttpMethod method = new FakeHttpMethod();
59
60
61 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(
62 EncodingUtil.getBytes(correctInput, CONTENT_CHARSET)), method);
63 byte[] buffer = new byte[300];
64 ByteArrayOutputStream out = new ByteArrayOutputStream();
65 int len;
66 while ((len = in.read(buffer)) > 0) {
67 out.write(buffer, 0, len);
68 }
69 String result = EncodingUtil.getString(out.toByteArray(), CONTENT_CHARSET);
70 assertEquals(result, correctResult);
71 Header footer = method.getResponseFooter("footer1");
72 assertEquals(footer.getValue(), "abcde");
73 footer = method.getResponseFooter("footer2");
74 assertEquals(footer.getValue(), "fghij");
75
76 method = new FakeHttpMethod();
77
78
79 in = new ChunkedInputStream(new ByteArrayInputStream(
80 EncodingUtil.getBytes(correctInput, CONTENT_CHARSET)), method);
81 buffer = new byte[7];
82 out = new ByteArrayOutputStream();
83 while ((len = in.read(buffer)) > 0) {
84 out.write(buffer, 0, len);
85 }
86 result = EncodingUtil.getString(out.toByteArray(), CONTENT_CHARSET);
87 assertEquals(result, correctResult);
88 footer = method.getResponseFooter("footer1");
89 assertEquals(footer.getValue(), "abcde");
90 footer = method.getResponseFooter("footer2");
91 assertEquals(footer.getValue(), "fghij");
92 }
93
94 public void testCorruptChunkedInputStream1() throws IOException {
95
96 String corrupInput = "10;key=\"value\"\r\n123456789012345\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
97 HttpMethod method = new FakeHttpMethod();
98
99 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(
100 EncodingUtil.getBytes(corrupInput, CONTENT_CHARSET)), method);
101 byte[] buffer = new byte[300];
102 ByteArrayOutputStream out = new ByteArrayOutputStream();
103 int len;
104 try {
105 while ((len = in.read(buffer)) > 0) {
106 out.write(buffer, 0, len);
107 }
108 fail("Should have thrown exception");
109 } catch(IOException e) {
110
111 }
112 }
113
114 public void testEmptyChunkedInputStream() throws IOException {
115 String input = "0\r\n";
116 HttpMethod method = new FakeHttpMethod();
117
118 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(
119 EncodingUtil.getBytes(input, CONTENT_CHARSET)), method);
120 byte[] buffer = new byte[300];
121 ByteArrayOutputStream out = new ByteArrayOutputStream();
122 int len;
123 while ((len = in.read(buffer)) > 0) {
124 out.write(buffer, 0, len);
125 }
126 assertEquals(0, out.size());
127 }
128
129 public void testContentLengthInputStream() throws IOException {
130 String correct = "1234567890123456";
131 InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(
132 EncodingUtil.getBytes(correct, CONTENT_CHARSET)), 10L);
133 byte[] buffer = new byte[50];
134 int len = in.read(buffer);
135 ByteArrayOutputStream out = new ByteArrayOutputStream();
136 out.write(buffer, 0, len);
137 String result = EncodingUtil.getString(out.toByteArray(), CONTENT_CHARSET);
138 assertEquals(result, "1234567890");
139 }
140
141 public void testContentLengthInputStreamSkip() throws IOException {
142 InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10L);
143 assertEquals(10, in.skip(10));
144 assertTrue(in.read() == -1);
145
146 in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10L);
147 in.read();
148 assertEquals(9, in.skip(10));
149 assertTrue(in.read() == -1);
150
151 in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 2L);
152 in.read();
153 in.read();
154 assertTrue(in.skip(10) <= 0);
155 assertTrue(in.read() == -1);
156 }
157
158 public void testChunkedConsitance() throws IOException {
159 String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?//suweb";
160 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
161 OutputStream out = new ChunkedOutputStream(buffer);
162 out.write(EncodingUtil.getBytes(input, CONTENT_CHARSET));
163 out.close();
164 buffer.close();
165 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(buffer.toByteArray()), new GetMethod());
166
167 byte[] d = new byte[10];
168 ByteArrayOutputStream result = new ByteArrayOutputStream();
169 int len = 0;
170 while ((len = in.read(d)) > 0) {
171 result.write(d, 0, len);
172 }
173
174 String output = EncodingUtil.getString(result.toByteArray(), CONTENT_CHARSET);
175 assertEquals(input, output);
176 }
177
178 public void testChunkedOutputStream() throws IOException {
179 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
180 ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
181 out.write('1');
182 out.write('2');
183 out.write('3');
184 out.write('4');
185 out.finish();
186 out.close();
187
188 byte [] rawdata = buffer.toByteArray();
189
190 assertEquals(19, rawdata.length);
191 assertEquals('2', rawdata[0]);
192 assertEquals('\r', rawdata[1]);
193 assertEquals('\n', rawdata[2]);
194 assertEquals('1', rawdata[3]);
195 assertEquals('2', rawdata[4]);
196 assertEquals('\r', rawdata[5]);
197 assertEquals('\n', rawdata[6]);
198 assertEquals('2', rawdata[7]);
199 assertEquals('\r', rawdata[8]);
200 assertEquals('\n', rawdata[9]);
201 assertEquals('3', rawdata[10]);
202 assertEquals('4', rawdata[11]);
203 assertEquals('\r', rawdata[12]);
204 assertEquals('\n', rawdata[13]);
205 assertEquals('0', rawdata[14]);
206 assertEquals('\r', rawdata[15]);
207 assertEquals('\n', rawdata[16]);
208 assertEquals('\r', rawdata[17]);
209 assertEquals('\n', rawdata[18]);
210 }
211
212 public void testChunkedOutputStreamLargeChunk() throws IOException {
213 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
214 ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
215 out.write(new byte[] {'1', '2', '3', '4'});
216 out.finish();
217 out.close();
218
219 byte [] rawdata = buffer.toByteArray();
220
221 assertEquals(14, rawdata.length);
222 assertEquals('4', rawdata[0]);
223 assertEquals('\r', rawdata[1]);
224 assertEquals('\n', rawdata[2]);
225 assertEquals('1', rawdata[3]);
226 assertEquals('2', rawdata[4]);
227 assertEquals('3', rawdata[5]);
228 assertEquals('4', rawdata[6]);
229 assertEquals('\r', rawdata[7]);
230 assertEquals('\n', rawdata[8]);
231 assertEquals('0', rawdata[9]);
232 assertEquals('\r', rawdata[10]);
233 assertEquals('\n', rawdata[11]);
234 assertEquals('\r', rawdata[12]);
235 assertEquals('\n', rawdata[13]);
236 }
237
238 public void testChunkedOutputStreamSmallChunk() throws IOException {
239 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
240 ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
241 out.write('1');
242 out.finish();
243 out.close();
244
245 byte [] rawdata = buffer.toByteArray();
246
247 assertEquals(11, rawdata.length);
248 assertEquals('1', rawdata[0]);
249 assertEquals('\r', rawdata[1]);
250 assertEquals('\n', rawdata[2]);
251 assertEquals('1', rawdata[3]);
252 assertEquals('\r', rawdata[4]);
253 assertEquals('\n', rawdata[5]);
254 assertEquals('0', rawdata[6]);
255 assertEquals('\r', rawdata[7]);
256 assertEquals('\n', rawdata[8]);
257 assertEquals('\r', rawdata[9]);
258 assertEquals('\n', rawdata[10]);
259 }
260
261
262
263 public static Test suite() {
264 return new TestSuite(TestStreams.class);
265 }
266
267
268 public static void main(String args[]) {
269 String[] testCaseName = { TestStreams.class.getName() };
270 junit.textui.TestRunner.main(testCaseName);
271 }
272 }
273