1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestGetMethodLocal.java,v 1.15 2004/06/13 20:22:19 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-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 junit.framework.Test;
34  import junit.framework.TestSuite;
35  
36  import org.apache.commons.httpclient.methods.GetMethod;
37  
38  /***
39   * Simple tests of {@link GetMethod}.
40   *
41   * @author Rodney Waldhoff
42   * @version $Id: TestGetMethodLocal.java 155418 2005-02-26 13:01:52Z dirkv $
43   */
44  public class TestGetMethodLocal extends TestLocalHostBase {
45  
46      // ------------------------------------------------------------ Constructor
47  
48      public TestGetMethodLocal(String testName) {
49          super(testName);
50      }
51  
52  
53      // ------------------------------------------------------- TestCase Methods
54  
55      public static Test suite() {
56          return new TestSuite(TestGetMethodLocal.class);
57      }
58  
59      // ------------------------------------------------------------------ Tests
60  
61      public void testGetSlash() {
62          HttpClient client = createHttpClient();
63  
64          GetMethod method = new GetMethod("/");
65          
66          try {
67              client.executeMethod(method);
68          } catch (Throwable t) {
69              t.printStackTrace();
70              fail("Unable to execute method : " + t.toString());
71          }
72  
73          try {
74              String data = method.getResponseBodyAsString();
75              assertTrue("No data returned.",(data.length() > 0));
76          } catch (Throwable t) {
77              t.printStackTrace();
78              fail("Unable to execute method : " + t.toString());
79          }
80          assertEquals(200,method.getStatusCode());
81      }
82  
83      public void testExecuteMultipleMethods() throws Exception {
84  
85          HttpClient client = createHttpClient();
86  
87          for(int i=0;i<10;i++) {
88              GetMethod getSlash = new GetMethod("/");
89              assertEquals(200, client.executeMethod(getSlash));
90              String data = getSlash.getResponseBodyAsString();
91              assertTrue(null != data);
92              assertTrue(data.length() > 0);
93          }
94      }
95  
96      public void test404() {
97          HttpClient client = createHttpClient(null);
98  
99          GetMethod method = new GetMethod("/i/am/assuming/this/path/and/file/doesnt/exist/on/the/web/server.xyzzy");
100         
101         try {
102             client.executeMethod(method);
103         } catch (Throwable t) {
104             t.printStackTrace();
105             fail("Unable to execute method : " + t.toString());
106         }
107         assertEquals(404,method.getStatusCode());
108 
109     }
110 
111     /***
112      * The intent of this test is to allow for the incomplete parsing of a GET
113      * response, and to make it particularly tricky, the GET response issues
114      * a Connection: close".
115      *
116      * <p>This wants to insure that a recoverable exception is not unexpectedly
117      * triggered.</p>
118      */
119     public void testGetResponseNotReadAutoRecover() {
120         HttpClient client = createHttpClient(null);
121 
122         try {
123             // issue a GET with a connection: close, and don't parse the body.
124             String path = "/";
125             GetMethod method1 = new GetMethod(path);
126             method1.addRequestHeader("Connection", "close");
127             client.executeMethod(method1);
128 
129             // issue another GET.
130             GetMethod method2 = new GetMethod(path);
131             client.executeMethod(method2);
132         }
133         catch (Exception ioe) {
134 
135             fail("Problem executing method : " + ioe.toString() );
136         }
137     }
138 
139 }