KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestStreams


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestStreams.java,v 1.11.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.11.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
5  * ====================================================================
6  *
7  * Copyright 2002-2004 The Apache Software Foundation
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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  * ====================================================================
21  *
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/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30
31 package org.apache.commons.httpclient;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.ByteArrayOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
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 JavaDoc testName) {
49         super(testName);
50     }
51
52     public void testChunkedInputStream() throws IOException JavaDoc {
53         String JavaDoc correctInput = "10;key=\"value\r\nnewline\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
54         String JavaDoc correctResult = "123456789012345612345";
55         HttpMethod method = new SimpleHttpMethod();
56
57         //Test for when buffer is larger than chunk size
58
InputStream JavaDoc in = new ChunkedInputStream(new ByteArrayInputStream JavaDoc(HttpConstants.getBytes(correctInput)), method);
59         byte[] buffer = new byte[300];
60         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
61         int len;
62         while ((len = in.read(buffer)) > 0) {
63             out.write(buffer, 0, len);
64         }
65         String JavaDoc 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         // recycle the method so that it can be reused below
73
method.recycle();
74
75         //Test for when buffer is smaller than chunk size.
76
in = new ChunkedInputStream(new ByteArrayInputStream JavaDoc(HttpConstants.getBytes(correctInput)), method);
77         buffer = new byte[7];
78         out = new ByteArrayOutputStream JavaDoc();
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 JavaDoc {
91         //missing \r\n at the end of the first chunk
92
String JavaDoc 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 JavaDoc in = new ChunkedInputStream(new ByteArrayInputStream JavaDoc(HttpConstants.getBytes(corrupInput)), method);
96         byte[] buffer = new byte[300];
97         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
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 JavaDoc e) {
105             /* expected exception */
106         }
107     }
108
109     public void testEmptyChunkedInputStream() throws IOException JavaDoc {
110         String JavaDoc input = "0\r\n";
111         HttpMethod method = new SimpleHttpMethod();
112
113         InputStream JavaDoc in = new ChunkedInputStream(new ByteArrayInputStream JavaDoc(HttpConstants.getBytes(input)), method);
114         byte[] buffer = new byte[300];
115         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
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 JavaDoc {
124         String JavaDoc correct = "1234567890123456";
125         InputStream JavaDoc in = new ContentLengthInputStream(new ByteArrayInputStream JavaDoc(HttpConstants.getBytes(correct)), 10);
126         byte[] buffer = new byte[50];
127         int len = in.read(buffer);
128         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
129         out.write(buffer, 0, len);
130         String JavaDoc result = HttpConstants.getContentString(out.toByteArray());
131         assertEquals(result, "1234567890");
132     }
133
134     public void testChunkedConsitance() throws IOException JavaDoc {
135         String JavaDoc input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
136         ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
137         OutputStream JavaDoc out = new ChunkedOutputStream(buffer);
138         out.write(HttpConstants.getBytes(input));
139         out.close();
140         buffer.close();
141         InputStream JavaDoc in = new ChunkedInputStream(new ByteArrayInputStream JavaDoc(buffer.toByteArray()), new GetMethod());
142
143         byte[] d = new byte[10];
144         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
145         int len = 0;
146         while ((len = in.read(d)) > 0) {
147             result.write(d, 0, len);
148         }
149
150         String JavaDoc output = HttpConstants.getContentString(result.toByteArray());
151         assertEquals(input, output);
152     }
153
154     // ------------------------------------------------------- TestCase Methods
155

156     public static Test suite() {
157         return new TestSuite(TestStreams.class);
158     }
159
160     // ------------------------------------------------------------------- Main
161
public static void main(String JavaDoc args[]) {
162         String JavaDoc[] testCaseName = { TestStreams.class.getName() };
163         junit.textui.TestRunner.main(testCaseName);
164     }
165 }
166
167
Popular Tags