Index: src/test/org/apache/commons/httpclient/TestHttps.java =================================================================== --- src/test/org/apache/commons/httpclient/TestHttps.java (revision 1422576) +++ src/test/org/apache/commons/httpclient/TestHttps.java (working copy) @@ -31,6 +31,11 @@ package org.apache.commons.httpclient; +import java.net.InetAddress; +import java.net.URL; + +import javax.net.ssl.SSLException; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -57,6 +62,7 @@ // ---------------------------------------------------------------- Members private String _urlWithPort = null; private String _urlWithoutPort = null; + private String _urlWithIp = null; private final String PROXY_HOST = System.getProperty("httpclient.test.proxyHost"); private final String PROXY_PORT = System.getProperty("httpclient.test.proxyPort"); private final String PROXY_USER = System.getProperty("httpclient.test.proxyUser"); @@ -81,6 +87,7 @@ public void setUp() throws Exception { _urlWithPort = "https://www.verisign.com:443/"; _urlWithoutPort = "https://www.verisign.com/"; + _urlWithIp = "https://"+InetAddress.getByName(new URL(_urlWithoutPort).getHost()).getHostAddress(); } public void testHttpsGet() { @@ -140,4 +147,28 @@ fail("Exception thrown while retrieving data : " + t.toString()); } } + + public void testHostNameValidation() { + HttpClient client = new HttpClient(); + if (PROXY_HOST != null) { + if (PROXY_USER != null) { + HttpState state = client.getState(); + state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials( + PROXY_USER, PROXY_PASS)); + } + client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT)); + } + GetMethod method = new GetMethod(_urlWithIp); + + try { + client.executeMethod(method); + fail ("Invalid hostname not detected"); + } catch (SSLException e) { + assertTrue("Connection with a invalid server certificate rejected", true); + } + catch (Throwable t) { + t.printStackTrace(); + fail ("Unexpected exception" + t.getMessage()); + } + } }