KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestMethodsExternalHost


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsExternalHost.java,v 1.9.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.9.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
5  * ====================================================================
6  *
7  * Copyright 1999-2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ====================================================================
21  *
22  * This software consists of voluntary contributions made by many
23  * individuals on behalf of the Apache Software Foundation. For more
24  * information on the Apache Software Foundation, please see
25  * <http://www.apache.org/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30
31 package org.apache.commons.httpclient;
32
33 import java.io.IOException JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import junit.framework.*;
36 import org.apache.commons.httpclient.methods.*;
37
38 /**
39  * Simple tests for the HTTP client hitting an external webserver.
40  *
41  * This test suite assumes you have an internet connection that
42  * can communicate with http://java.sun.com/.
43  *
44  * @author Remy Maucherat
45  * @author Rodney Waldhoff
46  * @author Ortwin Glück
47  * @author Jeff Dever
48  * @version $Id: TestMethodsExternalHost.java,v 1.9.2.1 2004/02/22 18:21:16 olegk Exp $
49  */

50 public class TestMethodsExternalHost extends TestCase {
51
52     private HttpClient client;
53     private HttpMethod method;
54
55     // -------------------------------------------------------------- Constants
56

57     private static final String JavaDoc externalHost = "java.sun.com";
58     private static final int externalPort = 80;
59     private static final String JavaDoc externalPath = "/index.html";
60     private static final String JavaDoc externalUri = "http://java.sun.com/index.html";
61     private final String JavaDoc PROXY_HOST = System.getProperty("httpclient.test.proxyHost");
62     private final String JavaDoc PROXY_PORT = System.getProperty("httpclient.test.proxyPort");
63     private final String JavaDoc PROXY_USER = System.getProperty("httpclient.test.proxyUser");
64     private final String JavaDoc PROXY_PASS = System.getProperty("httpclient.test.proxyPass");
65
66     // ------------------------------------------------------------ Constructor
67

68
69     public TestMethodsExternalHost(String JavaDoc testName) {
70         super(testName);
71     }
72
73
74     // ------------------------------------------------------- TestCase Methods
75

76
77     public static Test suite() {
78         return new TestSuite(TestMethodsExternalHost.class);
79     }
80
81     // ------------------------------------------------------- Helper Methods
82

83     public void setUp() {
84         client = new HttpClient();
85
86         client.getHostConfiguration().setHost(externalHost, externalPort, "http");
87
88         if (PROXY_HOST != null) {
89             if (PROXY_USER != null) {
90                 HttpState state = client.getState();
91                 state.setProxyCredentials(null, new UsernamePasswordCredentials(
92                     PROXY_USER, PROXY_PASS));
93             }
94             client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
95         }
96     }
97
98     public void tearDown() {
99         method.releaseConnection();
100         method = null;
101         client = null;
102     }
103
104     public void executeMethod() {
105         try {
106             client.executeMethod(method);
107         } catch (Throwable JavaDoc t) {
108             t.printStackTrace();
109             fail("Unable to execute method : " + t.toString());
110         }
111     }
112
113     // ----------------------------------------------------------- OPTIONS Test
114

115
116     public void testMethodsOptionsExternal() {
117
118         method = new OptionsMethod(externalPath);
119         executeMethod();
120
121         Enumeration JavaDoc methodsAllowed = ((OptionsMethod)method).getAllowedMethods();
122         // This enumeration musn't be empty
123
assertTrue("No HTTP method allowed : result of OPTIONS is incorrect.",
124                methodsAllowed.hasMoreElements());
125
126     }
127     // --------------------------------------------------------------- GET Test
128

129
130     public void testMethodsGetExternal() {
131
132         method = new GetMethod(externalUri);
133         executeMethod();
134
135         try {
136             String JavaDoc data = method.getResponseBodyAsString();
137             // This enumeration musn't be empty
138
assertTrue("No data returned.",
139                    (data.length() > 0));
140         } catch (Throwable JavaDoc t) {
141             t.printStackTrace();
142             fail("Unable to execute method : " + t.toString());
143         }
144
145         method.recycle();
146         method.setPath(externalPath);
147         executeMethod();
148
149         try {
150             String JavaDoc data = method.getResponseBodyAsString();
151             // This enumeration musn't be empty
152
assertTrue("No data returned.",
153                    (data.length() > 0));
154         } catch (Throwable JavaDoc t) {
155             t.printStackTrace();
156             fail("Unable to execute method : " + t.toString());
157         }
158
159     }
160
161
162     // -------------------------------------------------------------- HEAD Test
163

164     public void testMethodsHeadExternal() {
165
166         method = new HeadMethod(externalPath);
167         executeMethod();
168
169         assertTrue("Method failed : " + method.getStatusCode(),
170                (method.getStatusCode() == 200));
171
172     }
173
174     /**
175      * This test proves that bad urls throw an IOException,
176      * and not some other throwable like a NullPointerException.
177      *
178      * FIXME: Bad urls don't throw an IOException.
179      */

180     public void testIOException() {
181
182         method = new GetMethod("http://www.bogusurl.xyz");
183
184         try {
185             client.executeMethod(method);
186             if ((PROXY_HOST != null) && (method.getStatusCode() >= 400)) return;
187         } catch (IOException JavaDoc e) {
188             return; // IOException and HttpException are ok
189
}
190
191         fail("Should have thrown an exception");
192
193     }
194
195
196     /**
197      * http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16864
198      */

199     public void testDomino_Go_Webserver404() throws Exception JavaDoc {
200
201         // this file should not exist
202
method = new GetMethod("http://www.pc.ibm.com/us/accessories/monitors/p_allmodelos.html");
203         int statusCode = client.executeMethod(method);
204
205         assertEquals(404, method.getStatusCode());
206
207     }
208
209
210 }
211
Popular Tags