1   /*
2    * $HeadURL: http://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHostConfiguration.java $
3    * $Revision: 169849 $
4    * $Date: 2005-05-12 13:05:07 -0400 (Thu, 12 May 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  
28  package org.apache.commons.httpclient;
29  
30  import java.io.IOException;
31  import java.net.UnknownHostException;
32  
33  import junit.framework.Test;
34  import junit.framework.TestSuite;
35  
36  import org.apache.commons.httpclient.methods.GetMethod;
37  import org.apache.commons.httpclient.protocol.Protocol;
38  import org.apache.commons.httpclient.server.SimpleProxy;
39  
40  /***
41   * Tests basic HostConfiguration functionality.
42   *
43   * @author Oleg Kalnichevski
44   * 
45   * @version $Id: TestHostConfiguration.java 169849 2005-05-12 17:05:07Z olegk $
46   */
47  public class TestHostConfiguration extends HttpClientTestBase {
48  
49      public TestHostConfiguration(final String testName) throws IOException {
50          super(testName);
51      }
52  
53      public static Test suite() {
54          return new TestSuite(TestHostConfiguration.class);
55      }
56  
57      public static void main(String args[]) {
58          String[] testCaseName = { TestHostConfiguration.class.getName() };
59          junit.textui.TestRunner.main(testCaseName);
60      }
61      
62      public void testRelativeURLHitWithDefaultHost() throws IOException {
63          this.server.setHttpService(new EchoService());
64          // Set default host
65          this.client.getHostConfiguration().setHost(
66                  this.server.getLocalAddress(), 
67                  this.server.getLocalPort(),
68                  Protocol.getProtocol("http"));
69          
70          GetMethod httpget = new GetMethod("/test/");
71          try {
72              this.client.executeMethod(httpget);
73              assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
74          } finally {
75              httpget.releaseConnection();
76          }
77      }
78  
79      public void testRelativeURLHitWithoutDefaultHost() throws IOException {
80          this.server.setHttpService(new EchoService());
81          // reset default host configuration
82          this.client.setHostConfiguration(new HostConfiguration());
83          
84          GetMethod httpget = new GetMethod("/test/");
85          try {
86              this.client.executeMethod(httpget);
87              fail("IllegalArgumentException should have been thrown");
88          } catch (IllegalArgumentException expected) { 
89          } finally {
90              httpget.releaseConnection();
91          }
92      }
93  
94      public void testAbsoluteURLHitWithoutDefaultHost() throws IOException {
95          this.server.setHttpService(new EchoService());
96          // reset default host configuration
97          this.client.setHostConfiguration(new HostConfiguration());
98          
99          GetMethod httpget = new GetMethod("http://" + 
100                 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
101         try {
102             this.client.executeMethod(httpget);
103             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
104         } finally {
105             httpget.releaseConnection();
106         }
107     }
108 
109     public void testAbsoluteURLOverridesClientDefaultHost() throws IOException {
110         this.server.setHttpService(new EchoService());
111         // Somewhere out there in pampa
112         this.client.getHostConfiguration().setHost("somewhere.outthere.in.pampa", 9999);
113         
114         GetMethod httpget = new GetMethod("http://" + 
115                 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
116         try {
117             this.client.executeMethod(httpget);
118             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
119         } finally {
120             httpget.releaseConnection();
121         }
122         httpget = new GetMethod("/test/");
123         try {
124             this.client.executeMethod(httpget);
125             fail("UnknownHostException should have been thrown");
126         } catch (UnknownHostException expected) { 
127         } finally {
128             httpget.releaseConnection();
129         }
130     }
131 
132     public void testAbsoluteURLOverridesDefaultHostParam() throws IOException {
133 
134         this.proxy = new SimpleProxy();
135         
136         this.server.setHttpService(new EchoService());
137         // reset default host configuration
138         HostConfiguration hostconfig = new HostConfiguration();
139         hostconfig.setHost("somehwere.outthere.in.pampa", 9999);
140         hostconfig.setProxy(
141                 this.proxy.getLocalAddress(), 
142                 this.proxy.getLocalPort());                
143         
144         GetMethod httpget = new GetMethod("http://" + 
145                 this.server.getLocalAddress() + ":" + this.server.getLocalPort() + "/test/");
146         try {
147             this.client.executeMethod(hostconfig, httpget);
148             assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
149             assertNotNull(httpget.getResponseHeader("Via"));
150         } finally {
151             httpget.releaseConnection();
152         }
153         httpget = new GetMethod("/test/");
154         try {
155             this.client.executeMethod(hostconfig, httpget);
156             assertEquals(HttpStatus.SC_NOT_FOUND, httpget.getStatusCode());
157         } finally {
158             httpget.releaseConnection();
159         }
160     }
161 
162 }