1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsExternalHost.java,v 1.9 2003/02/07 04:50:03 jsdever Exp $
3 * $Revision: 1.9 $
4 * $Date: 2003/02/07 04:50:03 $
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.io.IOException;
66 import java.util.Enumeration;
67 import junit.framework.*;
68 import org.apache.commons.httpclient.methods.*;
69
70 /***
71 * Simple tests for the HTTP client hitting an external webserver.
72 *
73 * This test suite assumes you have an internet connection that
74 * can communicate with http://java.sun.com/.
75 *
76 * @author Remy Maucherat
77 * @author Rodney Waldhoff
78 * @author Ortwin Gl�ck
79 * @author Jeff Dever
80 * @version $Id: TestMethodsExternalHost.java,v 1.9 2003/02/07 04:50:03 jsdever Exp $
81 */
82 public class TestMethodsExternalHost extends TestCase {
83
84 private HttpClient client;
85 private HttpMethod method;
86
87 // -------------------------------------------------------------- Constants
88
89 private static final String externalHost = "java.sun.com";
90 private static final int externalPort = 80;
91 private static final String externalPath = "/index.html";
92 private static final String externalUri = "http://java.sun.com/index.html";
93 private final String PROXY_HOST = System.getProperty("httpclient.test.proxyHost");
94 private final String PROXY_PORT = System.getProperty("httpclient.test.proxyPort");
95 private final String PROXY_USER = System.getProperty("httpclient.test.proxyUser");
96 private final String PROXY_PASS = System.getProperty("httpclient.test.proxyPass");
97
98 // ------------------------------------------------------------ Constructor
99
100
101 public TestMethodsExternalHost(String testName) {
102 super(testName);
103 }
104
105
106 // ------------------------------------------------------- TestCase Methods
107
108
109 public static Test suite() {
110 return new TestSuite(TestMethodsExternalHost.class);
111 }
112
113 // ------------------------------------------------------- Helper Methods
114
115 public void setUp() {
116 client = new HttpClient();
117
118 client.getHostConfiguration().setHost(externalHost, externalPort, "http");
119
120 if (PROXY_HOST != null) {
121 if (PROXY_USER != null) {
122 HttpState state = client.getState();
123 state.setProxyCredentials(null, new UsernamePasswordCredentials(
124 PROXY_USER, PROXY_PASS));
125 }
126 client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
127 }
128 }
129
130 public void tearDown() {
131 method.releaseConnection();
132 method = null;
133 client = null;
134 }
135
136 public void executeMethod() {
137 try {
138 client.executeMethod(method);
139 } catch (Throwable t) {
140 t.printStackTrace();
141 fail("Unable to execute method : " + t.toString());
142 }
143 }
144
145 // ----------------------------------------------------------- OPTIONS Test
146
147
148 public void testMethodsOptionsExternal() {
149
150 method = new OptionsMethod(externalPath);
151 executeMethod();
152
153 Enumeration methodsAllowed = ((OptionsMethod)method).getAllowedMethods();
154 // This enumeration musn't be empty
155 assertTrue("No HTTP method allowed : result of OPTIONS is incorrect.",
156 methodsAllowed.hasMoreElements());
157
158 }
159 // --------------------------------------------------------------- GET Test
160
161
162 public void testMethodsGetExternal() {
163
164 method = new GetMethod(externalUri);
165 executeMethod();
166
167 try {
168 String data = method.getResponseBodyAsString();
169 // This enumeration musn't be empty
170 assertTrue("No data returned.",
171 (data.length() > 0));
172 } catch (Throwable t) {
173 t.printStackTrace();
174 fail("Unable to execute method : " + t.toString());
175 }
176
177 method.recycle();
178 method.setPath(externalPath);
179 executeMethod();
180
181 try {
182 String data = method.getResponseBodyAsString();
183 // This enumeration musn't be empty
184 assertTrue("No data returned.",
185 (data.length() > 0));
186 } catch (Throwable t) {
187 t.printStackTrace();
188 fail("Unable to execute method : " + t.toString());
189 }
190
191 }
192
193
194 // -------------------------------------------------------------- HEAD Test
195
196 public void testMethodsHeadExternal() {
197
198 method = new HeadMethod(externalPath);
199 executeMethod();
200
201 assertTrue("Method failed : " + method.getStatusCode(),
202 (method.getStatusCode() == 200));
203
204 }
205
206 /***
207 * This test proves that bad urls throw an IOException,
208 * and not some other throwable like a NullPointerException.
209 *
210 * FIXME: Bad urls don't throw an IOException.
211 */
212 public void testIOException() {
213
214 method = new GetMethod("http://www.bogusurl.xyz");
215
216 try {
217 client.executeMethod(method);
218 if ((PROXY_HOST != null) && (method.getStatusCode() >= 400)) return;
219 } catch (IOException e) {
220 return; // IOException and HttpException are ok
221 }
222
223 fail("Should have thrown an exception");
224
225 }
226
227
228 /***
229 * http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16864
230 */
231 public void testDomino_Go_Webserver404() throws Exception {
232
233 // this file should not exist
234 method = new GetMethod("http://www.pc.ibm.com/us/accessories/monitors/p_allmodelos.html");
235 int statusCode = client.executeMethod(method);
236
237 assertEquals(404, method.getStatusCode());
238
239 }
240
241
242 }
This page was automatically generated by Maven