1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestStreams.java,v 1.15 2004/03/11 20:55:27 olegk Exp $
3    * $Revision: 1.15 $
4    * $Date: 2004/03/11 20:55:27 $
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;
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  import org.apache.commons.httpclient.util.EncodingUtil;
45  
46  
47  public class TestStreams extends TestCase {
48  
49      private static final String CONTENT_CHARSET = "ISO-8859-1";
50      
51      public TestStreams(String testName) {
52          super(testName);
53      }
54  
55      public void testChunkedInputStream() throws IOException {
56          String correctInput = "10;key=\"value\r\nnewline\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
57          String correctResult = "123456789012345612345";
58          HttpMethod method = new SimpleHttpMethod();
59  
60          //Test for when buffer is larger than chunk size
61          InputStream in = new ChunkedInputStream(new ByteArrayInputStream(
62              EncodingUtil.getBytes(correctInput, CONTENT_CHARSET)), method);
63          byte[] buffer = new byte[300];
64          ByteArrayOutputStream out = new ByteArrayOutputStream();
65          int len;
66          while ((len = in.read(buffer)) > 0) {
67              out.write(buffer, 0, len);
68          }
69          String result = EncodingUtil.getString(out.toByteArray(), CONTENT_CHARSET);
70          assertEquals(result, correctResult);
71          Header footer = method.getResponseFooter("footer1");
72          assertEquals(footer.getValue(), "abcde");
73          footer = method.getResponseFooter("footer2");
74          assertEquals(footer.getValue(), "fghij");
75  
76          // recycle the method so that it can be reused below
77          method.recycle();
78  
79          //Test for when buffer is smaller than chunk size.
80          in = new ChunkedInputStream(new ByteArrayInputStream(
81              EncodingUtil.getBytes(correctInput, CONTENT_CHARSET)), method);
82          buffer = new byte[7];
83          out = new ByteArrayOutputStream();
84          while ((len = in.read(buffer)) > 0) {
85              out.write(buffer, 0, len);
86          }
87          result = EncodingUtil.getString(out.toByteArray(), CONTENT_CHARSET);
88          assertEquals(result, correctResult);
89          footer = method.getResponseFooter("footer1");
90          assertEquals(footer.getValue(), "abcde");
91          footer = method.getResponseFooter("footer2");
92          assertEquals(footer.getValue(), "fghij");
93      }
94  
95      public void testCorruptChunkedInputStream1() throws IOException {
96          //missing \r\n at the end of the first chunk
97          String corrupInput = "10;key=\"value\"\r\n123456789012345\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
98          HttpMethod method = new SimpleHttpMethod();
99  
100         InputStream in = new ChunkedInputStream(new ByteArrayInputStream(
101             EncodingUtil.getBytes(corrupInput, CONTENT_CHARSET)), method);
102         byte[] buffer = new byte[300];
103         ByteArrayOutputStream out = new ByteArrayOutputStream();
104         int len;
105         try {
106             while ((len = in.read(buffer)) > 0) {
107                 out.write(buffer, 0, len);
108             }
109             fail("Should have thrown exception");
110         } catch(IOException e) {
111             /* expected exception */
112         }
113     }
114 
115     public void testEmptyChunkedInputStream() throws IOException {
116         String input = "0\r\n";
117         HttpMethod method = new SimpleHttpMethod();
118 
119         InputStream in = new ChunkedInputStream(new ByteArrayInputStream(
120             EncodingUtil.getBytes(input, CONTENT_CHARSET)), method);
121         byte[] buffer = new byte[300];
122         ByteArrayOutputStream out = new ByteArrayOutputStream();
123         int len;
124         while ((len = in.read(buffer)) > 0) {
125             out.write(buffer, 0, len);
126         }
127         assertEquals(0, out.size());
128     }
129 
130     public void testContentLengthInputStream() throws IOException {
131         String correct = "1234567890123456";
132         InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(
133             EncodingUtil.getBytes(correct, CONTENT_CHARSET)), 10L);
134         byte[] buffer = new byte[50];
135         int len = in.read(buffer);
136         ByteArrayOutputStream out = new ByteArrayOutputStream();
137         out.write(buffer, 0, len);
138         String result = EncodingUtil.getString(out.toByteArray(), CONTENT_CHARSET);
139         assertEquals(result, "1234567890");
140     }
141 
142     public void testChunkedConsitance() throws IOException {
143         String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?//suweb";
144         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
145         OutputStream out = new ChunkedOutputStream(buffer);
146         out.write(EncodingUtil.getBytes(input, CONTENT_CHARSET));
147         out.close();
148         buffer.close();
149         InputStream in = new ChunkedInputStream(new ByteArrayInputStream(buffer.toByteArray()), new GetMethod());
150 
151         byte[] d = new byte[10];
152         ByteArrayOutputStream result = new ByteArrayOutputStream();
153         int len = 0;
154         while ((len = in.read(d)) > 0) {
155             result.write(d, 0, len);
156         }
157 
158         String output = EncodingUtil.getString(result.toByteArray(), CONTENT_CHARSET);
159         assertEquals(input, output);
160     }
161 
162     public void testChunkedOutputStream() throws IOException {
163         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
164         ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
165         out.write('1');  
166         out.write('2');  
167         out.write('3');  
168         out.write('4');  
169         out.finish();
170         out.close();
171         
172         byte [] rawdata =  buffer.toByteArray();
173         
174         assertEquals(19, rawdata.length);
175         assertEquals('2', rawdata[0]);
176         assertEquals('\r', rawdata[1]);
177         assertEquals('\n', rawdata[2]);
178         assertEquals('1', rawdata[3]);
179         assertEquals('2', rawdata[4]);
180         assertEquals('\r', rawdata[5]);
181         assertEquals('\n', rawdata[6]);
182         assertEquals('2', rawdata[7]);
183         assertEquals('\r', rawdata[8]);
184         assertEquals('\n', rawdata[9]);
185         assertEquals('3', rawdata[10]);
186         assertEquals('4', rawdata[11]);
187         assertEquals('\r', rawdata[12]);
188         assertEquals('\n', rawdata[13]);
189         assertEquals('0', rawdata[14]);
190         assertEquals('\r', rawdata[15]);
191         assertEquals('\n', rawdata[16]);
192         assertEquals('\r', rawdata[17]);
193         assertEquals('\n', rawdata[18]);
194     }
195 
196     public void testChunkedOutputStreamLargeChunk() throws IOException {
197         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
198         ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
199         out.write(new byte[] {'1', '2', '3', '4'});
200         out.finish();
201         out.close();
202         
203         byte [] rawdata =  buffer.toByteArray();
204         
205         assertEquals(14, rawdata.length);
206         assertEquals('4', rawdata[0]);
207         assertEquals('\r', rawdata[1]);
208         assertEquals('\n', rawdata[2]);
209         assertEquals('1', rawdata[3]);
210         assertEquals('2', rawdata[4]);
211         assertEquals('3', rawdata[5]);
212         assertEquals('4', rawdata[6]);
213         assertEquals('\r', rawdata[7]);
214         assertEquals('\n', rawdata[8]);
215         assertEquals('0', rawdata[9]);
216         assertEquals('\r', rawdata[10]);
217         assertEquals('\n', rawdata[11]);
218         assertEquals('\r', rawdata[12]);
219         assertEquals('\n', rawdata[13]);
220     }
221 
222     public void testChunkedOutputStreamSmallChunk() throws IOException {
223         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
224         ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
225         out.write('1');  
226         out.finish();
227         out.close();
228         
229         byte [] rawdata =  buffer.toByteArray();
230         
231         assertEquals(11, rawdata.length);
232         assertEquals('1', rawdata[0]);
233         assertEquals('\r', rawdata[1]);
234         assertEquals('\n', rawdata[2]);
235         assertEquals('1', rawdata[3]);
236         assertEquals('\r', rawdata[4]);
237         assertEquals('\n', rawdata[5]);
238         assertEquals('0', rawdata[6]);
239         assertEquals('\r', rawdata[7]);
240         assertEquals('\n', rawdata[8]);
241         assertEquals('\r', rawdata[9]);
242         assertEquals('\n', rawdata[10]);
243     }
244 
245     // ------------------------------------------------------- TestCase Methods
246 
247     public static Test suite() {
248         return new TestSuite(TestStreams.class);
249     }
250 
251     // ------------------------------------------------------------------- Main
252     public static void main(String args[]) {
253         String[] testCaseName = { TestStreams.class.getName() };
254         junit.textui.TestRunner.main(testCaseName);
255     }
256 }
257