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 155418 2005-02-26 13:01:52Z dirkv $
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 = 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
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
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 }