1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
60
61 public TestMethodsLocalHost(String testName) {
62 super(testName);
63 }
64
65
66
67
68
69 public static Test suite() {
70 return new TestSuite(TestMethodsLocalHost.class);
71 }
72
73
74
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
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
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
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
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
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 }