You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2013/07/31 18:29:35 UTC
svn commit: r1508949 - in /httpcomponents/httpclient/trunk/httpclient/src:
main/java/org/apache/http/client/config/
main/java/org/apache/http/impl/client/
test/java/org/apache/http/client/config/
test/java/org/apache/http/impl/client/integration/ test/...
Author: olegk
Date: Wed Jul 31 16:29:35 2013
New Revision: 1508949
URL: http://svn.apache.org/r1508949
Log:
Improved test coverage for classes added in 4.3
Added:
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/config/
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/config/TestRequestConfig.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestBasicConnectionManager.java (with props)
Modified:
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java (contents, props changed)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestPoolingHttpClientConnectionManager.java
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java?rev=1508949&r1=1508948&r2=1508949&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java Wed Jul 31 16:29:35 2013
@@ -189,7 +189,7 @@ public class RequestConfig implements Cl
.setCircularRedirectsAllowed(config.isCircularRedirectsAllowed())
.setMaxRedirects(config.getMaxRedirects())
.setAuthenticationEnabled(config.isAuthenticationEnabled())
- .setTargetPreferredAuthSchemes(config.getProxyPreferredAuthSchemes())
+ .setTargetPreferredAuthSchemes(config.getTargetPreferredAuthSchemes())
.setProxyPreferredAuthSchemes(config.getProxyPreferredAuthSchemes())
.setConnectionRequestTimeout(config.getConnectionRequestTimeout())
.setConnectTimeout(config.getConnectTimeout())
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java?rev=1508949&r1=1508948&r2=1508949&view=diff
==============================================================================
(empty)
Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
('svn:executable' removed)
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/config/TestRequestConfig.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/config/TestRequestConfig.java?rev=1508949&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/config/TestRequestConfig.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/config/TestRequestConfig.java Wed Jul 31 16:29:35 2013
@@ -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());
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/config/TestRequestConfig.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/config/TestRequestConfig.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/config/TestRequestConfig.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestBasicConnectionManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestBasicConnectionManager.java?rev=1508949&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestBasicConnectionManager.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestBasicConnectionManager.java Wed Jul 31 16:29:35 2013
@@ -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);
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestBasicConnectionManager.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestBasicConnectionManager.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestBasicConnectionManager.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java?rev=1508949&r1=1508948&r2=1508949&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java Wed Jul 31 16:29:35 2013
@@ -27,36 +27,59 @@
package org.apache.http.impl.conn;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpClientConnection;
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.SocketConfig;
import org.apache.http.conn.ConnectionRequest;
+import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.HttpConnectionFactory;
import org.apache.http.conn.ManagedHttpClientConnection;
+import org.apache.http.conn.SchemePortResolver;
import org.apache.http.conn.routing.HttpRoute;
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.Before;
import org.junit.Test;
+import org.mockito.Mock;
import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
public class TestBasicHttpClientConnectionManager {
+ @Mock
private ManagedHttpClientConnection conn;
+ @Mock
private HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory;
+ @Mock
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;
- @SuppressWarnings("unchecked")
@Before
public void setup() throws Exception {
- conn = Mockito.mock(ManagedHttpClientConnection.class);
- connFactory = Mockito.mock(HttpConnectionFactory.class);
- socketFactoryRegistry = Mockito.mock(Lookup.class);
- mgr = new BasicHttpClientConnectionManager(socketFactoryRegistry, connFactory);
+ MockitoAnnotations.initMocks(this);
+ mgr = new BasicHttpClientConnectionManager(
+ socketFactoryRegistry, connFactory, schemePortResolver, dnsResolver);
}
@Test
@@ -262,6 +285,13 @@ public class TestBasicHttpClientConnecti
Assert.fail("IllegalStateException expected");
} 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
@@ -339,4 +369,101 @@ public class TestBasicHttpClientConnecti
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);
+ }
+
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestPoolingHttpClientConnectionManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestPoolingHttpClientConnectionManager.java?rev=1508949&r1=1508948&r2=1508949&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestPoolingHttpClientConnectionManager.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestPoolingHttpClientConnectionManager.java Wed Jul 31 16:29:35 2013
@@ -27,6 +27,8 @@
package org.apache.http.impl.conn;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@@ -35,7 +37,10 @@ import java.util.concurrent.TimeoutExcep
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpClientConnection;
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.SocketConfig;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.ConnectionRequest;
import org.apache.http.conn.DnsResolver;
@@ -43,38 +48,43 @@ import org.apache.http.conn.SchemePortRe
import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.routing.HttpRoute;
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.Before;
import org.junit.Test;
+import org.mockito.Mock;
import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
/**
* {@link PoolingHttpClientConnectionManager} tests.
*/
public class TestPoolingHttpClientConnectionManager {
+ @Mock
private ManagedHttpClientConnection conn;
- private Socket socket;
- private ConnectionSocketFactory plainSocketFactory;
+ @Mock
private Lookup<ConnectionSocketFactory> socketFactoryRegistry;
+ @Mock
+ private ConnectionSocketFactory plainSocketFactory;
+ @Mock
+ private ConnectionSocketFactory sslSocketFactory;
+ @Mock
+ private Socket socket;
+ @Mock
private SchemePortResolver schemePortResolver;
+ @Mock
private DnsResolver dnsResolver;
+ @Mock
private Future<CPoolEntry> future;
+ @Mock
private CPool pool;
private PoolingHttpClientConnectionManager mgr;
- @SuppressWarnings("unchecked")
@Before
public void setup() throws Exception {
- conn = Mockito.mock(ManagedHttpClientConnection.class);
- 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);
+ MockitoAnnotations.initMocks(this);
mgr = new PoolingHttpClientConnectionManager(
pool, socketFactoryRegistry, schemePortResolver, dnsResolver);
}
@@ -218,4 +228,118 @@ public class TestPoolingHttpClientConnec
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);
+ }
+
}