1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsLocalHost.java,v 1.12 2003/03/05 04:02:56 mbecke Exp $
3 * $Revision: 1.12 $
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.util.Enumeration;
66
67 import junit.framework.Test;
68 import junit.framework.TestSuite;
69
70 import org.apache.commons.httpclient.methods.GetMethod;
71 import org.apache.commons.httpclient.methods.HeadMethod;
72 import org.apache.commons.httpclient.methods.OptionsMethod;
73
74 /***
75 * Simple tests for the HTTP client hitting a local webserver.
76 *
77 * This test assumes a webserver is running on port 8080 on
78 * the 127.0.0.1.
79 *
80 * The default configuration of Tomcat 4 will work fine.
81 *
82 * Tomcat 3.x will fail the OPTIONS test, because it
83 * treats OPTIONS as a GET request.
84 *
85 * @author Remy Maucherat
86 * @author Rodney Waldhoff
87 * @version $Id: TestMethodsLocalHost.java,v 1.12 2003/03/05 04:02:56 mbecke Exp $
88 */
89 public class TestMethodsLocalHost extends TestLocalHostBase {
90
91 // ------------------------------------------------------------ Constructor
92
93 public TestMethodsLocalHost(String testName) {
94 super(testName);
95 }
96
97
98 // ------------------------------------------------------- TestCase Methods
99
100
101 public static Test suite() {
102 return new TestSuite(TestMethodsLocalHost.class);
103 }
104
105
106 // ----------------------------------------------------------- OPTIONS Test
107
108 /***
109 * This test assumes that the webserver listening
110 * on host/port will respond properly to an OPTIONS
111 * request. Tomcat 4 is one such web server,
112 * but Tomcat 3.x is not.
113 */
114 public void testMethodsOptions() {
115
116 HttpClient client = createHttpClient();
117 OptionsMethod method = new OptionsMethod("/");
118
119 try {
120 client.executeMethod(method);
121 } catch (Throwable t) {
122 t.printStackTrace();
123 fail("Unable to execute method : " + t.toString());
124 }
125
126 Enumeration methodsAllowed = method.getAllowedMethods();
127 // This enumeration musn't be empty
128 assertTrue("No HTTP method allowed : result of OPTIONS is incorrect "
129 + "(make sure the webserver running on port " + getPort()
130 + " supports OPTIONS properly)",
131 methodsAllowed.hasMoreElements());
132
133 }
134
135
136 // --------------------------------------------------------------- GET Test
137
138
139 public void testMethodsGet() {
140
141 HttpClient client = createHttpClient();
142
143 GetMethod method = new GetMethod("/");
144
145
146 try {
147 client.executeMethod(method);
148 } catch (Throwable t) {
149 t.printStackTrace();
150 fail("Unable to execute method : " + t.toString());
151 }
152
153 try {
154 String data = method.getResponseBodyAsString();
155 // This enumeration musn't be empty
156 assertTrue("No data returned.",
157 (data.length() > 0));
158 } catch (Throwable t) {
159 t.printStackTrace();
160 fail("Unable to execute method : " + t.toString());
161 }
162
163 method.recycle();
164 method.setPath("/index.html");
165
166 try {
167 client.executeMethod(method);
168 } catch (Throwable t) {
169 t.printStackTrace();
170 fail("Unable to execute method : " + t.toString());
171 }
172
173 try {
174 String data = method.getResponseBodyAsString();
175 // This enumeration musn't be empty
176 assertTrue("No data returned.",
177 (data.length() > 0));
178 } catch (Throwable t) {
179 t.printStackTrace();
180 fail("Unable to execute method : " + t.toString());
181 }
182
183 }
184
185
186 // -------------------------------------------------------------- HEAD Test
187
188
189 public void testMethodsHead() {
190
191 HttpClient client = createHttpClient();
192
193 OptionsMethod opmethod = new OptionsMethod("/");
194
195 try {
196 client.executeMethod(opmethod);
197 } catch (Throwable t) {
198 t.printStackTrace();
199 fail("Unable to execute method : " + t.toString());
200 }
201
202 String path = "/";
203 HeadMethod method = new HeadMethod(path);
204
205 try {
206 client.executeMethod(method);
207 } catch (Throwable t) {
208 t.printStackTrace();
209 fail("Unable to execute method : " + t.toString());
210 }
211
212 assertEquals(200, method.getStatusCode());
213
214 method.recycle();
215 method.setPath(path);
216
217 try {
218 client.executeMethod(method);
219 } catch (Throwable t) {
220 t.printStackTrace();
221 fail("Unable to execute method : " + t.toString());
222 }
223
224 assertEquals(200, method.getStatusCode());
225
226 }
227
228 }
This page was automatically generated by Maven