View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.nio.conn;
28  
29  import java.lang.reflect.Proxy;
30  import java.net.InetAddress;
31  import java.net.InetSocketAddress;
32  import java.util.Calendar;
33  import java.util.concurrent.ExecutionException;
34  import java.util.concurrent.Future;
35  import java.util.concurrent.TimeUnit;
36  
37  import junit.framework.Assert;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.http.HttpHost;
41  import org.apache.http.concurrent.FutureCallback;
42  import org.apache.http.config.ConnectionConfig;
43  import org.apache.http.config.Registry;
44  import org.apache.http.config.RegistryBuilder;
45  import org.apache.http.conn.DnsResolver;
46  import org.apache.http.conn.SchemePortResolver;
47  import org.apache.http.conn.UnsupportedSchemeException;
48  import org.apache.http.conn.routing.HttpRoute;
49  import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.ConfigData;
50  import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.InternalAddressResolver;
51  import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.InternalConnectionFactory;
52  import org.apache.http.nio.NHttpClientConnection;
53  import org.apache.http.nio.conn.ManagedNHttpClientConnection;
54  import org.apache.http.nio.conn.NHttpConnectionFactory;
55  import org.apache.http.nio.conn.SchemeIOSessionStrategy;
56  import org.apache.http.nio.reactor.ConnectingIOReactor;
57  import org.apache.http.nio.reactor.IOSession;
58  import org.apache.http.nio.reactor.SessionRequest;
59  import org.apache.http.protocol.BasicHttpContext;
60  import org.apache.http.protocol.HttpContext;
61  import org.junit.Before;
62  import org.junit.Test;
63  import org.mockito.ArgumentCaptor;
64  import org.mockito.Captor;
65  import org.mockito.Mock;
66  import org.mockito.Mockito;
67  import org.mockito.MockitoAnnotations;
68  
69  public class TestPoolingHttpClientAsyncConnectionManager {
70  
71      @Mock
72      private ConnectingIOReactor ioreactor;
73      @Mock
74      private CPool pool;
75      @Mock
76      private SchemeIOSessionStrategy noopStrategy;
77      @Mock
78      private SchemeIOSessionStrategy sslStrategy;
79      @Mock
80      private SchemePortResolver schemePortResolver;
81      @Mock
82      private DnsResolver dnsResolver;
83      @Mock
84      private FutureCallback<NHttpClientConnection> connCallback;
85      @Captor
86      private ArgumentCaptor<FutureCallback<CPoolEntry>> poolEntryCallbackCaptor;
87      @Mock
88      private ManagedNHttpClientConnection conn;
89      @Mock
90      private NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory;
91      @Mock
92      private SessionRequest sessionRequest;
93      @Mock
94      private IOSession iosession;
95  
96      private Registry<SchemeIOSessionStrategy> layeringStrategyRegistry;
97      private PoolingNHttpClientConnectionManager connman;
98  
99      @Before
100     public void setUp() throws Exception {
101         MockitoAnnotations.initMocks(this);
102         Mockito.when(sslStrategy.isLayeringRequired()).thenReturn(Boolean.TRUE);
103 
104         layeringStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create()
105             .register("http", noopStrategy)
106             .register("https", sslStrategy)
107             .build();
108         connman = new PoolingNHttpClientConnectionManager(
109             ioreactor, pool, layeringStrategyRegistry);
110     }
111 
112     @Test
113     public void testShutdown() throws Exception {
114         connman.shutdown();
115 
116         Mockito.verify(pool).shutdown(2000);
117     }
118 
119     @Test
120     public void testShutdownMs() throws Exception {
121         connman.shutdown(500);
122 
123         Mockito.verify(pool).shutdown(500);
124     }
125 
126     @Test
127     public void testRequestReleaseConnection() throws Exception {
128         final HttpHost target = new HttpHost("localhost");
129         final HttpRoute route = new HttpRoute(target);
130         final Future<NHttpClientConnection> future = connman.requestConnection(
131             route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, connCallback);
132         Assert.assertNotNull(future);
133 
134         Mockito.verify(pool).lease(
135                 Mockito.same(route),
136                 Mockito.eq("some state"),
137                 Mockito.eq(1000L),
138                 Mockito.eq(2000L),
139                 Mockito.eq(TimeUnit.MILLISECONDS),
140                 poolEntryCallbackCaptor.capture());
141         final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
142         final Log log = Mockito.mock(Log.class);
143         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
144         poolentry.markRouteComplete();
145         callaback.completed(poolentry);
146 
147         Assert.assertTrue(future.isDone());
148         final NHttpClientConnection managedConn = future.get();
149         Assert.assertTrue(Proxy.isProxyClass(managedConn.getClass()));
150         Mockito.verify(connCallback).completed(Mockito.<NHttpClientConnection>any());
151 
152         Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE);
153         connman.releaseConnection(managedConn, "new state", 5, TimeUnit.SECONDS);
154 
155         Mockito.verify(pool).release(poolentry, true);
156         Assert.assertEquals("new state", poolentry.getState());
157         final Calendar cal = Calendar.getInstance();
158         cal.setTimeInMillis(poolentry.getUpdated());
159         cal.add(Calendar.SECOND, 5);
160         Assert.assertEquals(cal.getTimeInMillis(), poolentry.getExpiry());
161     }
162 
163     @Test
164     public void testReleaseConnectionIncompleteRoute() throws Exception {
165         final HttpHost target = new HttpHost("localhost");
166         final HttpRoute route = new HttpRoute(target);
167         final Future<NHttpClientConnection> future = connman.requestConnection(
168             route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, connCallback);
169         Assert.assertNotNull(future);
170 
171         Mockito.verify(pool).lease(
172                 Mockito.same(route),
173                 Mockito.eq("some state"),
174                 Mockito.eq(1000L),
175                 Mockito.eq(2000L),
176                 Mockito.eq(TimeUnit.MILLISECONDS),
177                 poolEntryCallbackCaptor.capture());
178         final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
179         final Log log = Mockito.mock(Log.class);
180         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
181         callaback.completed(poolentry);
182 
183         Assert.assertTrue(future.isDone());
184         final NHttpClientConnection managedConn = future.get();
185         Assert.assertTrue(Proxy.isProxyClass(managedConn.getClass()));
186         Mockito.verify(connCallback).completed(Mockito.<NHttpClientConnection>any());
187 
188         Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE);
189         connman.releaseConnection(managedConn, "new state", 5, TimeUnit.SECONDS);
190 
191         Mockito.verify(pool).release(poolentry, false);
192     }
193 
194     @Test
195     public void testRequestConnectionFutureCancelled() throws Exception {
196         final HttpHost target = new HttpHost("localhost");
197         final HttpRoute route = new HttpRoute(target);
198         final Future<NHttpClientConnection> future = connman.requestConnection(
199             route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, null);
200         Assert.assertNotNull(future);
201         future.cancel(true);
202 
203         Mockito.verify(pool).lease(
204                 Mockito.same(route),
205                 Mockito.eq("some state"),
206                 Mockito.eq(1000L),
207                 Mockito.eq(2000L),
208                 Mockito.eq(TimeUnit.MILLISECONDS),
209                 poolEntryCallbackCaptor.capture());
210         final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
211         final Log log = Mockito.mock(Log.class);
212         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
213         poolentry.markRouteComplete();
214         callaback.completed(poolentry);
215 
216         Mockito.verify(pool).release(poolentry, true);
217     }
218 
219     @Test(expected=ExecutionException.class)
220     public void testRequestConnectionFailed() throws Exception {
221         final HttpHost target = new HttpHost("localhost");
222         final HttpRoute route = new HttpRoute(target);
223         final Future<NHttpClientConnection> future = connman.requestConnection(
224             route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, null);
225         Assert.assertNotNull(future);
226 
227         Mockito.verify(pool).lease(
228                 Mockito.same(route),
229                 Mockito.eq("some state"),
230                 Mockito.eq(1000L),
231                 Mockito.eq(2000L),
232                 Mockito.eq(TimeUnit.MILLISECONDS),
233                 poolEntryCallbackCaptor.capture());
234         final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
235         callaback.failed(new Exception());
236 
237         Assert.assertTrue(future.isDone());
238         future.get();
239     }
240 
241     @Test
242     public void testRequestConnectionCancelled() throws Exception {
243         final HttpHost target = new HttpHost("localhost");
244         final HttpRoute route = new HttpRoute(target);
245         final Future<NHttpClientConnection> future = connman.requestConnection(
246             route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, null);
247         Assert.assertNotNull(future);
248 
249         Mockito.verify(pool).lease(
250                 Mockito.same(route),
251                 Mockito.eq("some state"),
252                 Mockito.eq(1000L),
253                 Mockito.eq(2000L),
254                 Mockito.eq(TimeUnit.MILLISECONDS),
255                 poolEntryCallbackCaptor.capture());
256         final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
257         callaback.cancelled();
258 
259         Assert.assertTrue(future.isDone());
260         Assert.assertTrue(future.isCancelled());
261         Assert.assertNull(future.get());
262     }
263 
264     @Test
265     public void testConnectionInitialize() throws Exception {
266         final HttpHost target = new HttpHost("somehost", -1, "http");
267         final HttpRoute route = new HttpRoute(target);
268         final HttpContext context = new BasicHttpContext();
269 
270         final Log log = Mockito.mock(Log.class);
271         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
272         final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry);
273 
274         Mockito.when(conn.getIOSession()).thenReturn(iosession);
275         Mockito.when(sslStrategy.upgrade(target, iosession)).thenReturn(iosession);
276 
277         connman.startRoute(managedConn, route, context);
278 
279         Mockito.verify(noopStrategy, Mockito.never()).upgrade(target, iosession);
280         Mockito.verify(conn, Mockito.never()).bind(iosession);
281 
282         Assert.assertFalse(connman.isRouteComplete(managedConn));
283     }
284 
285     @Test
286     public void testConnectionInitializeHttps() throws Exception {
287         final HttpHost target = new HttpHost("somehost", -1, "https");
288         final HttpRoute route = new HttpRoute(target, null, true);
289         final HttpContext context = new BasicHttpContext();
290 
291         final Log log = Mockito.mock(Log.class);
292         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
293         poolentry.markRouteComplete();
294         final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry);
295 
296         Mockito.when(conn.getIOSession()).thenReturn(iosession);
297         Mockito.when(sslStrategy.upgrade(target, iosession)).thenReturn(iosession);
298 
299         connman.startRoute(managedConn, route, context);
300 
301         Mockito.verify(sslStrategy).upgrade(target, iosession);
302         Mockito.verify(conn).bind(iosession);
303     }
304 
305     @Test
306     public void testConnectionInitializeContextSpecific() throws Exception {
307         final HttpHost target = new HttpHost("somehost", -1, "http11");
308         final HttpRoute route = new HttpRoute(target);
309         final HttpContext context = new BasicHttpContext();
310 
311         final Registry<SchemeIOSessionStrategy> reg = RegistryBuilder.<SchemeIOSessionStrategy>create()
312                 .register("http11", noopStrategy)
313                 .build();
314         context.setAttribute(PoolingNHttpClientConnectionManager.IOSESSION_FACTORY_REGISTRY, reg);
315 
316         final Log log = Mockito.mock(Log.class);
317         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
318         final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry);
319 
320         Mockito.when(conn.getIOSession()).thenReturn(iosession);
321         Mockito.when(sslStrategy.upgrade(target, iosession)).thenReturn(iosession);
322 
323         connman.startRoute(managedConn, route, context);
324 
325         Mockito.verify(noopStrategy, Mockito.never()).upgrade(target, iosession);
326         Mockito.verify(conn, Mockito.never()).bind(iosession);
327 
328         Assert.assertFalse(connman.isRouteComplete(managedConn));
329     }
330 
331     @Test(expected=UnsupportedSchemeException.class)
332     public void testConnectionInitializeUnknownScheme() throws Exception {
333         final HttpHost target = new HttpHost("somehost", -1, "whatever");
334         final HttpRoute route = new HttpRoute(target, null, true);
335         final HttpContext context = new BasicHttpContext();
336 
337         final Log log = Mockito.mock(Log.class);
338         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
339         poolentry.markRouteComplete();
340         final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry);
341 
342         Mockito.when(conn.getIOSession()).thenReturn(iosession);
343         Mockito.when(sslStrategy.upgrade(target, iosession)).thenReturn(iosession);
344 
345         connman.startRoute(managedConn, route, context);
346     }
347 
348     @Test
349     public void testConnectionUpgrade() throws Exception {
350         final HttpHost target = new HttpHost("somehost", -1, "https");
351         final HttpRoute route = new HttpRoute(target);
352         final HttpContext context = new BasicHttpContext();
353 
354         final Log log = Mockito.mock(Log.class);
355         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
356         poolentry.markRouteComplete();
357         final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry);
358 
359         Mockito.when(conn.getIOSession()).thenReturn(iosession);
360         Mockito.when(sslStrategy.upgrade(target, iosession)).thenReturn(iosession);
361 
362         connman.upgrade(managedConn, route, context);
363 
364         Mockito.verify(sslStrategy).upgrade(target, iosession);
365         Mockito.verify(conn).bind(iosession);
366     }
367 
368     @Test(expected=UnsupportedSchemeException.class)
369     public void testConnectionUpgradeUnknownScheme() throws Exception {
370         final HttpHost target = new HttpHost("somehost", -1, "whatever");
371         final HttpRoute route = new HttpRoute(target);
372         final HttpContext context = new BasicHttpContext();
373 
374         final Log log = Mockito.mock(Log.class);
375         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
376         poolentry.markRouteComplete();
377         final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry);
378 
379         Mockito.when(conn.getIOSession()).thenReturn(iosession);
380         Mockito.when(sslStrategy.upgrade(target, iosession)).thenReturn(iosession);
381 
382         connman.upgrade(managedConn, route, context);
383     }
384 
385     @Test(expected=UnsupportedSchemeException.class)
386     public void testConnectionUpgradeIllegalScheme() throws Exception {
387         final HttpHost target = new HttpHost("somehost", -1, "http");
388         final HttpRoute route = new HttpRoute(target);
389         final HttpContext context = new BasicHttpContext();
390 
391         final Log log = Mockito.mock(Log.class);
392         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
393         poolentry.markRouteComplete();
394         final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry);
395 
396         Mockito.when(conn.getIOSession()).thenReturn(iosession);
397         Mockito.when(sslStrategy.upgrade(target, iosession)).thenReturn(iosession);
398 
399         connman.upgrade(managedConn, route, context);
400     }
401 
402     @Test
403     public void testConnectionRouteComplete() throws Exception {
404         final HttpHost target = new HttpHost("somehost", -1, "http");
405         final HttpRoute route = new HttpRoute(target);
406         final HttpContext context = new BasicHttpContext();
407 
408         final Log log = Mockito.mock(Log.class);
409         final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS);
410         poolentry.markRouteComplete();
411         final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry);
412 
413         Mockito.when(conn.getIOSession()).thenReturn(iosession);
414         Mockito.when(sslStrategy.upgrade(target, iosession)).thenReturn(iosession);
415 
416         connman.startRoute(managedConn, route, context);
417         connman.routeComplete(managedConn, route, context);
418 
419         Assert.assertTrue(connman.isRouteComplete(managedConn));
420     }
421 
422     @Test
423     public void testDelegationToCPool() throws Exception {
424         connman.closeExpiredConnections();
425         Mockito.verify(pool).closeExpired();
426 
427         connman.closeIdleConnections(3, TimeUnit.SECONDS);
428         Mockito.verify(pool).closeIdle(3, TimeUnit.SECONDS);
429 
430         connman.getMaxTotal();
431         Mockito.verify(pool).getMaxTotal();
432 
433         connman.getDefaultMaxPerRoute();
434         Mockito.verify(pool).getDefaultMaxPerRoute();
435 
436         final HttpRoute route = new HttpRoute(new HttpHost("somehost"));
437         connman.getMaxPerRoute(route);
438         Mockito.verify(pool).getMaxPerRoute(route);
439 
440         connman.setMaxTotal(200);
441         Mockito.verify(pool).setMaxTotal(200);
442 
443         connman.setDefaultMaxPerRoute(100);
444         Mockito.verify(pool).setDefaultMaxPerRoute(100);
445 
446         connman.setMaxPerRoute(route, 150);
447         Mockito.verify(pool).setMaxPerRoute(route, 150);
448 
449         connman.getTotalStats();
450         Mockito.verify(pool).getTotalStats();
451 
452         connman.getStats(route);
453         Mockito.verify(pool).getStats(route);
454     }
455 
456     @Test
457     public void testInternalConnFactoryCreate() throws Exception {
458         final ConfigData configData = new ConfigData();
459         final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory(
460             configData, connFactory);
461 
462         final HttpRoute route = new HttpRoute(new HttpHost("somehost"));
463         internalConnFactory.create(route, iosession);
464 
465         Mockito.verify(sslStrategy, Mockito.never()).upgrade(Mockito.eq(new HttpHost("somehost")),
466                 Mockito.<IOSession>any());
467         Mockito.verify(connFactory).create(Mockito.same(iosession), Mockito.<ConnectionConfig>any());
468     }
469 
470     @Test
471     public void testInternalConnFactoryCreateViaProxy() throws Exception {
472         final ConfigData configData = new ConfigData();
473         final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory(
474             configData, connFactory);
475 
476         final HttpHost target = new HttpHost("somehost");
477         final HttpHost proxy = new HttpHost("someproxy", 8888);
478         final HttpRoute route = new HttpRoute(target, null, proxy, false);
479 
480         final ConnectionConfig config = ConnectionConfig.custom().build();
481         configData.setConnectionConfig(proxy, config);
482 
483         internalConnFactory.create(route, iosession);
484 
485         Mockito.verify(connFactory).create(iosession, config);
486     }
487 
488     @Test
489     public void testInternalConnFactoryCreateDirect() throws Exception {
490         final ConfigData configData = new ConfigData();
491         final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory(
492             configData, connFactory);
493 
494         final HttpHost target = new HttpHost("somehost");
495         final HttpRoute route = new HttpRoute(target);
496 
497         final ConnectionConfig config = ConnectionConfig.custom().build();
498         configData.setConnectionConfig(target, config);
499 
500         internalConnFactory.create(route, iosession);
501 
502         Mockito.verify(connFactory).create(iosession, config);
503     }
504 
505     @Test
506     public void testInternalConnFactoryCreateDefaultConfig() throws Exception {
507         final ConfigData configData = new ConfigData();
508         final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory(
509             configData, connFactory);
510 
511         final HttpHost target = new HttpHost("somehost");
512         final HttpRoute route = new HttpRoute(target);
513 
514         final ConnectionConfig config = ConnectionConfig.custom().build();
515         configData.setDefaultConnectionConfig(config);
516 
517         internalConnFactory.create(route, iosession);
518 
519         Mockito.verify(connFactory).create(iosession, config);
520     }
521 
522     @Test
523     public void testInternalConnFactoryCreateGlobalDefaultConfig() throws Exception {
524         final ConfigData configData = new ConfigData();
525         final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory(
526             configData, connFactory);
527 
528         final HttpHost target = new HttpHost("somehost");
529         final HttpRoute route = new HttpRoute(target);
530 
531         configData.setDefaultConnectionConfig(null);
532 
533         internalConnFactory.create(route, iosession);
534 
535         Mockito.verify(connFactory).create(iosession, ConnectionConfig.DEFAULT);
536     }
537 
538     @Test
539     public void testResolveLocalAddress() throws Exception {
540         final InternalAddressResolver addressResolver = new InternalAddressResolver(
541                 schemePortResolver, dnsResolver);
542 
543         final HttpHost target = new HttpHost("localhost");
544         final byte[] ip = new byte[] {10, 0, 0, 10};
545         final HttpRoute route = new HttpRoute(target, InetAddress.getByAddress(ip), false);
546         final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveLocalAddress(route);
547 
548         Assert.assertNotNull(address);
549         Assert.assertEquals(InetAddress.getByAddress(ip), address.getAddress());
550         Assert.assertEquals(0, address.getPort());
551     }
552 
553     @Test
554     public void testResolveLocalAddressNull() throws Exception {
555         final InternalAddressResolver addressResolver = new InternalAddressResolver(
556                 schemePortResolver, dnsResolver);
557 
558         final HttpHost target = new HttpHost("localhost");
559         final HttpRoute route = new HttpRoute(target);
560         final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveLocalAddress(route);
561 
562         Assert.assertNull(address);
563     }
564 
565     @Test
566     public void testResolveRemoteAddress() throws Exception {
567         final InternalAddressResolver addressResolver = new InternalAddressResolver(
568                 schemePortResolver, dnsResolver);
569 
570         final HttpHost target = new HttpHost("somehost");
571         final HttpRoute route = new HttpRoute(target);
572 
573         Mockito.when(schemePortResolver.resolve(target)).thenReturn(123);
574         final byte[] ip = new byte[] {10, 0, 0, 10};
575         Mockito.when(dnsResolver.resolve("somehost")).thenReturn(new InetAddress[] {InetAddress.getByAddress(ip)});
576 
577         final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveRemoteAddress(route);
578 
579         Assert.assertNotNull(address);
580         Assert.assertEquals(InetAddress.getByAddress(ip), address.getAddress());
581         Assert.assertEquals(123, address.getPort());
582     }
583 
584     @Test
585     public void testResolveRemoteAddressViaProxy() throws Exception {
586         final InternalAddressResolver addressResolver = new InternalAddressResolver(
587                 schemePortResolver, dnsResolver);
588 
589         final HttpHost target = new HttpHost("somehost");
590         final HttpHost proxy = new HttpHost("someproxy");
591         final HttpRoute route = new HttpRoute(target, null, proxy, false);
592 
593         Mockito.when(schemePortResolver.resolve(proxy)).thenReturn(8888);
594         final byte[] ip = new byte[] {10, 0, 0, 10};
595         Mockito.when(dnsResolver.resolve("someproxy")).thenReturn(new InetAddress[] {InetAddress.getByAddress(ip)});
596 
597         final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveRemoteAddress(route);
598 
599         Assert.assertNotNull(address);
600         Assert.assertEquals(InetAddress.getByAddress(ip), address.getAddress());
601         Assert.assertEquals(8888, address.getPort());
602     }
603 
604 }