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