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.io.IOException;
34 import java.util.Enumeration;
35 import junit.framework.*;
36
37 import org.apache.commons.httpclient.auth.AuthScope;
38 import org.apache.commons.httpclient.methods.*;
39
40 /***
41 * Simple tests for the HTTP client hitting an external webserver.
42 *
43 * This test suite assumes you have an internet connection that
44 * can communicate with http://java.sun.com/.
45 *
46 * @author Remy Maucherat
47 * @author Rodney Waldhoff
48 * @author Ortwin Glück
49 * @author Jeff Dever
50 * @version $Id: TestMethodsExternalHost.java 155418 2005-02-26 13:01:52Z dirkv $
51 */
52 public class TestMethodsExternalHost extends TestCase {
53
54 private HttpClient client;
55 private HttpMethod method;
56
57
58
59 private static final String externalHost = "jakarta.apache.org";
60 private static final int externalPort = 80;
61 private static final String externalPath = "/index.html";
62 private static final String externalUri = "http://"+ externalHost + externalPath;
63 private final String PROXY_HOST = System.getProperty("httpclient.test.proxyHost");
64 private final String PROXY_PORT = System.getProperty("httpclient.test.proxyPort");
65 private final String PROXY_USER = System.getProperty("httpclient.test.proxyUser");
66 private final String PROXY_PASS = System.getProperty("httpclient.test.proxyPass");
67
68
69
70
71 public TestMethodsExternalHost(String testName) {
72 super(testName);
73 }
74
75
76
77
78
79 public static Test suite() {
80 return new TestSuite(TestMethodsExternalHost.class);
81 }
82
83
84
85 public void setUp() {
86 client = new HttpClient();
87
88 client.getHostConfiguration().setHost(externalHost, externalPort, "http");
89
90 if (PROXY_HOST != null) {
91 if (PROXY_USER != null) {
92 HttpState state = client.getState();
93 state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
94 PROXY_USER, PROXY_PASS));
95 }
96 client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
97 }
98 }
99
100 public void tearDown() {
101 method.releaseConnection();
102 method = null;
103 client = null;
104 }
105
106 public void executeMethod() {
107 try {
108 client.executeMethod(method);
109 } catch (Throwable t) {
110 t.printStackTrace();
111 fail("Unable to execute method : " + t.toString());
112 }
113 }
114
115
116
117
118 public void testMethodsOptionsExternal() {
119
120 method = new OptionsMethod(externalPath);
121 executeMethod();
122
123 Enumeration methodsAllowed = ((OptionsMethod)method).getAllowedMethods();
124
125 assertTrue("No HTTP method allowed : result of OPTIONS is incorrect.",
126 methodsAllowed.hasMoreElements());
127
128 }
129
130
131
132 public void testMethodsGetExternal() {
133
134 method = new GetMethod(externalUri);
135 executeMethod();
136
137 try {
138 String data = method.getResponseBodyAsString();
139
140 assertTrue("No data returned.",
141 (data.length() > 0));
142 } catch (Throwable t) {
143 t.printStackTrace();
144 fail("Unable to execute method : " + t.toString());
145 }
146
147 method = new GetMethod(externalUri);
148 executeMethod();
149
150 try {
151 String data = method.getResponseBodyAsString();
152
153 assertTrue("No data returned.",
154 (data.length() > 0));
155 } catch (Throwable t) {
156 t.printStackTrace();
157 fail("Unable to execute method : " + t.toString());
158 }
159
160 }
161
162
163
164
165 public void testMethodsHeadExternal() {
166
167 method = new HeadMethod(externalPath);
168 executeMethod();
169
170 assertTrue("Method failed : " + method.getStatusCode(),
171 (method.getStatusCode() == 200));
172
173 }
174
175 /***
176 * This test proves that bad urls throw an IOException,
177 * and not some other throwable like a NullPointerException.
178 *
179 * FIXME: Bad urls don't throw an IOException.
180 */
181 public void testIOException() {
182
183 method = new GetMethod("http://www.bogusurl.xyz");
184
185 try {
186 client.executeMethod(method);
187 if ((PROXY_HOST != null) && (method.getStatusCode() >= 400)) return;
188 } catch (IOException e) {
189 return;
190 }
191 fail("Should have thrown an exception");
192
193 }
194
195
196 /***
197 * see issue #16864
198 */
199 public void testDomino_Go_Webserver404() throws Exception {
200
201
202 method = new GetMethod("http://www.pc.ibm.com/us/accessories/monitors/p_allmodelos.html");
203 client.executeMethod(method);
204
205 assertEquals(404, method.getStatusCode());
206
207 }
208
209
210 }