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  
28  package org.apache.http.impl.conn;
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.http.HttpHost;
39  import org.apache.http.HttpRequest;
40  import org.apache.http.HttpVersion;
41  import org.apache.http.conn.SchemePortResolver;
42  import org.apache.http.conn.routing.HttpRoute;
43  import org.apache.http.message.BasicHttpRequest;
44  import org.apache.http.protocol.BasicHttpContext;
45  import org.apache.http.protocol.HttpContext;
46  import org.junit.Assert;
47  import org.junit.Before;
48  import org.junit.Test;
49  import org.mockito.Matchers;
50  import org.mockito.Mockito;
51  
52  /**
53   * Tests for {@link SystemDefaultRoutePlanner}.
54   */
55  @SuppressWarnings("boxing") // test code
56  public class TestSystemDefaultRoutePlanner {
57  
58      private SchemePortResolver schemePortResolver;
59      private ProxySelector proxySelector;
60      private SystemDefaultRoutePlanner routePlanner;
61  
62      @Before
63      public void setup() {
64          schemePortResolver = Mockito.mock(SchemePortResolver.class);
65          proxySelector = Mockito.mock(ProxySelector.class);
66          routePlanner = new SystemDefaultRoutePlanner(schemePortResolver, proxySelector);
67      }
68  
69      @Test
70      public void testDirect() throws Exception {
71          final HttpHost target = new HttpHost("somehost", 80, "http");
72          final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
73  
74          final HttpContext context = new BasicHttpContext();
75          final HttpRoute route = routePlanner.determineRoute(target, request, context);
76  
77          Assert.assertEquals(target, route.getTargetHost());
78          Assert.assertEquals(1, route.getHopCount());
79          Assert.assertFalse(route.isSecure());
80          Mockito.verify(schemePortResolver, Mockito.never()).resolve(Matchers.<HttpHost>any());
81      }
82  
83      @Test
84      public void testDirectDefaultPort() throws Exception {
85          final HttpHost target = new HttpHost("somehost", -1, "https");
86          Mockito.when(schemePortResolver.resolve(target)).thenReturn(443);
87          final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
88  
89          final HttpContext context = new BasicHttpContext();
90          final HttpRoute route = routePlanner.determineRoute(target, request, context);
91  
92          Assert.assertEquals(new HttpHost("somehost", 443, "https"), route.getTargetHost());
93          Assert.assertEquals(1, route.getHopCount());
94          Assert.assertTrue(route.isSecure());
95      }
96  
97      @Test
98      public void testProxy() throws Exception {
99  
100         final InetAddress ia = InetAddress.getByAddress(new byte[] {
101             (byte)127, (byte)0, (byte)0, (byte)1
102         });
103         final InetSocketAddress isa1 = new InetSocketAddress(ia, 11111);
104         final InetSocketAddress isa2 = new InetSocketAddress(ia, 22222);
105 
106         final List<Proxy> proxies = new ArrayList<Proxy>(2);
107         proxies.add(new Proxy(Proxy.Type.HTTP, isa1));
108         proxies.add(new Proxy(Proxy.Type.HTTP, isa2));
109 
110         Mockito.when(proxySelector.select(new URI("http://somehost:80"))).thenReturn(proxies);
111 
112         final HttpHost target = new HttpHost("somehost", 80, "http");
113         final HttpRequest request =
114             new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
115 
116         final HttpContext context = new BasicHttpContext();
117         final HttpRoute route = routePlanner.determineRoute(target, request, context);
118 
119         Assert.assertEquals(target, route.getTargetHost());
120         Assert.assertEquals(2, route.getHopCount());
121         Assert.assertEquals(isa1.getPort(), route.getProxyHost().getPort());
122     }
123 
124 }