1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestMethodsLocalHost.java,v 1.14 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 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 155418 2005-02-26 13:01:52Z dirkv $
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 = new GetMethod("/index.html");
132 
133         try {
134             client.executeMethod(method);
135         } catch (Throwable t) {
136             t.printStackTrace();
137             fail("Unable to execute method : " + t.toString());
138         }
139 
140         try {
141             String data = method.getResponseBodyAsString();
142             // This enumeration musn't be empty
143             assertTrue("No data returned.",
144                    (data.length() > 0));
145         } catch (Throwable t) {
146             t.printStackTrace();
147             fail("Unable to execute method : " + t.toString());
148         }
149 
150     }
151 
152 
153     // -------------------------------------------------------------- HEAD Test
154 
155 
156     public void testMethodsHead() {
157 
158         HttpClient client = createHttpClient();
159 
160         OptionsMethod opmethod = new OptionsMethod("/");
161 
162         try {
163             client.executeMethod(opmethod);
164         } catch (Throwable t) {
165             t.printStackTrace();
166             fail("Unable to execute method : " + t.toString());
167         }
168 
169         String path = "/";
170         HeadMethod method = new HeadMethod(path);
171 
172         try {
173             client.executeMethod(method);
174         } catch (Throwable t) {
175             t.printStackTrace();
176             fail("Unable to execute method : " + t.toString());
177         }
178 
179         assertEquals(200, method.getStatusCode());
180 
181         method = new HeadMethod(path);
182 
183         try {
184             client.executeMethod(method);
185         } catch (Throwable t) {
186             t.printStackTrace();
187             fail("Unable to execute method : " + t.toString());
188         }
189 
190         assertEquals(200, method.getStatusCode());
191 
192     }
193 
194 }