001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestStreams.java,v 1.19 2004/10/31 14:04:13 olegk Exp $
003: * $Revision: 505890 $
004: * $Date: 2007-02-11 12:25:25 +0100 (Sun, 11 Feb 2007) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: package org.apache.commons.httpclient;
033:
034: import java.io.ByteArrayInputStream;
035: import java.io.ByteArrayOutputStream;
036: import java.io.IOException;
037: import java.io.InputStream;
038: import java.io.OutputStream;
039:
040: import junit.framework.Test;
041: import junit.framework.TestCase;
042: import junit.framework.TestSuite;
043:
044: import org.apache.commons.httpclient.methods.GetMethod;
045: import org.apache.commons.httpclient.util.EncodingUtil;
046:
047: public class TestStreams extends TestCase {
048:
049: private static final String CONTENT_CHARSET = "ISO-8859-1";
050:
051: public TestStreams(String testName) {
052: super (testName);
053: }
054:
055: public void testChunkedInputStream() throws IOException {
056: String correctInput = "10;key=\"value\r\nnewline\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
057: String correctResult = "123456789012345612345";
058: HttpMethod method = new FakeHttpMethod();
059:
060: //Test for when buffer is larger than chunk size
061: InputStream in = new ChunkedInputStream(
062: new ByteArrayInputStream(EncodingUtil.getBytes(
063: correctInput, CONTENT_CHARSET)), method);
064: byte[] buffer = new byte[300];
065: ByteArrayOutputStream out = new ByteArrayOutputStream();
066: int len;
067: while ((len = in.read(buffer)) > 0) {
068: out.write(buffer, 0, len);
069: }
070: String result = EncodingUtil.getString(out.toByteArray(),
071: CONTENT_CHARSET);
072: assertEquals(result, correctResult);
073: Header footer = method.getResponseFooter("footer1");
074: assertEquals(footer.getValue(), "abcde");
075: footer = method.getResponseFooter("footer2");
076: assertEquals(footer.getValue(), "fghij");
077:
078: method = new FakeHttpMethod();
079:
080: //Test for when buffer is smaller than chunk size.
081: in = new ChunkedInputStream(new ByteArrayInputStream(
082: EncodingUtil.getBytes(correctInput, CONTENT_CHARSET)),
083: method);
084: buffer = new byte[7];
085: out = new ByteArrayOutputStream();
086: while ((len = in.read(buffer)) > 0) {
087: out.write(buffer, 0, len);
088: }
089: result = EncodingUtil.getString(out.toByteArray(),
090: CONTENT_CHARSET);
091: assertEquals(result, correctResult);
092: footer = method.getResponseFooter("footer1");
093: assertEquals(footer.getValue(), "abcde");
094: footer = method.getResponseFooter("footer2");
095: assertEquals(footer.getValue(), "fghij");
096: }
097:
098: public void testCorruptChunkedInputStream1() throws IOException {
099: //missing \r\n at the end of the first chunk
100: String corrupInput = "10;key=\"value\"\r\n123456789012345\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
101: HttpMethod method = new FakeHttpMethod();
102:
103: InputStream in = new ChunkedInputStream(
104: new ByteArrayInputStream(EncodingUtil.getBytes(
105: corrupInput, CONTENT_CHARSET)), method);
106: byte[] buffer = new byte[300];
107: ByteArrayOutputStream out = new ByteArrayOutputStream();
108: int len;
109: try {
110: while ((len = in.read(buffer)) > 0) {
111: out.write(buffer, 0, len);
112: }
113: fail("Should have thrown exception");
114: } catch (IOException e) {
115: /* expected exception */
116: }
117: }
118:
119: public void testEmptyChunkedInputStream() throws IOException {
120: String input = "0\r\n";
121: HttpMethod method = new FakeHttpMethod();
122:
123: InputStream in = new ChunkedInputStream(
124: new ByteArrayInputStream(EncodingUtil.getBytes(input,
125: CONTENT_CHARSET)), method);
126: byte[] buffer = new byte[300];
127: ByteArrayOutputStream out = new ByteArrayOutputStream();
128: int len;
129: while ((len = in.read(buffer)) > 0) {
130: out.write(buffer, 0, len);
131: }
132: assertEquals(0, out.size());
133: }
134:
135: public void testContentLengthInputStream() throws IOException {
136: String correct = "1234567890123456";
137: InputStream in = new ContentLengthInputStream(
138: new ByteArrayInputStream(EncodingUtil.getBytes(correct,
139: CONTENT_CHARSET)), 10L);
140: byte[] buffer = new byte[50];
141: int len = in.read(buffer);
142: ByteArrayOutputStream out = new ByteArrayOutputStream();
143: out.write(buffer, 0, len);
144: String result = EncodingUtil.getString(out.toByteArray(),
145: CONTENT_CHARSET);
146: assertEquals(result, "1234567890");
147: }
148:
149: public void testContentLengthInputStreamSkip() throws IOException {
150: InputStream in = new ContentLengthInputStream(
151: new ByteArrayInputStream(new byte[20]), 10L);
152: assertEquals(10, in.skip(10));
153: assertTrue(in.read() == -1);
154:
155: in = new ContentLengthInputStream(new ByteArrayInputStream(
156: new byte[20]), 10L);
157: in.read();
158: assertEquals(9, in.skip(10));
159: assertTrue(in.read() == -1);
160:
161: in = new ContentLengthInputStream(new ByteArrayInputStream(
162: new byte[20]), 2L);
163: in.read();
164: in.read();
165: assertTrue(in.skip(10) <= 0);
166: assertTrue(in.read() == -1);
167: }
168:
169: public void testChunkedConsitance() throws IOException {
170: String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
171: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
172: OutputStream out = new ChunkedOutputStream(buffer);
173: out.write(EncodingUtil.getBytes(input, CONTENT_CHARSET));
174: out.close();
175: buffer.close();
176: InputStream in = new ChunkedInputStream(
177: new ByteArrayInputStream(buffer.toByteArray()),
178: new GetMethod());
179:
180: byte[] d = new byte[10];
181: ByteArrayOutputStream result = new ByteArrayOutputStream();
182: int len = 0;
183: while ((len = in.read(d)) > 0) {
184: result.write(d, 0, len);
185: }
186:
187: String output = EncodingUtil.getString(result.toByteArray(),
188: CONTENT_CHARSET);
189: assertEquals(input, output);
190: }
191:
192: public void testChunkedOutputStream() throws IOException {
193: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
194: ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
195: out.write('1');
196: out.write('2');
197: out.write('3');
198: out.write('4');
199: out.finish();
200: out.close();
201:
202: byte[] rawdata = buffer.toByteArray();
203:
204: assertEquals(19, rawdata.length);
205: assertEquals('2', rawdata[0]);
206: assertEquals('\r', rawdata[1]);
207: assertEquals('\n', rawdata[2]);
208: assertEquals('1', rawdata[3]);
209: assertEquals('2', rawdata[4]);
210: assertEquals('\r', rawdata[5]);
211: assertEquals('\n', rawdata[6]);
212: assertEquals('2', rawdata[7]);
213: assertEquals('\r', rawdata[8]);
214: assertEquals('\n', rawdata[9]);
215: assertEquals('3', rawdata[10]);
216: assertEquals('4', rawdata[11]);
217: assertEquals('\r', rawdata[12]);
218: assertEquals('\n', rawdata[13]);
219: assertEquals('0', rawdata[14]);
220: assertEquals('\r', rawdata[15]);
221: assertEquals('\n', rawdata[16]);
222: assertEquals('\r', rawdata[17]);
223: assertEquals('\n', rawdata[18]);
224: }
225:
226: public void testChunkedOutputStreamLargeChunk() throws IOException {
227: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
228: ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
229: out.write(new byte[] { '1', '2', '3', '4' });
230: out.finish();
231: out.close();
232:
233: byte[] rawdata = buffer.toByteArray();
234:
235: assertEquals(14, rawdata.length);
236: assertEquals('4', rawdata[0]);
237: assertEquals('\r', rawdata[1]);
238: assertEquals('\n', rawdata[2]);
239: assertEquals('1', rawdata[3]);
240: assertEquals('2', rawdata[4]);
241: assertEquals('3', rawdata[5]);
242: assertEquals('4', rawdata[6]);
243: assertEquals('\r', rawdata[7]);
244: assertEquals('\n', rawdata[8]);
245: assertEquals('0', rawdata[9]);
246: assertEquals('\r', rawdata[10]);
247: assertEquals('\n', rawdata[11]);
248: assertEquals('\r', rawdata[12]);
249: assertEquals('\n', rawdata[13]);
250: }
251:
252: public void testChunkedOutputStreamSmallChunk() throws IOException {
253: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
254: ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
255: out.write('1');
256: out.finish();
257: out.close();
258:
259: byte[] rawdata = buffer.toByteArray();
260:
261: assertEquals(11, rawdata.length);
262: assertEquals('1', rawdata[0]);
263: assertEquals('\r', rawdata[1]);
264: assertEquals('\n', rawdata[2]);
265: assertEquals('1', rawdata[3]);
266: assertEquals('\r', rawdata[4]);
267: assertEquals('\n', rawdata[5]);
268: assertEquals('0', rawdata[6]);
269: assertEquals('\r', rawdata[7]);
270: assertEquals('\n', rawdata[8]);
271: assertEquals('\r', rawdata[9]);
272: assertEquals('\n', rawdata[10]);
273: }
274:
275: // ------------------------------------------------------- TestCase Methods
276:
277: public static Test suite() {
278: return new TestSuite(TestStreams.class);
279: }
280:
281: // ------------------------------------------------------------------- Main
282: public static void main(String args[]) {
283: String[] testCaseName = { TestStreams.class.getName() };
284: junit.textui.TestRunner.main(testCaseName);
285: }
286:
287: public void testAutoCloseInputStream() throws IOException {
288: // The purpose of this test is to check EOF handling of ACIS with
289: // respect to exceptions being thrown. Putting it on top of a
290: // plain ByteArrayInputStream won't do, since BAIS can't be closed.
291: ByteArrayInputStream bais = new ByteArrayInputStream("whatever"
292: .getBytes());
293: InputStream fbais = new java.io.FilterInputStream(bais) {
294: private boolean closed = false;
295:
296: public void close() throws IOException {
297: closed = true;
298: super .close();
299: }
300:
301: public int available() throws IOException {
302: if (closed)
303: throw new IOException("closed");
304: return super .available();
305: }
306: };
307:
308: AutoCloseInputStream acis = new AutoCloseInputStream(fbais,
309: null);
310: byte[] data = new byte[16];
311: int count = 0;
312: while (count >= 0) {
313: count = acis.read(data);
314: }
315: // We're at EOF. The underlying stream should be closed,
316: // but the ACIS itself not.
317: try {
318: fbais.available();
319: fail("underlying stream not auto-closed");
320: } catch (IOException x) {
321: // expected, pis should be closed
322: }
323:
324: // don't want to see an exception being thrown here
325: acis.available();
326:
327: acis.close();
328: try {
329: acis.available();
330: fail("auto-close stream not closed");
331: } catch (IOException x) {
332: // expected, acis should be closed
333: }
334: }
335: }
|