2
* $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestStreams.java,v 1.11.2.2 2004/08/09 01:22:05 mbecke Exp $
3
* $Revision: 1.11.2.2 $
4
* $Date: 2004/08/09 01:22:05 $
5
* ====================================================================
7
* Copyright 2002-2004 The Apache Software Foundation
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
20
* ====================================================================
22
* This software consists of voluntary contributions made by many
23
* individuals on behalf of the Apache Software Foundation. For more
24
* information on the Apache Software Foundation, please see
25
* <http://www.apache.org/>.
27
* [Additional notices, if required by prior licensing conditions]
31
package org.apache.commons.httpclient;
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;
39
import junit.framework.Test;
40
import junit.framework.TestCase;
41
import junit.framework.TestSuite;
43
import org.apache.commons.httpclient.methods.GetMethod;
46
public class TestStreams extends TestCase {
48
public TestStreams(String testName) {
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();
57
//Test for when buffer is larger than chunk size
58
InputStream in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(correctInput)), method);
59
byte[] buffer = new byte[300];
60
ByteArrayOutputStream out = new ByteArrayOutputStream();
62
while ((len = in.read(buffer)) > 0) {
63
out.write(buffer, 0, len);
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");
72
// recycle the method so that it can be reused below
75
//Test for when buffer is smaller than chunk size.
76
in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(correctInput)), method);
78
out = new ByteArrayOutputStream();
79
while ((len = in.read(buffer)) > 0) {
80
out.write(buffer, 0, len);
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");
90
public void testCorruptChunkedInputStream1() throws IOException {
91
//missing \r\n at the end of the first chunk
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();
95
InputStream in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(corrupInput)), method);
96
byte[] buffer = new byte[300];
97
ByteArrayOutputStream out = new ByteArrayOutputStream();
100
while ((len = in.read(buffer)) > 0) {
101
out.write(buffer, 0, len);
103
fail("Should have thrown exception");
104
} catch(IOException e) {
105
/* expected exception */
109
public void testEmptyChunkedInputStream() throws IOException {
110
String input = "0\r\n";
111
HttpMethod method = new SimpleHttpMethod();
113
InputStream in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(input)), method);
114
byte[] buffer = new byte[300];
115
ByteArrayOutputStream out = new ByteArrayOutputStream();
117
while ((len = in.read(buffer)) > 0) {
118
out.write(buffer, 0, len);
120
assertEquals(0, out.size());
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");
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);
139
in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10);
141
assertEquals(9, in.skip(10));
142
assertTrue(in.read() == -1);
144
in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 2);
147
assertTrue(in.skip(10) <= 0);
148
assertTrue(in.read() == -1);
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));
158
InputStream in = new ChunkedInputStream(new ByteArrayInputStream(buffer.toByteArray()), new GetMethod());
160
byte[] d = new byte[10];
161
ByteArrayOutputStream result = new ByteArrayOutputStream();
163
while ((len = in.read(d)) > 0) {
164
result.write(d, 0, len);
167
String output = HttpConstants.getContentString(result.toByteArray());
168
assertEquals(input, output);
171
// ------------------------------------------------------- TestCase Methods
173
public static Test suite() {
174
return new TestSuite(TestStreams.class);
177
// ------------------------------------------------------------------- Main
178
public static void main(String args[]) {
179
String[] testCaseName = { TestStreams.class.getName() };
180
junit.textui.TestRunner.main(testCaseName);