1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestMethodsExternalHost.java,v 1.15 2004/09/17 07:57:49 oglueck Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
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;
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      // -------------------------------------------------------------- Constants
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      // ------------------------------------------------------------ Constructor
69  
70  
71      public TestMethodsExternalHost(String testName) {
72          super(testName);
73      }
74  
75  
76      // ------------------------------------------------------- TestCase Methods
77  
78  
79      public static Test suite() {
80          return new TestSuite(TestMethodsExternalHost.class);
81      }
82  
83      // ------------------------------------------------------- Helper Methods
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     // ----------------------------------------------------------- OPTIONS Test
116 
117 
118     public void testMethodsOptionsExternal() {
119 
120         method = new OptionsMethod(externalPath);
121         executeMethod();
122 
123         Enumeration methodsAllowed = ((OptionsMethod)method).getAllowedMethods();
124         // This enumeration musn't be empty
125         assertTrue("No HTTP method allowed : result of OPTIONS is incorrect.",
126                methodsAllowed.hasMoreElements());
127 
128     }
129     // --------------------------------------------------------------- GET Test
130 
131 
132     public void testMethodsGetExternal() {
133 
134         method = new GetMethod(externalUri);
135         executeMethod();
136 
137         try {
138             String data = method.getResponseBodyAsString();
139             // This enumeration musn't be empty
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             // This enumeration musn't be empty
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     // -------------------------------------------------------------- HEAD Test
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; // IOException and HttpException are ok
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         // this file should not exist
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 }