1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestStreams.java,v 1.11 2003/05/08 17:33:53 olegk Exp $ 3 * $Revision: 1.11 $ 4 * $Date: 2003/05/08 17:33:53 $ 5 * ==================================================================== 6 * 7 * The Apache Software License, Version 1.1 8 * 9 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights 10 * reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * 24 * 3. The end-user documentation included with the redistribution, if 25 * any, must include the following acknowlegement: 26 * "This product includes software developed by the 27 * Apache Software Foundation (http://www.apache.org/)." 28 * Alternately, this acknowlegement may appear in the software itself, 29 * if and wherever such third-party acknowlegements normally appear. 30 * 31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 32 * Foundation" must not be used to endorse or promote products derived 33 * from this software without prior written permission. For written 34 * permission, please contact apache@apache.org. 35 * 36 * 5. Products derived from this software may not be called "Apache" 37 * nor may "Apache" appear in their names without prior written 38 * permission of the Apache Group. 39 * 40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This software consists of voluntary contributions made by many 55 * individuals on behalf of the Apache Software Foundation. For more 56 * information on the Apache Software Foundation, please see 57 * <http://www.apache.org/>. 58 * 59 * [Additional notices, if required by prior licensing conditions] 60 * 61 */ 62 63 package org.apache.commons.httpclient; 64 65 import java.io.ByteArrayInputStream; 66 import java.io.ByteArrayOutputStream; 67 import java.io.IOException; 68 import java.io.InputStream; 69 import java.io.OutputStream; 70 71 import junit.framework.Test; 72 import junit.framework.TestCase; 73 import junit.framework.TestSuite; 74 75 import org.apache.commons.httpclient.methods.GetMethod; 76 77 78 public class TestStreams extends TestCase { 79 80 public TestStreams(String testName) { 81 super(testName); 82 } 83 84 public void testChunkedInputStream() throws IOException { 85 String correctInput = "10;key=\"value\r\nnewline\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n"; 86 String correctResult = "123456789012345612345"; 87 HttpMethod method = new SimpleHttpMethod(); 88 89 //Test for when buffer is larger than chunk size 90 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(correctInput)), method); 91 byte[] buffer = new byte[300]; 92 ByteArrayOutputStream out = new ByteArrayOutputStream(); 93 int len; 94 while ((len = in.read(buffer)) > 0) { 95 out.write(buffer, 0, len); 96 } 97 String result = HttpConstants.getContentString(out.toByteArray()); 98 assertEquals(result, correctResult); 99 Header footer = method.getResponseFooter("footer1"); 100 assertEquals(footer.getValue(), "abcde"); 101 footer = method.getResponseFooter("footer2"); 102 assertEquals(footer.getValue(), "fghij"); 103 104 // recycle the method so that it can be reused below 105 method.recycle(); 106 107 //Test for when buffer is smaller than chunk size. 108 in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(correctInput)), method); 109 buffer = new byte[7]; 110 out = new ByteArrayOutputStream(); 111 while ((len = in.read(buffer)) > 0) { 112 out.write(buffer, 0, len); 113 } 114 result = HttpConstants.getContentString(out.toByteArray()); 115 assertEquals(result, correctResult); 116 footer = method.getResponseFooter("footer1"); 117 assertEquals(footer.getValue(), "abcde"); 118 footer = method.getResponseFooter("footer2"); 119 assertEquals(footer.getValue(), "fghij"); 120 } 121 122 public void testCorruptChunkedInputStream1() throws IOException { 123 //missing \r\n at the end of the first chunk 124 String corrupInput = "10;key=\"value\"\r\n123456789012345\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n"; 125 HttpMethod method = new SimpleHttpMethod(); 126 127 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(corrupInput)), method); 128 byte[] buffer = new byte[300]; 129 ByteArrayOutputStream out = new ByteArrayOutputStream(); 130 int len; 131 try { 132 while ((len = in.read(buffer)) > 0) { 133 out.write(buffer, 0, len); 134 } 135 fail("Should have thrown exception"); 136 } catch(IOException e) { 137 /* expected exception */ 138 } 139 } 140 141 public void testEmptyChunkedInputStream() throws IOException { 142 String input = "0\r\n"; 143 HttpMethod method = new SimpleHttpMethod(); 144 145 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(HttpConstants.getBytes(input)), method); 146 byte[] buffer = new byte[300]; 147 ByteArrayOutputStream out = new ByteArrayOutputStream(); 148 int len; 149 while ((len = in.read(buffer)) > 0) { 150 out.write(buffer, 0, len); 151 } 152 assertEquals(0, out.size()); 153 } 154 155 public void testContentLengthInputStream() throws IOException { 156 String correct = "1234567890123456"; 157 InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(HttpConstants.getBytes(correct)), 10); 158 byte[] buffer = new byte[50]; 159 int len = in.read(buffer); 160 ByteArrayOutputStream out = new ByteArrayOutputStream(); 161 out.write(buffer, 0, len); 162 String result = HttpConstants.getContentString(out.toByteArray()); 163 assertEquals(result, "1234567890"); 164 } 165 166 public void testChunkedConsitance() throws IOException { 167 String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?//suweb"; 168 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 169 OutputStream out = new ChunkedOutputStream(buffer); 170 out.write(HttpConstants.getBytes(input)); 171 out.close(); 172 buffer.close(); 173 InputStream in = new ChunkedInputStream(new ByteArrayInputStream(buffer.toByteArray()), new GetMethod()); 174 175 byte[] d = new byte[10]; 176 ByteArrayOutputStream result = new ByteArrayOutputStream(); 177 int len = 0; 178 while ((len = in.read(d)) > 0) { 179 result.write(d, 0, len); 180 } 181 182 String output = HttpConstants.getContentString(result.toByteArray()); 183 assertEquals(input, output); 184 } 185 186 // ------------------------------------------------------- TestCase Methods 187 188 public static Test suite() { 189 return new TestSuite(TestStreams.class); 190 } 191 192 // ------------------------------------------------------------------- Main 193 public static void main(String args[]) { 194 String[] testCaseName = { TestStreams.class.getName() }; 195 junit.textui.TestRunner.main(testCaseName); 196 } 197 } 198

This page was automatically generated by Maven