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.hc.client5.http.impl.classic;
28  
29  import java.io.Closeable;
30  import java.io.IOException;
31  import java.util.Arrays;
32  
33  import org.apache.hc.client5.http.ClientProtocolException;
34  import org.apache.hc.client5.http.HttpRoute;
35  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
36  import org.apache.hc.client5.http.auth.CredentialsProvider;
37  import org.apache.hc.client5.http.classic.ExecChain;
38  import org.apache.hc.client5.http.classic.ExecChainHandler;
39  import org.apache.hc.client5.http.classic.methods.HttpGet;
40  import org.apache.hc.client5.http.config.RequestConfig;
41  import org.apache.hc.client5.http.cookie.CookieSpecFactory;
42  import org.apache.hc.client5.http.cookie.CookieStore;
43  import org.apache.hc.client5.http.io.HttpClientConnectionManager;
44  import org.apache.hc.client5.http.protocol.HttpClientContext;
45  import org.apache.hc.client5.http.routing.HttpRoutePlanner;
46  import org.apache.hc.core5.http.ClassicHttpRequest;
47  import org.apache.hc.core5.http.HttpException;
48  import org.apache.hc.core5.http.HttpHost;
49  import org.apache.hc.core5.http.config.Lookup;
50  import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
51  import org.junit.Assert;
52  import org.junit.Before;
53  import org.junit.Test;
54  import org.mockito.Mock;
55  import org.mockito.Mockito;
56  import org.mockito.MockitoAnnotations;
57  
58  /**
59   *  Simple tests for {@link InternalHttpClient}.
60   */
61  @SuppressWarnings({"static-access"}) // test code
62  public class TestInternalHttpClient {
63  
64      @Mock
65      private HttpClientConnectionManager connManager;
66      @Mock
67      private HttpRequestExecutor requestExecutor;
68      @Mock
69      private ExecChainHandler execChain;
70      @Mock
71      private HttpRoutePlanner routePlanner;
72      @Mock
73      private Lookup<CookieSpecFactory> cookieSpecRegistry;
74      @Mock
75      private Lookup<AuthSchemeFactory> authSchemeRegistry;
76      @Mock
77      private CookieStore cookieStore;
78      @Mock
79      private CredentialsProvider credentialsProvider;
80      @Mock
81      private RequestConfig defaultConfig;
82      @Mock
83      private Closeable closeable1;
84      @Mock
85      private Closeable closeable2;
86  
87      private InternalHttpClient client;
88  
89      @Before
90      public void setup() throws Exception {
91          MockitoAnnotations.initMocks(this);
92          client = new InternalHttpClient(connManager, requestExecutor, new ExecChainElement(execChain, null), routePlanner,
93                  cookieSpecRegistry, authSchemeRegistry, cookieStore, credentialsProvider,
94                  defaultConfig, Arrays.asList(closeable1, closeable2));
95  
96      }
97  
98      @Test
99      public void testExecute() throws Exception {
100         final HttpGet httpget = new HttpGet("http://somehost/stuff");
101         final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
102 
103         Mockito.when(routePlanner.determineRoute(
104                 Mockito.eq(new HttpHost("somehost")),
105                 Mockito.<HttpClientContext>any())).thenReturn(route);
106 
107         client.execute(httpget);
108 
109         Mockito.verify(execChain).execute(
110                 Mockito.<ClassicHttpRequest>any(),
111                 Mockito.<ExecChain.Scope>any(),
112                 Mockito.<ExecChain>any());
113     }
114 
115     @Test(expected=ClientProtocolException.class)
116     public void testExecuteHttpException() throws Exception {
117         final HttpGet httpget = new HttpGet("http://somehost/stuff");
118         final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
119 
120         Mockito.when(routePlanner.determineRoute(
121                 Mockito.eq(new HttpHost("somehost")),
122                 Mockito.<HttpClientContext>any())).thenReturn(route);
123         Mockito.when(execChain.execute(
124                 Mockito.<ClassicHttpRequest>any(),
125                 Mockito.<ExecChain.Scope>any(),
126                 Mockito.<ExecChain>any())).thenThrow(new HttpException());
127 
128         client.execute(httpget);
129     }
130 
131     @Test
132     public void testExecuteDefaultContext() throws Exception {
133         final HttpGet httpget = new HttpGet("http://somehost/stuff");
134         final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
135 
136         Mockito.when(routePlanner.determineRoute(
137                 Mockito.eq(new HttpHost("somehost")),
138                 Mockito.<HttpClientContext>any())).thenReturn(route);
139 
140         final HttpClientContext context = HttpClientContext.create();
141         client.execute(httpget, context);
142 
143         Assert.assertSame(cookieSpecRegistry, context.getCookieSpecRegistry());
144         Assert.assertSame(authSchemeRegistry, context.getAuthSchemeRegistry());
145         Assert.assertSame(cookieStore, context.getCookieStore());
146         Assert.assertSame(credentialsProvider, context.getCredentialsProvider());
147         Assert.assertSame(defaultConfig, context.getRequestConfig());
148     }
149 
150     @Test
151     public void testExecuteRequestConfig() throws Exception {
152         final HttpGet httpget = new HttpGet("http://somehost/stuff");
153         final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
154 
155         Mockito.when(routePlanner.determineRoute(
156                 Mockito.eq(new HttpHost("somehost")),
157                 Mockito.<HttpClientContext>any())).thenReturn(route);
158 
159         final RequestConfig config = RequestConfig.custom().build();
160         httpget.setConfig(config);
161         final HttpClientContext context = HttpClientContext.create();
162         client.execute(httpget, context);
163 
164         Assert.assertSame(config, context.getRequestConfig());
165     }
166 
167     @Test
168     public void testExecuteLocalContext() throws Exception {
169         final HttpGet httpget = new HttpGet("http://somehost/stuff");
170         final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
171 
172         Mockito.when(routePlanner.determineRoute(
173                 Mockito.eq(new HttpHost("somehost")),
174                 Mockito.<HttpClientContext>any())).thenReturn(route);
175 
176         final HttpClientContext context = HttpClientContext.create();
177 
178         final Lookup<CookieSpecFactory> localCookieSpecRegistry = Mockito.mock(Lookup.class);
179         final Lookup<AuthSchemeFactory> localAuthSchemeRegistry = Mockito.mock(Lookup.class);
180         final CookieStore localCookieStore = Mockito.mock(CookieStore.class);
181         final CredentialsProvider localCredentialsProvider = Mockito.mock(CredentialsProvider.class);
182         final RequestConfig localConfig = RequestConfig.custom().build();
183 
184         context.setCookieSpecRegistry(localCookieSpecRegistry);
185         context.setAuthSchemeRegistry(localAuthSchemeRegistry);
186         context.setCookieStore(localCookieStore);
187         context.setCredentialsProvider(localCredentialsProvider);
188         context.setRequestConfig(localConfig);
189 
190         client.execute(httpget, context);
191 
192         Assert.assertSame(localCookieSpecRegistry, context.getCookieSpecRegistry());
193         Assert.assertSame(localAuthSchemeRegistry, context.getAuthSchemeRegistry());
194         Assert.assertSame(localCookieStore, context.getCookieStore());
195         Assert.assertSame(localCredentialsProvider, context.getCredentialsProvider());
196         Assert.assertSame(localConfig, context.getRequestConfig());
197     }
198 
199     @Test
200     public void testClientClose() throws Exception {
201         client.close();
202 
203         Mockito.verify(closeable1).close();
204         Mockito.verify(closeable2).close();
205     }
206 
207     @Test
208     public void testClientCloseIOException() throws Exception {
209         Mockito.doThrow(new IOException()).when(closeable1).close();
210 
211         client.close();
212 
213         Mockito.verify(closeable1).close();
214         Mockito.verify(closeable2).close();
215     }
216 
217 }