1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestGetMethodLocal.java,v 1.10 2003/03/05 04:02:56 mbecke Exp $ 3 * $Revision: 1.10 $ 4 * $Date: 2003/03/05 04:02:56 $ 5 * ==================================================================== 6 * 7 * The Apache Software License, Version 1.1 8 * 9 * Copyright (c) 1999-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.IOException; 66 67 import junit.framework.Test; 68 import junit.framework.TestSuite; 69 70 import org.apache.commons.httpclient.methods.GetMethod; 71 72 /*** 73 * Simple tests of {@link GetMethod}. 74 * 75 * @author Rodney Waldhoff 76 * @version $Id: TestGetMethodLocal.java,v 1.10 2003/03/05 04:02:56 mbecke Exp $ 77 */ 78 public class TestGetMethodLocal extends TestLocalHostBase { 79 80 // ------------------------------------------------------------ Constructor 81 82 public TestGetMethodLocal(String testName) { 83 super(testName); 84 } 85 86 87 // ------------------------------------------------------- TestCase Methods 88 89 public static Test suite() { 90 return new TestSuite(TestGetMethodLocal.class); 91 } 92 93 // ------------------------------------------------------------------ Tests 94 95 public void testGetSlash() { 96 HttpClient client = createHttpClient(); 97 98 GetMethod method = new GetMethod("/"); 99 100 try { 101 client.executeMethod(method); 102 } catch (Throwable t) { 103 t.printStackTrace(); 104 fail("Unable to execute method : " + t.toString()); 105 } 106 107 try { 108 String data = method.getResponseBodyAsString(); 109 assertTrue("No data returned.",(data.length() > 0)); 110 } catch (Throwable t) { 111 t.printStackTrace(); 112 fail("Unable to execute method : " + t.toString()); 113 } 114 assertEquals(200,method.getStatusCode()); 115 } 116 117 public void testExecuteMultipleMethods() throws Exception { 118 119 HttpClient client = createHttpClient(); 120 121 GetMethod getSlash = new GetMethod("/"); 122 for(int i=0;i<10;i++) { 123 assertEquals(200, client.executeMethod(getSlash)); 124 String data = getSlash.getResponseBodyAsString(); 125 assertTrue(null != data); 126 assertTrue(data.length() > 0); 127 getSlash.recycle(); 128 getSlash.setPath("/"); 129 } 130 } 131 132 public void testRecycle() { 133 HttpClient client = createHttpClient(null); 134 135 GetMethod method = new GetMethod("/"); 136 137 try { 138 client.executeMethod(method); 139 } catch (Throwable t) { 140 t.printStackTrace(); 141 fail("Unable to execute method : " + t.toString()); 142 } 143 144 try { 145 String data = method.getResponseBodyAsString(); 146 assertTrue("No data returned.",(data.length() > 0)); 147 } catch (Throwable t) { 148 t.printStackTrace(); 149 fail("Unable to execute method : " + t.toString()); 150 } 151 assertEquals(200,method.getStatusCode()); 152 153 method.recycle(); 154 method.setPath("/"); 155 156 try { 157 client.executeMethod(method); 158 } catch (Throwable t) { 159 t.printStackTrace(); 160 fail("Unable to execute method : " + t.toString()); 161 } 162 163 try { 164 String data = method.getResponseBodyAsString(); 165 assertTrue("No data returned.",(data.length() > 0)); 166 } catch (Throwable t) { 167 t.printStackTrace(); 168 fail("Unable to execute method : " + t.toString()); 169 } 170 assertEquals(200,method.getStatusCode()); 171 172 } 173 174 public void test404() { 175 HttpClient client = createHttpClient(null); 176 177 GetMethod method = new GetMethod("/i/am/assuming/this/path/and/file/doesnt/exist/on/the/web/server.xyzzy"); 178 179 try { 180 client.executeMethod(method); 181 } catch (Throwable t) { 182 t.printStackTrace(); 183 fail("Unable to execute method : " + t.toString()); 184 } 185 assertEquals(404,method.getStatusCode()); 186 187 } 188 189 /*** 190 * The intent of this test is to allow for the incomplete parsing of a GET 191 * response, and to make it particularly tricky, the GET response issues 192 * a Connection: close". 193 * 194 * <p>This wants to insure that a recoverable exception is not unexpectedly 195 * triggered.</p> 196 */ 197 public void testGetResponseNotReadAutoRecover() { 198 HttpClient client = createHttpClient(null); 199 200 try { 201 // issue a GET with a connection: close, and don't parse the body. 202 String path = "/"; 203 GetMethod method1 = new GetMethod(path); 204 method1.addRequestHeader("Connection", "close"); 205 client.executeMethod(method1); 206 assertEquals(0, method1.getRecoverableExceptionCount() ); 207 208 // issue another GET. 209 GetMethod method2 = new GetMethod(path); 210 client.executeMethod(method2); 211 assertEquals(0, method2.getRecoverableExceptionCount() ); 212 } 213 catch (IOException ioe) { 214 215 fail("Problem executing method : " + ioe.toString() ); 216 } 217 } 218 219 }

This page was automatically generated by Maven