2
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/test/java/org/apache/http/impl/io/TestChunkCoding.java $
4
* $Date: 2008-10-18 16:26:42 +0200 (Sat, 18 Oct 2008) $
5
* ====================================================================
6
* Licensed to the Apache Software Foundation (ASF) under one
7
* or more contributor license agreements. See the NOTICE file
8
* distributed with this work for additional information
9
* regarding copyright ownership. The ASF licenses this file
10
* to you under the Apache License, Version 2.0 (the
11
* "License"); you may not use this file except in compliance
12
* with the License. You may obtain a copy of the License at
14
* http://www.apache.org/licenses/LICENSE-2.0
16
* Unless required by applicable law or agreed to in writing,
17
* software distributed under the License is distributed on an
18
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19
* KIND, either express or implied. See the License for the
20
* specific language governing permissions and limitations
22
* ====================================================================
24
* This software consists of voluntary contributions made by many
25
* individuals on behalf of the Apache Software Foundation. For more
26
* information on the Apache Software Foundation, please see
27
* <http://www.apache.org/>.
31
package org.apache.http.impl.io;
33
import java.io.ByteArrayOutputStream;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.OutputStream;
38
import org.apache.http.Header;
39
import org.apache.http.MalformedChunkCodingException;
40
import org.apache.http.impl.io.ChunkedInputStream;
41
import org.apache.http.impl.io.ChunkedOutputStream;
42
import org.apache.http.io.SessionInputBuffer;
43
import org.apache.http.mockup.SessionInputBufferMockup;
44
import org.apache.http.mockup.SessionOutputBufferMockup;
45
import org.apache.http.util.EncodingUtils;
47
import junit.framework.Test;
48
import junit.framework.TestCase;
49
import junit.framework.TestSuite;
51
public class TestChunkCoding extends TestCase {
53
private static final String CONTENT_CHARSET = "ISO-8859-1";
55
public TestChunkCoding(String testName) {
59
// ------------------------------------------------------- TestCase Methods
61
public static Test suite() {
62
return new TestSuite(TestChunkCoding.class);
65
// ------------------------------------------------------------------- Main
66
public static void main(String args[]) {
67
String[] testCaseName = { TestChunkCoding.class.getName() };
68
junit.textui.TestRunner.main(testCaseName);
71
public void testConstructors() throws Exception {
73
new ChunkedInputStream((SessionInputBuffer)null);
74
fail("IllegalArgumentException should have been thrown");
75
} catch (IllegalArgumentException ex) {
78
new MalformedChunkCodingException();
79
new MalformedChunkCodingException("");
82
private final static String CHUNKED_INPUT
83
= "10;key=\"value\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
85
private final static String CHUNKED_RESULT
86
= "123456789012345612345";
88
// Test for when buffer is larger than chunk size
89
public void testChunkedInputStreamLargeBuffer() throws IOException {
90
ChunkedInputStream in = new ChunkedInputStream(
91
new SessionInputBufferMockup(
92
EncodingUtils.getBytes(CHUNKED_INPUT, CONTENT_CHARSET)));
93
byte[] buffer = new byte[300];
94
ByteArrayOutputStream out = new ByteArrayOutputStream();
96
while ((len = in.read(buffer)) > 0) {
97
out.write(buffer, 0, len);
99
assertEquals(-1, in.read(buffer));
100
assertEquals(-1, in.read(buffer));
104
String result = EncodingUtils.getString(out.toByteArray(), CONTENT_CHARSET);
105
assertEquals(result, CHUNKED_RESULT);
107
Header[] footers = in.getFooters();
108
assertNotNull(footers);
109
assertEquals(2, footers.length);
110
assertEquals("Footer1", footers[0].getName());
111
assertEquals("abcde", footers[0].getValue());
112
assertEquals("Footer2", footers[1].getName());
113
assertEquals("fghij", footers[1].getValue());
116
//Test for when buffer is smaller than chunk size.
117
public void testChunkedInputStreamSmallBuffer() throws IOException {
118
ChunkedInputStream in = new ChunkedInputStream(
119
new SessionInputBufferMockup(
120
EncodingUtils.getBytes(CHUNKED_INPUT, CONTENT_CHARSET)));
122
byte[] buffer = new byte[7];
123
ByteArrayOutputStream out = new ByteArrayOutputStream();
125
while ((len = in.read(buffer)) > 0) {
126
out.write(buffer, 0, len);
128
assertEquals(-1, in.read(buffer));
129
assertEquals(-1, in.read(buffer));
133
EncodingUtils.getString(out.toByteArray(), CONTENT_CHARSET);
134
Header[] footers = in.getFooters();
135
assertNotNull(footers);
136
assertEquals(2, footers.length);
137
assertEquals("Footer1", footers[0].getName());
138
assertEquals("abcde", footers[0].getValue());
139
assertEquals("Footer2", footers[1].getName());
140
assertEquals("fghij", footers[1].getValue());
144
public void testChunkedInputStreamOneByteRead() throws IOException {
145
String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
146
ChunkedInputStream in = new ChunkedInputStream(
147
new SessionInputBufferMockup(
148
EncodingUtils.getBytes(s, CONTENT_CHARSET)));
151
while ((ch = in.read()) != -1) {
155
assertEquals(-1, in.read());
156
assertEquals(-1, in.read());
161
public void testChunkedInputStreamClose() throws IOException {
162
String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
163
ChunkedInputStream in = new ChunkedInputStream(
164
new SessionInputBufferMockup(
165
EncodingUtils.getBytes(s, CONTENT_CHARSET)));
170
fail("IOException should have been thrown");
171
} catch (IOException ex) {
174
byte[] tmp = new byte[10];
177
fail("IOException should have been thrown");
178
} catch (IOException ex) {
182
in.read(tmp, 0, tmp.length);
183
fail("IOException should have been thrown");
184
} catch (IOException ex) {
189
public void testChunkedOutputStreamClose() throws IOException {
190
ChunkedOutputStream out = new ChunkedOutputStream(
191
new SessionOutputBufferMockup());
195
out.write(new byte[] {1,2,3});
196
fail("IOException should have been thrown");
197
} catch (IOException ex) {
202
fail("IOException should have been thrown");
203
} catch (IOException ex) {
208
// Missing closing chunk
209
public void testChunkedInputStreamNoClosingChunk() throws IOException {
210
String s = "5\r\n01234\r\n";
211
ChunkedInputStream in = new ChunkedInputStream(
212
new SessionInputBufferMockup(
213
EncodingUtils.getBytes(s, CONTENT_CHARSET)));
214
byte[] tmp = new byte[5];
215
assertEquals(5, in.read(tmp));
216
assertEquals(-1, in.read());
219
// Missing \r\n at the end of the first chunk
220
public void testCorruptChunkedInputStreamMissingCRLF() throws IOException {
221
String s = "5\r\n012345\r\n56789\r\n0\r\n";
222
InputStream in = new ChunkedInputStream(
223
new SessionInputBufferMockup(
224
EncodingUtils.getBytes(s, CONTENT_CHARSET)));
225
byte[] buffer = new byte[300];
226
ByteArrayOutputStream out = new ByteArrayOutputStream();
229
while ((len = in.read(buffer)) > 0) {
230
out.write(buffer, 0, len);
232
fail("MalformedChunkCodingException should have been thrown");
233
} catch(MalformedChunkCodingException e) {
234
/* expected exception */
239
public void testCorruptChunkedInputStreamMissingLF() throws IOException {
240
String s = "5\r01234\r\n5\r\n56789\r\n0\r\n";
241
InputStream in = new ChunkedInputStream(
242
new SessionInputBufferMockup(
243
EncodingUtils.getBytes(s, CONTENT_CHARSET)));
246
fail("MalformedChunkCodingException should have been thrown");
247
} catch(MalformedChunkCodingException e) {
248
/* expected exception */
252
// Invalid chunk size
253
public void testCorruptChunkedInputStreamInvalidSize() throws IOException {
254
String s = "whatever\r\n01234\r\n5\r\n56789\r\n0\r\n";
255
InputStream in = new ChunkedInputStream(
256
new SessionInputBufferMockup(
257
EncodingUtils.getBytes(s, CONTENT_CHARSET)));
260
fail("MalformedChunkCodingException should have been thrown");
261
} catch(MalformedChunkCodingException e) {
262
/* expected exception */
266
// Negative chunk size
267
public void testCorruptChunkedInputStreamNegativeSize() throws IOException {
268
String s = "-5\r\n01234\r\n5\r\n56789\r\n0\r\n";
269
InputStream in = new ChunkedInputStream(
270
new SessionInputBufferMockup(
271
EncodingUtils.getBytes(s, CONTENT_CHARSET)));
274
fail("MalformedChunkCodingException should have been thrown");
275
} catch(MalformedChunkCodingException e) {
276
/* expected exception */
281
public void testCorruptChunkedInputStreamTruncatedChunk() throws IOException {
282
String s = "3\r\n12";
283
InputStream in = new ChunkedInputStream(
284
new SessionInputBufferMockup(
285
EncodingUtils.getBytes(s, CONTENT_CHARSET)));
286
byte[] buffer = new byte[300];
287
assertEquals(2, in.read(buffer));
290
fail("MalformedChunkCodingException should have been thrown");
291
} catch(MalformedChunkCodingException e) {
292
/* expected exception */
297
public void testCorruptChunkedInputStreamInvalidFooter() throws IOException {
298
String s = "1\r\n0\r\n0\r\nstuff\r\n";
299
InputStream in = new ChunkedInputStream(
300
new SessionInputBufferMockup(
301
EncodingUtils.getBytes(s, CONTENT_CHARSET)));
305
fail("MalformedChunkCodingException should have been thrown");
306
} catch(MalformedChunkCodingException e) {
307
/* expected exception */
311
public void testEmptyChunkedInputStream() throws IOException {
312
String input = "0\r\n";
313
InputStream in = new ChunkedInputStream(
314
new SessionInputBufferMockup(
315
EncodingUtils.getBytes(input, CONTENT_CHARSET)));
316
byte[] buffer = new byte[300];
317
ByteArrayOutputStream out = new ByteArrayOutputStream();
319
while ((len = in.read(buffer)) > 0) {
320
out.write(buffer, 0, len);
322
assertEquals(0, out.size());
325
public void testChunkedConsistence() throws IOException {
326
String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
327
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
328
OutputStream out = new ChunkedOutputStream(new SessionOutputBufferMockup(buffer));
329
out.write(EncodingUtils.getBytes(input, CONTENT_CHARSET));
334
InputStream in = new ChunkedInputStream(
335
new SessionInputBufferMockup(
336
buffer.toByteArray()));
338
byte[] d = new byte[10];
339
ByteArrayOutputStream result = new ByteArrayOutputStream();
341
while ((len = in.read(d)) > 0) {
342
result.write(d, 0, len);
345
String output = EncodingUtils.getString(result.toByteArray(), CONTENT_CHARSET);
346
assertEquals(input, output);
349
public void testChunkedOutputStream() throws IOException {
350
SessionOutputBufferMockup buffer = new SessionOutputBufferMockup();
351
ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
359
byte [] rawdata = buffer.getData();
361
assertEquals(19, rawdata.length);
362
assertEquals('2', rawdata[0]);
363
assertEquals('\r', rawdata[1]);
364
assertEquals('\n', rawdata[2]);
365
assertEquals('1', rawdata[3]);
366
assertEquals('2', rawdata[4]);
367
assertEquals('\r', rawdata[5]);
368
assertEquals('\n', rawdata[6]);
369
assertEquals('2', rawdata[7]);
370
assertEquals('\r', rawdata[8]);
371
assertEquals('\n', rawdata[9]);
372
assertEquals('3', rawdata[10]);
373
assertEquals('4', rawdata[11]);
374
assertEquals('\r', rawdata[12]);
375
assertEquals('\n', rawdata[13]);
376
assertEquals('0', rawdata[14]);
377
assertEquals('\r', rawdata[15]);
378
assertEquals('\n', rawdata[16]);
379
assertEquals('\r', rawdata[17]);
380
assertEquals('\n', rawdata[18]);
383
public void testChunkedOutputStreamLargeChunk() throws IOException {
384
SessionOutputBufferMockup buffer = new SessionOutputBufferMockup();
385
ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
386
out.write(new byte[] {'1', '2', '3', '4'});
390
byte [] rawdata = buffer.getData();
392
assertEquals(14, rawdata.length);
393
assertEquals('4', rawdata[0]);
394
assertEquals('\r', rawdata[1]);
395
assertEquals('\n', rawdata[2]);
396
assertEquals('1', rawdata[3]);
397
assertEquals('2', rawdata[4]);
398
assertEquals('3', rawdata[5]);
399
assertEquals('4', rawdata[6]);
400
assertEquals('\r', rawdata[7]);
401
assertEquals('\n', rawdata[8]);
402
assertEquals('0', rawdata[9]);
403
assertEquals('\r', rawdata[10]);
404
assertEquals('\n', rawdata[11]);
405
assertEquals('\r', rawdata[12]);
406
assertEquals('\n', rawdata[13]);
409
public void testChunkedOutputStreamSmallChunk() throws IOException {
410
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
411
ChunkedOutputStream out = new ChunkedOutputStream(
412
new SessionOutputBufferMockup(buffer), 2);
417
byte [] rawdata = buffer.toByteArray();
419
assertEquals(11, rawdata.length);
420
assertEquals('1', rawdata[0]);
421
assertEquals('\r', rawdata[1]);
422
assertEquals('\n', rawdata[2]);
423
assertEquals('1', rawdata[3]);
424
assertEquals('\r', rawdata[4]);
425
assertEquals('\n', rawdata[5]);
426
assertEquals('0', rawdata[6]);
427
assertEquals('\r', rawdata[7]);
428
assertEquals('\n', rawdata[8]);
429
assertEquals('\r', rawdata[9]);
430
assertEquals('\n', rawdata[10]);