1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsLocalHost.java,v 1.12.2.1 2004/02/22 18:21:16 olegk Exp $
3    * $Revision: 1.12.2.1 $
4    * $Date: 2004/02/22 18:21:16 $
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 java.util.Enumeration;
34  
35  import junit.framework.Test;
36  import junit.framework.TestSuite;
37  
38  import org.apache.commons.httpclient.methods.GetMethod;
39  import org.apache.commons.httpclient.methods.HeadMethod;
40  import org.apache.commons.httpclient.methods.OptionsMethod;
41  
42  /***
43   * Simple tests for the HTTP client hitting a local webserver.
44   *
45   * This test assumes a webserver is running on port 8080 on
46   * the 127.0.0.1.
47   *
48   * The default configuration of Tomcat 4 will work fine.
49   *
50   * Tomcat 3.x will fail the OPTIONS test, because it
51   * treats OPTIONS as a GET request.
52   *
53   * @author Remy Maucherat
54   * @author Rodney Waldhoff
55   * @version $Id: TestMethodsLocalHost.java,v 1.12.2.1 2004/02/22 18:21:16 olegk Exp $
56   */
57  public class TestMethodsLocalHost extends TestLocalHostBase {
58  
59      // ------------------------------------------------------------ Constructor
60  
61      public TestMethodsLocalHost(String testName) {
62          super(testName);
63      }
64  
65  
66      // ------------------------------------------------------- TestCase Methods
67  
68  
69      public static Test suite() {
70          return new TestSuite(TestMethodsLocalHost.class);
71      }
72  
73  
74      // ----------------------------------------------------------- OPTIONS Test
75  
76      /***
77       * This test assumes that the webserver listening
78       * on host/port will respond properly to an OPTIONS
79       * request.  Tomcat 4 is one such web server,
80       * but Tomcat 3.x is not.
81       */
82      public void testMethodsOptions() {
83  
84          HttpClient client = createHttpClient();
85          OptionsMethod method = new OptionsMethod("/");
86  
87          try {
88              client.executeMethod(method);
89          } catch (Throwable t) {
90              t.printStackTrace();
91              fail("Unable to execute method : " + t.toString());
92          }
93  
94          Enumeration methodsAllowed = method.getAllowedMethods();
95          // This enumeration musn't be empty
96          assertTrue("No HTTP method allowed : result of OPTIONS is incorrect "
97                 + "(make sure the webserver running on port " + getPort()
98                 + " supports OPTIONS properly)",
99                 methodsAllowed.hasMoreElements());
100 
101     }
102 
103 
104     // --------------------------------------------------------------- GET Test
105 
106 
107     public void testMethodsGet() {
108 
109         HttpClient client = createHttpClient();
110 
111         GetMethod method = new GetMethod("/");
112         
113 
114         try {
115             client.executeMethod(method);
116         } catch (Throwable t) {
117             t.printStackTrace();
118             fail("Unable to execute method : " + t.toString());
119         }
120 
121         try {
122             String data = method.getResponseBodyAsString();
123             // This enumeration musn't be empty
124             assertTrue("No data returned.",
125                    (data.length() > 0));
126         } catch (Throwable t) {
127             t.printStackTrace();
128             fail("Unable to execute method : " + t.toString());
129         }
130 
131         method.recycle();
132         method.setPath("/index.html");
133 
134         try {
135             client.executeMethod(method);
136         } catch (Throwable t) {
137             t.printStackTrace();
138             fail("Unable to execute method : " + t.toString());
139         }
140 
141         try {
142             String data = method.getResponseBodyAsString();
143             // This enumeration musn't be empty
144             assertTrue("No data returned.",
145                    (data.length() > 0));
146         } catch (Throwable t) {
147             t.printStackTrace();
148             fail("Unable to execute method : " + t.toString());
149         }
150 
151     }
152 
153 
154     // -------------------------------------------------------------- HEAD Test
155 
156 
157     public void testMethodsHead() {
158 
159         HttpClient client = createHttpClient();
160 
161         OptionsMethod opmethod = new OptionsMethod("/");
162 
163         try {
164             client.executeMethod(opmethod);
165         } catch (Throwable t) {
166             t.printStackTrace();
167             fail("Unable to execute method : " + t.toString());
168         }
169 
170         String path = "/";
171         HeadMethod method = new HeadMethod(path);
172 
173         try {
174             client.executeMethod(method);
175         } catch (Throwable t) {
176             t.printStackTrace();
177             fail("Unable to execute method : " + t.toString());
178         }
179 
180         assertEquals(200, method.getStatusCode());
181 
182         method.recycle();
183         method.setPath(path);
184 
185         try {
186             client.executeMethod(method);
187         } catch (Throwable t) {
188             t.printStackTrace();
189             fail("Unable to execute method : " + t.toString());
190         }
191 
192         assertEquals(200, method.getStatusCode());
193 
194     }
195 
196 }