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 org.apache.http.HttpHost;
31  import org.apache.http.HttpRequest;
32  import org.apache.http.HttpVersion;
33  import org.apache.http.ProtocolException;
34  import org.apache.http.client.config.RequestConfig;
35  import org.apache.http.client.protocol.HttpClientContext;
36  import org.apache.http.conn.SchemePortResolver;
37  import org.apache.http.conn.routing.HttpRoute;
38  import org.apache.http.message.BasicHttpRequest;
39  import org.apache.http.protocol.BasicHttpContext;
40  import org.apache.http.protocol.HttpContext;
41  import org.junit.Assert;
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.mockito.Mockito;
45  
46  /**
47   * Tests for {@link DefaultRoutePlanner}.
48   */
49  @SuppressWarnings({"boxing","static-access"}) // test code
50  public class TestDefaultRoutePlanner {
51  
52      private SchemePortResolver schemePortResolver;
53      private DefaultRoutePlanner routePlanner;
54  
55      @Before
56      public void setup() {
57          schemePortResolver = Mockito.mock(SchemePortResolver.class);
58          routePlanner = new DefaultRoutePlanner(schemePortResolver);
59      }
60  
61      @Test
62      public void testDirect() throws Exception {
63          final HttpHost target = new HttpHost("somehost", 80, "http");
64          final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
65  
66          final HttpContext context = new BasicHttpContext();
67          final HttpRoute route = routePlanner.determineRoute(target, request, context);
68  
69          Assert.assertEquals(target, route.getTargetHost());
70          Assert.assertEquals(1, route.getHopCount());
71          Assert.assertFalse(route.isSecure());
72          Mockito.verify(schemePortResolver, Mockito.never()).resolve(Mockito.<HttpHost>any());
73      }
74  
75      @Test
76      public void testDirectDefaultPort() throws Exception {
77          final HttpHost target = new HttpHost("somehost", -1, "https");
78          Mockito.when(schemePortResolver.resolve(target)).thenReturn(443);
79          final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
80  
81          final HttpContext context = new BasicHttpContext();
82          final HttpRoute route = routePlanner.determineRoute(target, request, context);
83  
84          Assert.assertEquals(new HttpHost("somehost", 443, "https"), route.getTargetHost());
85          Assert.assertEquals(1, route.getHopCount());
86          Assert.assertTrue(route.isSecure());
87      }
88  
89      @Test
90      public void testViaProxy() throws Exception {
91          final HttpHost target = new HttpHost("somehost", 80, "http");
92          final HttpHost proxy = new HttpHost("proxy", 8080);
93          final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
94  
95          final HttpClientContext context = HttpClientContext.create();
96          context.setRequestConfig(RequestConfig.custom().setProxy(proxy).build());
97          final HttpRoute route = routePlanner.determineRoute(target, request, context);
98  
99          Assert.assertEquals(target, route.getTargetHost());
100         Assert.assertEquals(proxy, route.getProxyHost());
101         Assert.assertEquals(2, route.getHopCount());
102         Assert.assertFalse(route.isSecure());
103         Mockito.verify(schemePortResolver, Mockito.never()).resolve(Mockito.<HttpHost>any());
104     }
105 
106     @Test(expected= ProtocolException.class)
107     public void testNullTarget() throws Exception {
108         final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
109 
110         final HttpContext context = new BasicHttpContext();
111         routePlanner.determineRoute(null, request, context);
112     }
113 
114 }