001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/test/org/apache/commons/httpclient/TestHostConfiguration.java $
003: * $Revision: 509577 $
004: * $Date: 2007-02-20 15:28:18 +0100 (Tue, 20 Feb 2007) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: */
028:
029: package org.apache.commons.httpclient;
030:
031: import java.io.IOException;
032: import java.net.UnknownHostException;
033:
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036:
037: import org.apache.commons.httpclient.methods.GetMethod;
038: import org.apache.commons.httpclient.protocol.Protocol;
039: import org.apache.commons.httpclient.server.SimpleProxy;
040:
041: /**
042: * Tests basic HostConfiguration functionality.
043: *
044: * @author Oleg Kalnichevski
045: *
046: * @version $Id: TestHostConfiguration.java 509577 2007-02-20 14:28:18Z rolandw $
047: */
048: public class TestHostConfiguration extends HttpClientTestBase {
049:
050: public TestHostConfiguration(final String testName)
051: throws IOException {
052: super (testName);
053: }
054:
055: public static Test suite() {
056: return new TestSuite(TestHostConfiguration.class);
057: }
058:
059: public static void main(String args[]) {
060: String[] testCaseName = { TestHostConfiguration.class.getName() };
061: junit.textui.TestRunner.main(testCaseName);
062: }
063:
064: public void testRelativeURLHitWithDefaultHost() throws IOException {
065: this .server.setHttpService(new EchoService());
066: // Set default host
067: this .client.getHostConfiguration().setHost(
068: this .server.getLocalAddress(),
069: this .server.getLocalPort(),
070: Protocol.getProtocol("http"));
071:
072: GetMethod httpget = new GetMethod("/test/");
073: try {
074: this .client.executeMethod(httpget);
075: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
076: } finally {
077: httpget.releaseConnection();
078: }
079: }
080:
081: public void testRelativeURLHitWithoutDefaultHost()
082: throws IOException {
083: this .server.setHttpService(new EchoService());
084: // reset default host configuration
085: this .client.setHostConfiguration(new HostConfiguration());
086:
087: GetMethod httpget = new GetMethod("/test/");
088: try {
089: this .client.executeMethod(httpget);
090: fail("IllegalArgumentException should have been thrown");
091: } catch (IllegalArgumentException expected) {
092: } finally {
093: httpget.releaseConnection();
094: }
095: }
096:
097: public void testAbsoluteURLHitWithoutDefaultHost()
098: throws IOException {
099: this .server.setHttpService(new EchoService());
100: // reset default host configuration
101: this .client.setHostConfiguration(new HostConfiguration());
102:
103: GetMethod httpget = new GetMethod("http://"
104: + this .server.getLocalAddress() + ":"
105: + this .server.getLocalPort() + "/test/");
106: try {
107: this .client.executeMethod(httpget);
108: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
109: } finally {
110: httpget.releaseConnection();
111: }
112: }
113:
114: public void testAbsoluteURLOverridesClientDefaultHost()
115: throws IOException {
116: this .server.setHttpService(new EchoService());
117: // Somewhere out there in pampa
118: this .client.getHostConfiguration().setHost(
119: "somewhere.outthere.in.pampa", 9999);
120:
121: GetMethod httpget = new GetMethod("http://"
122: + this .server.getLocalAddress() + ":"
123: + this .server.getLocalPort() + "/test/");
124: try {
125: this .client.executeMethod(httpget);
126: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
127: } finally {
128: httpget.releaseConnection();
129: }
130: httpget = new GetMethod("/test/");
131: try {
132: this .client.executeMethod(httpget);
133: fail("UnknownHostException should have been thrown");
134: } catch (UnknownHostException expected) {
135: } finally {
136: httpget.releaseConnection();
137: }
138: }
139:
140: public void testAbsoluteURLOverridesDefaultHostParam()
141: throws IOException {
142:
143: this .proxy = new SimpleProxy();
144:
145: this .server.setHttpService(new EchoService());
146: // reset default host configuration
147: HostConfiguration hostconfig = new HostConfiguration();
148: hostconfig.setHost("somehwere.outthere.in.pampa", 9999);
149: hostconfig.setProxy(this .proxy.getLocalAddress(), this .proxy
150: .getLocalPort());
151:
152: GetMethod httpget = new GetMethod("http://"
153: + this .server.getLocalAddress() + ":"
154: + this .server.getLocalPort() + "/test/");
155: try {
156: this .client.executeMethod(hostconfig, httpget);
157: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
158: assertNotNull(httpget.getResponseHeader("Via"));
159: } finally {
160: httpget.releaseConnection();
161: }
162: httpget = new GetMethod("/test/");
163: try {
164: this .client.executeMethod(hostconfig, httpget);
165: assertEquals(HttpStatus.SC_NOT_FOUND, httpget
166: .getStatusCode());
167: } finally {
168: httpget.releaseConnection();
169: }
170: }
171:
172: /**
173: * Test that HttpClient uses HostConfiguration.clone (not the copy
174: * constructor) to copy its default HostConfiguration when preparing to
175: * execute a method. This behavior is required to support specialized
176: * Protocols; for example, HostConfigurationWithStickyProtocol.
177: *
178: * @see org.apache.commons.httpclient.contrib.ssl.HostConfigurationWithStickyProtocol
179: */
180: public void testClientClonesHostConfiguration() throws IOException {
181: this .server.setHttpService(new EchoService());
182: SpecialHostConfiguration configuration = new SpecialHostConfiguration(
183: this .client.getHostConfiguration());
184: configuration.setHost(this .server.getLocalAddress(),
185: this .server.getLocalPort(), new String(
186: HttpURL.DEFAULT_SCHEME));
187: this .client.setHostConfiguration(configuration);
188:
189: HttpMethod method = new GetMethod(configuration.getHostURL()
190: + "/test/");
191: try {
192: this .client.executeMethod(method);
193: fail("HostConfiguration wasn't cloned");
194: } catch (ExpectedError e) {
195: assertNotSame("ExpectedError.configuration", configuration,
196: e.configuration);
197: } finally {
198: method.releaseConnection();
199: }
200:
201: method = new GetMethod("/test/");
202: try {
203: this .client.executeMethod(method);
204: fail("HostConfiguration wasn't cloned");
205: } catch (ExpectedError e) {
206: assertNotSame("ExpectedError.configuration", configuration,
207: e.configuration);
208: } finally {
209: method.releaseConnection();
210: }
211: }
212:
213: /** A HostConfiguration that refuses to provide a protocol. */
214: private class SpecialHostConfiguration extends HostConfiguration {
215: SpecialHostConfiguration(HostConfiguration hostConfiguration) {
216: super (hostConfiguration);
217: }
218:
219: public Object clone() {
220: return new SpecialHostConfiguration(this );
221: }
222:
223: public synchronized Protocol getProtocol() {
224: throw new ExpectedError(this );
225: }
226: }
227:
228: private class ExpectedError extends Error {
229: ExpectedError(SpecialHostConfiguration c) {
230: configuration = c;
231: }
232:
233: SpecialHostConfiguration configuration;
234:
235: private static final long serialVersionUID = 1L;
236:
237: }
238:
239: }
|