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