1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestHostConfiguration.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 19:29:41 +0100 (Sat, 22 Dec 2012) $
5    * ====================================================================
6    *
7    *  Licensed to the Apache Software Foundation (ASF) under one or more
8    *  contributor license agreements.  See the NOTICE file distributed with
9    *  this work for additional information regarding copyright ownership.
10   *  The ASF licenses this file to You under the Apache License, Version 2.0
11   *  (the "License"); you may not use this file except in compliance with
12   *  the License.  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   */
28  
29  package org.apache.commons.httpclient;
30  
31  import java.io.IOException;
32  import java.net.UnknownHostException;
33  
34  import junit.framework.Test;
35  import junit.framework.TestSuite;
36  
37  import org.apache.commons.httpclient.methods.GetMethod;
38  import org.apache.commons.httpclient.protocol.Protocol;
39  import org.apache.commons.httpclient.server.SimpleProxy;
40  
41  /***
42   * Tests basic HostConfiguration functionality.
43   *
44   * @author Oleg Kalnichevski
45   * 
46   * @version $Id: TestHostConfiguration.java 509577 2007-02-20 14:28:18Z rolandw $
47   */
48  public class TestHostConfiguration extends HttpClientTestBase {
49  
50      public TestHostConfiguration(final String testName) throws IOException {
51          super(testName);
52      }
53  
54      public static Test suite() {
55          return new TestSuite(TestHostConfiguration.class);
56      }
57  
58      public static void main(String args[]) {
59          String[] testCaseName = { TestHostConfiguration.class.getName() };
60          junit.textui.TestRunner.main(testCaseName);
61      }
62      
63      public void testRelativeURLHitWithDefaultHost() throws IOException {
64          this.server.setHttpService(new EchoService());
65          // Set default host
66          this.client.getHostConfiguration().setHost(
67                  this.server.getLocalAddress(), 
68                  this.server.getLocalPort(),
69                  Protocol.getProtocol("http"));
70          
71          GetMethod httpget = new GetMethod("/test/");
72          try {
73              this.client.executeMethod(httpget);
74              assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
75          } finally {
76              httpget.releaseConnection();
77          }
78      }
79  
80      public void testRelativeURLHitWithoutDefaultHost() throws IOException {
81          this.server.setHttpService(new EchoService());
82          // reset default host configuration
83          this.client.setHostConfiguration(new HostConfiguration());
84          
85          GetMethod httpget = new GetMethod("/test/");
86          try {
87              this.client.executeMethod(httpget);
88              fail("IllegalArgumentException should have been thrown");
89          } catch (IllegalArgumentException expected) { 
90          } finally {
91              httpget.releaseConnection();
92          }
93      }
94  
95      public void testAbsoluteURLHitWithoutDefaultHost() throws IOException {
96          this.server.setHttpService(new EchoService());
97          // reset default host configuration
98          this.client.setHostConfiguration(new HostConfiguration());
99          
100         GetMethod httpget = new GetMethod("http://" + 
101                 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
102         try {
103             this.client.executeMethod(httpget);
104             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
105         } finally {
106             httpget.releaseConnection();
107         }
108     }
109 
110     public void testAbsoluteURLOverridesClientDefaultHost() throws IOException {
111         this.server.setHttpService(new EchoService());
112         // Somewhere out there in pampa
113         this.client.getHostConfiguration().setHost("somewhere.outthere.in.pampa", 9999);
114         
115         GetMethod httpget = new GetMethod("http://" + 
116                 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
117         try {
118             this.client.executeMethod(httpget);
119             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
120         } finally {
121             httpget.releaseConnection();
122         }
123         httpget = new GetMethod("/test/");
124         try {
125             this.client.executeMethod(httpget);
126             fail("UnknownHostException should have been thrown");
127         } catch (UnknownHostException expected) { 
128         } finally {
129             httpget.releaseConnection();
130         }
131     }
132 
133     public void testAbsoluteURLOverridesDefaultHostParam() throws IOException {
134 
135         this.proxy = new SimpleProxy();
136         
137         this.server.setHttpService(new EchoService());
138         // reset default host configuration
139         HostConfiguration hostconfig = new HostConfiguration();
140         hostconfig.setHost("somehwere.outthere.in.pampa", 9999);
141         hostconfig.setProxy(
142                 this.proxy.getLocalAddress(), 
143                 this.proxy.getLocalPort());                
144         
145         GetMethod httpget = new GetMethod("http://" + 
146                 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
147         try {
148             this.client.executeMethod(hostconfig, httpget);
149             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
150             assertNotNull(httpget.getResponseHeader("Via"));
151         } finally {
152             httpget.releaseConnection();
153         }
154         httpget = new GetMethod("/test/");
155         try {
156             this.client.executeMethod(hostconfig, httpget);
157             assertEquals(HttpStatus.SC_NOT_FOUND, httpget.getStatusCode());
158         } finally {
159             httpget.releaseConnection();
160         }
161     }
162 
163     /***
164      * Test that HttpClient uses HostConfiguration.clone (not the copy
165      * constructor) to copy its default HostConfiguration when preparing to
166      * execute a method. This behavior is required to support specialized
167      * Protocols; for example, HostConfigurationWithStickyProtocol.
168      * 
169      * @see org.apache.commons.httpclient.contrib.ssl.HostConfigurationWithStickyProtocol
170      */
171     public void testClientClonesHostConfiguration() throws IOException {
172         this.server.setHttpService(new EchoService());
173         SpecialHostConfiguration configuration = new SpecialHostConfiguration(this.client
174                 .getHostConfiguration());
175         configuration.setHost(this.server.getLocalAddress(), this.server.getLocalPort(),
176                 new String(HttpURL.DEFAULT_SCHEME));
177         this.client.setHostConfiguration(configuration);
178 
179         HttpMethod method = new GetMethod(configuration.getHostURL() + "/test/");
180         try {
181             this.client.executeMethod(method);
182             fail("HostConfiguration wasn't cloned");
183         } catch (ExpectedError e) {
184             assertNotSame("ExpectedError.configuration", configuration, e.configuration);
185         } finally {
186             method.releaseConnection();
187         }
188 
189         method = new GetMethod("/test/");
190         try {
191             this.client.executeMethod(method);
192             fail("HostConfiguration wasn't cloned");
193         } catch (ExpectedError e) {
194             assertNotSame("ExpectedError.configuration", configuration, e.configuration);
195         } finally {
196             method.releaseConnection();
197         }
198     }
199 
200     /*** A HostConfiguration that refuses to provide a protocol. */
201     private class SpecialHostConfiguration extends HostConfiguration
202     {
203         SpecialHostConfiguration(HostConfiguration hostConfiguration)
204         {
205             super(hostConfiguration);
206         }
207 
208         public Object clone()
209         {
210             return new SpecialHostConfiguration(this);
211         }
212 
213         public synchronized Protocol getProtocol()
214         {
215             throw new ExpectedError(this);
216         }
217     }
218 
219     private class ExpectedError extends Error
220     {
221         ExpectedError(SpecialHostConfiguration c)
222         {
223             configuration = c;
224         }
225 
226         SpecialHostConfiguration configuration;
227 
228         private static final long serialVersionUID = 1L;
229 
230     }
231 
232 }