1   /*
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    * ====================================================================
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  
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          //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();
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          // 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(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          //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();
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             /* expected exception */
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     // ------------------------------------------------------- TestCase Methods
172 
173     public static Test suite() {
174         return new TestSuite(TestStreams.class);
175     }
176 
177     // ------------------------------------------------------------------- Main
178     public static void main(String args[]) {
179         String[] testCaseName = { TestStreams.class.getName() };
180         junit.textui.TestRunner.main(testCaseName);
181     }
182 }
183