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
45
46 public class TestStreams extends TestCase {
47
48 public TestStreams(String testName) {
49 super(testName);
50 }
51
52 public void testChunkedInputStream() throws IOException {
53 String correctInput = "10;key=\"value\r\nnewline\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
54 String correctResult = "123456789012345612345";
55 HttpMethod method = new SimpleHttpMethod();
56
57
58 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(correctInput)), method);
59 byte[] buffer = new byte[300];
60 ByteArrayOutputStream out = new ByteArrayOutputStream();
61 int len;
62 while ((len = in.read(buffer)) > 0) {
63 out.write(buffer, 0, len);
64 }
65 String result = HttpConstants.getContentString(out.toByteArray());
66 assertEquals(result, correctResult);
67 Header footer = method.getResponseFooter("footer1");
68 assertEquals(footer.getValue(), "abcde");
69 footer = method.getResponseFooter("footer2");
70 assertEquals(footer.getValue(), "fghij");
71
72
73 method.recycle();
74
75
76 in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(correctInput)), method);
77 buffer = new byte[7];
78 out = new ByteArrayOutputStream();
79 while ((len = in.read(buffer)) > 0) {
80 out.write(buffer, 0, len);
81 }
82 result = HttpConstants.getContentString(out.toByteArray());
83 assertEquals(result, correctResult);
84 footer = method.getResponseFooter("footer1");
85 assertEquals(footer.getValue(), "abcde");
86 footer = method.getResponseFooter("footer2");
87 assertEquals(footer.getValue(), "fghij");
88 }
89
90 public void testCorruptChunkedInputStream1() throws IOException {
91
92 String corrupInput = "10;key=\"value\"\r\n123456789012345\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
93 HttpMethod method = new SimpleHttpMethod();
94
95 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(corrupInput)), method);
96 byte[] buffer = new byte[300];
97 ByteArrayOutputStream out = new ByteArrayOutputStream();
98 int len;
99 try {
100 while ((len = in.read(buffer)) > 0) {
101 out.write(buffer, 0, len);
102 }
103 fail("Should have thrown exception");
104 } catch(IOException e) {
105
106 }
107 }
108
109 public void testEmptyChunkedInputStream() throws IOException {
110 String input = "0\r\n";
111 HttpMethod method = new SimpleHttpMethod();
112
113 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(input)), method);
114 byte[] buffer = new byte[300];
115 ByteArrayOutputStream out = new ByteArrayOutputStream();
116 int len;
117 while ((len = in.read(buffer)) > 0) {
118 out.write(buffer, 0, len);
119 }
120 assertEquals(0, out.size());
121 }
122
123 public void testContentLengthInputStream() throws IOException {
124 String correct = "1234567890123456";
125 InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(HttpConstants.getBytes(correct)), 10);
126 byte[] buffer = new byte[50];
127 int len = in.read(buffer);
128 ByteArrayOutputStream out = new ByteArrayOutputStream();
129 out.write(buffer, 0, len);
130 String result = HttpConstants.getContentString(out.toByteArray());
131 assertEquals(result, "1234567890");
132 }
133
134 public void testChunkedConsitance() throws IOException {
135 String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?//suweb";
136 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
137 OutputStream out = new ChunkedOutputStream(buffer);
138 out.write(HttpConstants.getBytes(input));
139 out.close();
140 buffer.close();
141 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(buffer.toByteArray()), new GetMethod());
142
143 byte[] d = new byte[10];
144 ByteArrayOutputStream result = new ByteArrayOutputStream();
145 int len = 0;
146 while ((len = in.read(d)) > 0) {
147 result.write(d, 0, len);
148 }
149
150 String output = HttpConstants.getContentString(result.toByteArray());
151 assertEquals(input, output);
152 }
153
154
155
156 public static Test suite() {
157 return new TestSuite(TestStreams.class);
158 }
159
160
161 public static void main(String args[]) {
162 String[] testCaseName = { TestStreams.class.getName() };
163 junit.textui.TestRunner.main(testCaseName);
164 }
165 }
166