Index: TestHostConfiguration.java =================================================================== --- TestHostConfiguration.java (Revision 546403) +++ TestHostConfiguration.java (Arbeitskopie) @@ -29,13 +29,17 @@ package org.apache.commons.httpclient; import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; import java.net.UnknownHostException; import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import org.apache.commons.httpclient.server.SimpleProxy; /** @@ -197,6 +201,74 @@ } } + + /** + * Test if HttpClient uses the custom protocol provided by the HostConfiguration. + */ + public void testProvidedProtocol() throws HttpException, IOException { + this.server.setHttpService(new EchoService()); + Protocol appWideDefault = Protocol.getProtocol("http"); + HostConfiguration configuration=new HostConfiguration(); + Protocol custom = new Protocol("http",new SpecialSocketFactory(configuration),443); + configuration.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), custom); + this.client.setHostConfiguration(configuration); + + HttpMethod method = new GetMethod(configuration.getHostURL() + "/test/"); + try { + this.client.executeMethod(method); + fail("Provided protocol wasn't used"); + } catch (ExpectedError e) { + assertSame("Expected custom protocol", e.configuration.getProtocol(), custom); + } finally { + method.releaseConnection(); + } + + Protocol newAppwide = new Protocol("http",new SpecialSocketFactory(configuration),443); + Protocol.registerProtocol("http", newAppwide); + configuration.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), + new String(HttpURL.DEFAULT_SCHEME)); + this.client.setHostConfiguration(configuration); + method = new GetMethod(configuration.getHostURL() + "/test/"); + try { + this.client.executeMethod(method); + fail("system wide protocol wasn't used"); + } catch (ExpectedError e) { + assertSame("Expected new application wide protocol", newAppwide, e.configuration.getProtocol()); + } finally { + method.releaseConnection(); + Protocol.registerProtocol("http", appWideDefault); + } + + } + + + private class SpecialSocketFactory implements ProtocolSocketFactory{ + + private HostConfiguration c; + + protected SpecialSocketFactory(HostConfiguration c) + { + this.c = c; + } + + public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException, UnknownHostException + { + throw new ExpectedError(c); + } + + public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException + { + throw new ExpectedError(c); + } + + public Socket createSocket(String host, int port) throws IOException, UnknownHostException + { + throw new ExpectedError(c); + } + + } + + /** A HostConfiguration that refuses to provide a protocol. */ private class SpecialHostConfiguration extends HostConfiguration { @@ -218,12 +290,12 @@ private class ExpectedError extends Error { - ExpectedError(SpecialHostConfiguration c) + ExpectedError(HostConfiguration c) { configuration = c; } - SpecialHostConfiguration configuration; + HostConfiguration configuration; private static final long serialVersionUID = 1L;