1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestGetMethodLocal.java,v 1.14 2004/02/22 18:08:49 olegk Exp $
3    * $Revision: 1.14 $
4    * $Date: 2004/02/22 18:08:49 $
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,v 1.14 2004/02/22 18:08:49 olegk Exp $
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          GetMethod getSlash = new GetMethod("/");
88          for(int i=0;i<10;i++) {
89              assertEquals(200, client.executeMethod(getSlash));
90              String data = getSlash.getResponseBodyAsString();
91              assertTrue(null != data);
92              assertTrue(data.length() > 0);
93              getSlash.recycle();
94              getSlash.setPath("/");
95          }
96      }
97  
98      public void testRecycle() {
99          HttpClient client = createHttpClient(null);
100 
101         GetMethod method = new GetMethod("/");
102         
103         try {
104             client.executeMethod(method);
105         } catch (Throwable t) {
106             t.printStackTrace();
107             fail("Unable to execute method : " + t.toString());
108         }
109 
110         try {
111             String data = method.getResponseBodyAsString();
112             assertTrue("No data returned.",(data.length() > 0));
113         } catch (Throwable t) {
114             t.printStackTrace();
115             fail("Unable to execute method : " + t.toString());
116         }
117         assertEquals(200,method.getStatusCode());
118 
119         method.recycle();
120         method.setPath("/");
121 
122         try {
123             client.executeMethod(method);
124         } catch (Throwable t) {
125             t.printStackTrace();
126             fail("Unable to execute method : " + t.toString());
127         }
128 
129         try {
130             String data = method.getResponseBodyAsString();
131             assertTrue("No data returned.",(data.length() > 0));
132         } catch (Throwable t) {
133             t.printStackTrace();
134             fail("Unable to execute method : " + t.toString());
135         }
136         assertEquals(200,method.getStatusCode());
137 
138     }
139 
140     public void test404() {
141         HttpClient client = createHttpClient(null);
142 
143         GetMethod method = new GetMethod("/i/am/assuming/this/path/and/file/doesnt/exist/on/the/web/server.xyzzy");
144         
145         try {
146             client.executeMethod(method);
147         } catch (Throwable t) {
148             t.printStackTrace();
149             fail("Unable to execute method : " + t.toString());
150         }
151         assertEquals(404,method.getStatusCode());
152 
153     }
154 
155     /***
156      * The intent of this test is to allow for the incomplete parsing of a GET
157      * response, and to make it particularly tricky, the GET response issues
158      * a Connection: close".
159      *
160      * <p>This wants to insure that a recoverable exception is not unexpectedly
161      * triggered.</p>
162      */
163     public void testGetResponseNotReadAutoRecover() {
164         HttpClient client = createHttpClient(null);
165 
166         try {
167             // issue a GET with a connection: close, and don't parse the body.
168             String path = "/";
169             GetMethod method1 = new GetMethod(path);
170             method1.addRequestHeader("Connection", "close");
171             client.executeMethod(method1);
172 
173             // issue another GET.
174             GetMethod method2 = new GetMethod(path);
175             client.executeMethod(method2);
176         }
177         catch (Exception ioe) {
178 
179             fail("Problem executing method : " + ioe.toString() );
180         }
181     }
182 
183 }