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 testContentLengthInputStreamSkip() throws IOException {
135 InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10);
136 assertEquals(10, in.skip(10));
137 assertTrue(in.read() == -1);
138
139 in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10);
140 in.read();
141 assertEquals(9, in.skip(10));
142 assertTrue(in.read() == -1);
143
144 in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 2);
145 in.read();
146 in.read();
147 assertTrue(in.skip(10) <= 0);
148 assertTrue(in.read() == -1);
149 }
150
151 public void testChunkedConsitance() throws IOException {
152 String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?//suweb";
153 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
154 OutputStream out = new ChunkedOutputStream(buffer);
155 out.write(HttpConstants.getBytes(input));
156 out.close();
157 buffer.close();
158 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(buffer.toByteArray()), new GetMethod());
159
160 byte[] d = new byte[10];
161 ByteArrayOutputStream result = new ByteArrayOutputStream();
162 int len = 0;
163 while ((len = in.read(d)) > 0) {
164 result.write(d, 0, len);
165 }
166
167 String output = HttpConstants.getContentString(result.toByteArray());
168 assertEquals(input, output);
169 }
170
171
172
173 public static Test suite() {
174 return new TestSuite(TestStreams.class);
175 }
176
177
178 public static void main(String args[]) {
179 String[] testCaseName = { TestStreams.class.getName() };
180 junit.textui.TestRunner.main(testCaseName);
181 }
182 }
183