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