1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.impl.routing;
29
30 import java.net.InetAddress;
31 import java.net.InetSocketAddress;
32 import java.net.Proxy;
33 import java.net.ProxySelector;
34 import java.net.URI;
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import org.apache.hc.client5.http.HttpRoute;
39 import org.apache.hc.client5.http.SchemePortResolver;
40 import org.apache.hc.core5.http.HttpHost;
41 import org.apache.hc.core5.http.protocol.BasicHttpContext;
42 import org.apache.hc.core5.http.protocol.HttpContext;
43 import org.junit.jupiter.api.Assertions;
44 import org.junit.jupiter.api.BeforeEach;
45 import org.junit.jupiter.api.Test;
46 import org.mockito.ArgumentMatchers;
47 import org.mockito.Mockito;
48
49
50
51
52 @SuppressWarnings("boxing")
53 public class TestSystemDefaultRoutePlanner {
54
55 private SchemePortResolver schemePortResolver;
56 private ProxySelector proxySelector;
57 private SystemDefaultRoutePlanner routePlanner;
58
59 @BeforeEach
60 public void setup() {
61 schemePortResolver = Mockito.mock(SchemePortResolver.class);
62 proxySelector = Mockito.mock(ProxySelector.class);
63 routePlanner = new SystemDefaultRoutePlanner(schemePortResolver, proxySelector);
64 }
65
66 @Test
67 public void testDirect() throws Exception {
68 final HttpHost target = new HttpHost("http", "somehost", 80);
69
70 final HttpContext context = new BasicHttpContext();
71 final HttpRoute route = routePlanner.determineRoute(target, context);
72
73 Assertions.assertEquals(target, route.getTargetHost());
74 Assertions.assertEquals(1, route.getHopCount());
75 Assertions.assertFalse(route.isSecure());
76 Mockito.verify(schemePortResolver, Mockito.never()).resolve(ArgumentMatchers.any());
77 }
78
79 @Test
80 public void testDirectDefaultPort() throws Exception {
81 final HttpHost target = new HttpHost("https", "somehost", -1);
82 Mockito.when(schemePortResolver.resolve(target)).thenReturn(443);
83
84 final HttpContext context = new BasicHttpContext();
85 final HttpRoute route = routePlanner.determineRoute(target, context);
86
87 Assertions.assertEquals(new HttpHost("https", "somehost", 443), route.getTargetHost());
88 Assertions.assertEquals(1, route.getHopCount());
89 Assertions.assertTrue(route.isSecure());
90 }
91
92 @Test
93 public void testProxy() throws Exception {
94
95 final InetAddress ia = InetAddress.getByAddress(new byte[] {
96 (byte)127, (byte)0, (byte)0, (byte)1
97 });
98 final InetSocketAddress isa1 = new InetSocketAddress(ia, 11111);
99 final InetSocketAddress isa2 = new InetSocketAddress(ia, 22222);
100
101 final List<Proxy> proxies = new ArrayList<>(2);
102 proxies.add(new Proxy(Proxy.Type.HTTP, isa1));
103 proxies.add(new Proxy(Proxy.Type.HTTP, isa2));
104
105 Mockito.when(proxySelector.select(new URI("http://somehost:80"))).thenReturn(proxies);
106
107 final HttpHost target = new HttpHost("http", "somehost", 80);
108
109 final HttpContext context = new BasicHttpContext();
110 final HttpRoute route = routePlanner.determineRoute(target, context);
111
112 Assertions.assertEquals(target, route.getTargetHost());
113 Assertions.assertEquals(2, route.getHopCount());
114 Assertions.assertEquals(isa1.getPort(), route.getProxyHost().getPort());
115 }
116
117 }