Improved test coverage for classes added in 4.3

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1508949 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-07-31 16:29:35 +00:00
parent f69b8a3472
commit fc8df4ef2a
6 changed files with 456 additions and 17 deletions

View File

@ -189,7 +189,7 @@ public class RequestConfig implements Cloneable {
.setCircularRedirectsAllowed(config.isCircularRedirectsAllowed()) .setCircularRedirectsAllowed(config.isCircularRedirectsAllowed())
.setMaxRedirects(config.getMaxRedirects()) .setMaxRedirects(config.getMaxRedirects())
.setAuthenticationEnabled(config.isAuthenticationEnabled()) .setAuthenticationEnabled(config.isAuthenticationEnabled())
.setTargetPreferredAuthSchemes(config.getProxyPreferredAuthSchemes()) .setTargetPreferredAuthSchemes(config.getTargetPreferredAuthSchemes())
.setProxyPreferredAuthSchemes(config.getProxyPreferredAuthSchemes()) .setProxyPreferredAuthSchemes(config.getProxyPreferredAuthSchemes())
.setConnectionRequestTimeout(config.getConnectionRequestTimeout()) .setConnectionRequestTimeout(config.getConnectionRequestTimeout())
.setConnectTimeout(config.getConnectTimeout()) .setConnectTimeout(config.getConnectTimeout())

View File

@ -0,0 +1,102 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.client.config;
import junit.framework.Assert;
import org.apache.http.HttpHost;
import org.junit.Test;
import java.net.InetAddress;
import java.util.Arrays;
public class TestRequestConfig {
@Test
public void testBasics() {
final RequestConfig config = RequestConfig.custom().build();
config.toString();
}
@Test
public void testDefaults() {
final RequestConfig config = RequestConfig.DEFAULT;
Assert.assertEquals(-1, config.getSocketTimeout());
Assert.assertEquals(-1, config.getConnectTimeout());
Assert.assertEquals(-1, config.getConnectionRequestTimeout());
Assert.assertEquals(false, config.isExpectContinueEnabled());
Assert.assertEquals(true, config.isStaleConnectionCheckEnabled());
Assert.assertEquals(true, config.isAuthenticationEnabled());
Assert.assertEquals(true, config.isRedirectsEnabled());
Assert.assertEquals(true, config.isRelativeRedirectsAllowed());
Assert.assertEquals(false, config.isCircularRedirectsAllowed());
Assert.assertEquals(50, config.getMaxRedirects());
Assert.assertEquals(null, config.getCookieSpec());
Assert.assertEquals(null, config.getLocalAddress());
Assert.assertEquals(null, config.getProxy());
Assert.assertEquals(null, config.getTargetPreferredAuthSchemes());
Assert.assertEquals(null, config.getProxyPreferredAuthSchemes());
}
@Test
public void testBuildAndCopy() throws Exception {
final RequestConfig config0 = RequestConfig.custom()
.setSocketTimeout(22)
.setConnectTimeout(33)
.setConnectionRequestTimeout(44)
.setExpectContinueEnabled(true)
.setStaleConnectionCheckEnabled(false)
.setAuthenticationEnabled(false)
.setRedirectsEnabled(false)
.setRelativeRedirectsAllowed(false)
.setCircularRedirectsAllowed(true)
.setMaxRedirects(100)
.setCookieSpec(CookieSpecs.RFC_2965)
.setLocalAddress(InetAddress.getLocalHost())
.setProxy(new HttpHost("someproxy"))
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.DIGEST))
.build();
final RequestConfig config = RequestConfig.copy(config0).build();
Assert.assertEquals(22, config.getSocketTimeout());
Assert.assertEquals(33, config.getConnectTimeout());
Assert.assertEquals(44, config.getConnectionRequestTimeout());
Assert.assertEquals(true, config.isExpectContinueEnabled());
Assert.assertEquals(false, config.isStaleConnectionCheckEnabled());
Assert.assertEquals(false, config.isAuthenticationEnabled());
Assert.assertEquals(false, config.isRedirectsEnabled());
Assert.assertEquals(false, config.isRelativeRedirectsAllowed());
Assert.assertEquals(true, config.isCircularRedirectsAllowed());
Assert.assertEquals(100, config.getMaxRedirects());
Assert.assertEquals(CookieSpecs.RFC_2965, config.getCookieSpec());
Assert.assertEquals(InetAddress.getLocalHost(), config.getLocalAddress());
Assert.assertEquals(new HttpHost("someproxy"), config.getProxy());
Assert.assertEquals(Arrays.asList(AuthSchemes.NTLM), config.getTargetPreferredAuthSchemes());
Assert.assertEquals(Arrays.asList(AuthSchemes.DIGEST), config.getProxyPreferredAuthSchemes());
}
}

View File

@ -0,0 +1,86 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.client.integration;
import junit.framework.Assert;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.localserver.LocalTestServer;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
public class TestBasicConnectionManager extends IntegrationTestBase {
@Before
public void setUp() throws Exception {
this.localServer = new LocalTestServer(null, null);
this.localServer.registerDefaultHandlers();
this.localServer.start();
this.httpclient = HttpClients.custom()
.setConnectionManager(new BasicHttpClientConnectionManager())
.build();
}
@After
public void cleanup() throws IOException {
this.httpclient.close();
}
@Test
public void testBasics() throws Exception {
final InetSocketAddress address = localServer.getServiceAddress();
final HttpHost target = new HttpHost(address.getHostName(), address.getPort());
final HttpGet get = new HttpGet("/random/1024");
final CloseableHttpResponse response = this.httpclient.execute(target, get);
try {
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
}
@Test(expected=IllegalStateException.class)
public void testConnectionStillInUse() throws Exception {
final InetSocketAddress address = localServer.getServiceAddress();
final HttpHost target = new HttpHost(address.getHostName(), address.getPort());
final HttpGet get1 = new HttpGet("/random/1024");
this.httpclient.execute(target, get1);
final HttpGet get2 = new HttpGet("/random/1024");
this.httpclient.execute(target, get2);
}
}

View File

@ -27,36 +27,59 @@
package org.apache.http.impl.conn; package org.apache.http.impl.conn;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.http.HttpClientConnection; import org.apache.http.HttpClientConnection;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.ConnectionConfig; import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Lookup; import org.apache.http.config.Lookup;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectionRequest; import org.apache.http.conn.ConnectionRequest;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.HttpConnectionFactory; import org.apache.http.conn.HttpConnectionFactory;
import org.apache.http.conn.ManagedHttpClientConnection; import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.SchemePortResolver;
import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.protocol.HttpContext;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
public class TestBasicHttpClientConnectionManager { public class TestBasicHttpClientConnectionManager {
@Mock
private ManagedHttpClientConnection conn; private ManagedHttpClientConnection conn;
@Mock
private HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory; private HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory;
@Mock
private Lookup<ConnectionSocketFactory> socketFactoryRegistry; private Lookup<ConnectionSocketFactory> socketFactoryRegistry;
@Mock
private ConnectionSocketFactory plainSocketFactory;
@Mock
private LayeredConnectionSocketFactory sslSocketFactory;
@Mock
private Socket socket;
@Mock
private SchemePortResolver schemePortResolver;
@Mock
private DnsResolver dnsResolver;
private BasicHttpClientConnectionManager mgr; private BasicHttpClientConnectionManager mgr;
@SuppressWarnings("unchecked")
@Before @Before
public void setup() throws Exception { public void setup() throws Exception {
conn = Mockito.mock(ManagedHttpClientConnection.class); MockitoAnnotations.initMocks(this);
connFactory = Mockito.mock(HttpConnectionFactory.class); mgr = new BasicHttpClientConnectionManager(
socketFactoryRegistry = Mockito.mock(Lookup.class); socketFactoryRegistry, connFactory, schemePortResolver, dnsResolver);
mgr = new BasicHttpClientConnectionManager(socketFactoryRegistry, connFactory);
} }
@Test @Test
@ -262,6 +285,13 @@ public class TestBasicHttpClientConnectionManager {
Assert.fail("IllegalStateException expected"); Assert.fail("IllegalStateException expected");
} catch (final IllegalStateException ex) { } catch (final IllegalStateException ex) {
} }
// Should have no effect
mgr.closeExpiredConnections();
mgr.closeIdleConnections(0L, TimeUnit.MILLISECONDS);
mgr.shutdown();
Mockito.verify(conn, Mockito.times(1)).shutdown();
} }
@Test @Test
@ -339,4 +369,101 @@ public class TestBasicHttpClientConnectionManager {
mgr.getConnection(route, null); mgr.getConnection(route, null);
} }
@Test
public void testTargetConnect() throws Exception {
final HttpHost target = new HttpHost("somehost", -1, "https");
final InetAddress remote = InetAddress.getByAddress(new byte[] {10, 0, 0, 1});
final InetAddress local = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
final HttpRoute route = new HttpRoute(target, local, true);
Mockito.when(connFactory.create(
Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
Assert.assertNotNull(conn1);
final HttpClientContext context = HttpClientContext.create();
final SocketConfig sconfig = SocketConfig.custom().build();
mgr.setSocketConfig(sconfig);
Mockito.when(dnsResolver.resolve("somehost")).thenReturn(new InetAddress[] {remote});
Mockito.when(schemePortResolver.resolve(target)).thenReturn(8443);
Mockito.when(socketFactoryRegistry.lookup("https")).thenReturn(plainSocketFactory);
Mockito.when(plainSocketFactory.createSocket(Mockito.<HttpContext>any())).thenReturn(socket);
Mockito.when(plainSocketFactory.connectSocket(
Mockito.anyInt(),
Mockito.eq(socket),
Mockito.<HttpHost>any(),
Mockito.<InetSocketAddress>any(),
Mockito.<InetSocketAddress>any(),
Mockito.<HttpContext>any())).thenReturn(socket);
mgr.connect(conn1, route, 123, context);
Mockito.verify(dnsResolver, Mockito.times(1)).resolve("somehost");
Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(target);
Mockito.verify(plainSocketFactory, Mockito.times(1)).createSocket(context);
Mockito.verify(plainSocketFactory, Mockito.times(1)).connectSocket(123, socket, target,
new InetSocketAddress(remote, 8443),
new InetSocketAddress(local, 0), context);
mgr.routeComplete(conn1, route, context);
}
@Test
public void testProxyConnectAndUpgrade() throws Exception {
final HttpHost target = new HttpHost("somehost", -1, "https");
final HttpHost proxy = new HttpHost("someproxy", 8080);
final InetAddress remote = InetAddress.getByAddress(new byte[] {10, 0, 0, 1});
final InetAddress local = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
final HttpRoute route = new HttpRoute(target, local, proxy, true);
Mockito.when(connFactory.create(
Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
Assert.assertNotNull(conn1);
final HttpClientContext context = HttpClientContext.create();
final SocketConfig sconfig = SocketConfig.custom().build();
mgr.setSocketConfig(sconfig);
Mockito.when(dnsResolver.resolve("someproxy")).thenReturn(new InetAddress[] {remote});
Mockito.when(schemePortResolver.resolve(proxy)).thenReturn(8080);
Mockito.when(schemePortResolver.resolve(target)).thenReturn(8443);
Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainSocketFactory);
Mockito.when(socketFactoryRegistry.lookup("https")).thenReturn(sslSocketFactory);
Mockito.when(plainSocketFactory.createSocket(Mockito.<HttpContext>any())).thenReturn(socket);
Mockito.when(plainSocketFactory.connectSocket(
Mockito.anyInt(),
Mockito.eq(socket),
Mockito.<HttpHost>any(),
Mockito.<InetSocketAddress>any(),
Mockito.<InetSocketAddress>any(),
Mockito.<HttpContext>any())).thenReturn(socket);
mgr.connect(conn1, route, 123, context);
Mockito.verify(dnsResolver, Mockito.times(1)).resolve("someproxy");
Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(proxy);
Mockito.verify(plainSocketFactory, Mockito.times(1)).createSocket(context);
Mockito.verify(plainSocketFactory, Mockito.times(1)).connectSocket(123, socket, proxy,
new InetSocketAddress(remote, 8080),
new InetSocketAddress(local, 0), context);
Mockito.when(conn.getSocket()).thenReturn(socket);
mgr.upgrade(conn1, route, context);
Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(target);
Mockito.verify(sslSocketFactory, Mockito.times(1)).createLayeredSocket(
socket, "somehost", 8443, context);
mgr.routeComplete(conn1, route, context);
}
} }

View File

@ -27,6 +27,8 @@
package org.apache.http.impl.conn; package org.apache.http.impl.conn;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -35,7 +37,10 @@ import java.util.concurrent.TimeoutException;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpClientConnection; import org.apache.http.HttpClientConnection;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Lookup; import org.apache.http.config.Lookup;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectionPoolTimeoutException; import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.ConnectionRequest; import org.apache.http.conn.ConnectionRequest;
import org.apache.http.conn.DnsResolver; import org.apache.http.conn.DnsResolver;
@ -43,38 +48,43 @@ import org.apache.http.conn.SchemePortResolver;
import org.apache.http.conn.ManagedHttpClientConnection; import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
/** /**
* {@link PoolingHttpClientConnectionManager} tests. * {@link PoolingHttpClientConnectionManager} tests.
*/ */
public class TestPoolingHttpClientConnectionManager { public class TestPoolingHttpClientConnectionManager {
@Mock
private ManagedHttpClientConnection conn; private ManagedHttpClientConnection conn;
private Socket socket; @Mock
private ConnectionSocketFactory plainSocketFactory;
private Lookup<ConnectionSocketFactory> socketFactoryRegistry; private Lookup<ConnectionSocketFactory> socketFactoryRegistry;
@Mock
private ConnectionSocketFactory plainSocketFactory;
@Mock
private ConnectionSocketFactory sslSocketFactory;
@Mock
private Socket socket;
@Mock
private SchemePortResolver schemePortResolver; private SchemePortResolver schemePortResolver;
@Mock
private DnsResolver dnsResolver; private DnsResolver dnsResolver;
@Mock
private Future<CPoolEntry> future; private Future<CPoolEntry> future;
@Mock
private CPool pool; private CPool pool;
private PoolingHttpClientConnectionManager mgr; private PoolingHttpClientConnectionManager mgr;
@SuppressWarnings("unchecked")
@Before @Before
public void setup() throws Exception { public void setup() throws Exception {
conn = Mockito.mock(ManagedHttpClientConnection.class); MockitoAnnotations.initMocks(this);
socket = Mockito.mock(Socket.class);
plainSocketFactory = Mockito.mock(ConnectionSocketFactory.class);
socketFactoryRegistry = Mockito.mock(Lookup.class);
schemePortResolver = Mockito.mock(SchemePortResolver.class);
dnsResolver = Mockito.mock(DnsResolver.class);
pool = Mockito.mock(CPool.class);
future = Mockito.mock(Future.class);
mgr = new PoolingHttpClientConnectionManager( mgr = new PoolingHttpClientConnectionManager(
pool, socketFactoryRegistry, schemePortResolver, dnsResolver); pool, socketFactoryRegistry, schemePortResolver, dnsResolver);
} }
@ -218,4 +228,118 @@ public class TestPoolingHttpClientConnectionManager {
Mockito.verify(entry, Mockito.never()).updateExpiry(Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS)); Mockito.verify(entry, Mockito.never()).updateExpiry(Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS));
} }
@Test
public void testTargetConnect() throws Exception {
final HttpHost target = new HttpHost("somehost", -1, "https");
final InetAddress remote = InetAddress.getByAddress(new byte[] {10, 0, 0, 1});
final InetAddress local = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
final HttpRoute route = new HttpRoute(target, local, true);
final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn,
-1, TimeUnit.MILLISECONDS);
entry.markRouteComplete();
Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE);
Mockito.when(conn.isOpen()).thenReturn(true);
Mockito.when(future.isCancelled()).thenReturn(false);
Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry);
Mockito.when(pool.lease(route, null, null)).thenReturn(future);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS);
Assert.assertNotNull(conn1);
final HttpClientContext context = HttpClientContext.create();
final SocketConfig sconfig = SocketConfig.custom().build();
mgr.setDefaultSocketConfig(sconfig);
Mockito.when(dnsResolver.resolve("somehost")).thenReturn(new InetAddress[]{remote});
Mockito.when(schemePortResolver.resolve(target)).thenReturn(8443);
Mockito.when(socketFactoryRegistry.lookup("https")).thenReturn(plainSocketFactory);
Mockito.when(plainSocketFactory.createSocket(Mockito.<HttpContext>any())).thenReturn(socket);
Mockito.when(plainSocketFactory.connectSocket(
Mockito.anyInt(),
Mockito.eq(socket),
Mockito.<HttpHost>any(),
Mockito.<InetSocketAddress>any(),
Mockito.<InetSocketAddress>any(),
Mockito.<HttpContext>any())).thenReturn(socket);
mgr.connect(conn1, route, 123, context);
Mockito.verify(dnsResolver, Mockito.times(1)).resolve("somehost");
Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(target);
Mockito.verify(plainSocketFactory, Mockito.times(1)).createSocket(context);
Mockito.verify(plainSocketFactory, Mockito.times(1)).connectSocket(123, socket, target,
new InetSocketAddress(remote, 8443),
new InetSocketAddress(local, 0), context);
mgr.routeComplete(conn1, route, context);
}
@Test
public void testProxyConnectAndUpgrade() throws Exception {
final HttpHost target = new HttpHost("somehost", -1, "https");
final HttpHost proxy = new HttpHost("someproxy", 8080);
final InetAddress remote = InetAddress.getByAddress(new byte[] {10, 0, 0, 1});
final InetAddress local = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
final HttpRoute route = new HttpRoute(target, local, proxy, true);
final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn,
-1, TimeUnit.MILLISECONDS);
entry.markRouteComplete();
Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE);
Mockito.when(conn.isOpen()).thenReturn(true);
Mockito.when(future.isCancelled()).thenReturn(false);
Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry);
Mockito.when(pool.lease(route, null, null)).thenReturn(future);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS);
Assert.assertNotNull(conn1);
final ConnectionSocketFactory plainsf = Mockito.mock(ConnectionSocketFactory.class);
final LayeredConnectionSocketFactory sslsf = Mockito.mock(LayeredConnectionSocketFactory.class);
final Socket socket = Mockito.mock(Socket.class);
final HttpClientContext context = HttpClientContext.create();
final SocketConfig sconfig = SocketConfig.custom().build();
final ConnectionConfig cconfig = ConnectionConfig.custom().build();
mgr.setDefaultSocketConfig(sconfig);
mgr.setDefaultConnectionConfig(cconfig);
Mockito.when(dnsResolver.resolve("someproxy")).thenReturn(new InetAddress[] {remote});
Mockito.when(schemePortResolver.resolve(proxy)).thenReturn(8080);
Mockito.when(schemePortResolver.resolve(target)).thenReturn(8443);
Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainsf);
Mockito.when(socketFactoryRegistry.lookup("https")).thenReturn(sslsf);
Mockito.when(plainsf.createSocket(Mockito.<HttpContext>any())).thenReturn(socket);
Mockito.when(plainsf.connectSocket(
Mockito.anyInt(),
Mockito.eq(socket),
Mockito.<HttpHost>any(),
Mockito.<InetSocketAddress>any(),
Mockito.<InetSocketAddress>any(),
Mockito.<HttpContext>any())).thenReturn(socket);
mgr.connect(conn1, route, 123, context);
Mockito.verify(dnsResolver, Mockito.times(1)).resolve("someproxy");
Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(proxy);
Mockito.verify(plainsf, Mockito.times(1)).createSocket(context);
Mockito.verify(plainsf, Mockito.times(1)).connectSocket(123, socket, proxy,
new InetSocketAddress(remote, 8080),
new InetSocketAddress(local, 0), context);
Mockito.when(conn.getSocket()).thenReturn(socket);
mgr.upgrade(conn1, route, context);
Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(target);
Mockito.verify(sslsf, Mockito.times(1)).createLayeredSocket(
socket, "somehost", 8443, context);
mgr.routeComplete(conn1, route, context);
}
} }