HTTPCLIENT-1536: default connection operator to use HttpHost#getAddress if available

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1615915 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-08-05 11:53:55 +00:00
parent f3e67f807c
commit 6c4f30fc4d
2 changed files with 34 additions and 1 deletions

View File

@ -107,7 +107,8 @@ public class DefaultHttpClientConnectionOperator implements HttpClientConnection
throw new UnsupportedSchemeException(host.getSchemeName() +
" protocol is not supported");
}
final InetAddress[] addresses = this.dnsResolver.resolve(host.getHostName());
final InetAddress[] addresses = host.getAddress() != null ?
new InetAddress[] { host.getAddress() } : this.dnsResolver.resolve(host.getHostName());
final int port = this.schemePortResolver.resolve(host);
for (int i = 0; i < addresses.length; i++) {
final InetAddress address = addresses[i];

View File

@ -206,6 +206,38 @@ public class TestHttpClientConnectionOperator {
Mockito.verify(conn, Mockito.times(3)).bind(socket);
}
@Test
public void testConnectExplicitAddress() throws Exception {
final HttpContext context = new BasicHttpContext();
final InetAddress local = InetAddress.getByAddress(new byte[] {127, 0, 0, 0});
final InetAddress ip = InetAddress.getByAddress(new byte[] {127, 0, 0, 23});
final HttpHost host = new HttpHost(ip);
Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainSocketFactory);
Mockito.when(schemePortResolver.resolve(host)).thenReturn(80);
Mockito.when(plainSocketFactory.createSocket(Mockito.<HttpContext>any())).thenReturn(socket);
Mockito.when(plainSocketFactory.connectSocket(
Mockito.anyInt(),
Mockito.<Socket>any(),
Mockito.<HttpHost>any(),
Mockito.<InetSocketAddress>any(),
Mockito.<InetSocketAddress>any(),
Mockito.<HttpContext>any())).thenReturn(socket);
final InetSocketAddress localAddress = new InetSocketAddress(local, 0);
connectionOperator.connect(conn, host, localAddress, 1000, SocketConfig.DEFAULT, context);
Mockito.verify(plainSocketFactory).connectSocket(
1000,
socket,
host,
new InetSocketAddress(ip, 80),
localAddress,
context);
Mockito.verify(dnsResolver, Mockito.never()).resolve(Mockito.anyString());
Mockito.verify(conn, Mockito.times(2)).bind(socket);
}
@Test
public void testUpgrade() throws Exception {
final HttpContext context = new BasicHttpContext();