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 @SuppressWarnings({"static-access"})
61 public class TestInternalHttpClient {
62
63 @Mock
64 private HttpClientConnectionManager connManager;
65 @Mock
66 private HttpRequestExecutor requestExecutor;
67 @Mock
68 private ExecChainHandler execChain;
69 @Mock
70 private HttpRoutePlanner routePlanner;
71 @Mock
72 private Lookup<CookieSpecFactory> cookieSpecRegistry;
73 @Mock
74 private Lookup<AuthSchemeFactory> authSchemeRegistry;
75 @Mock
76 private CookieStore cookieStore;
77 @Mock
78 private CredentialsProvider credentialsProvider;
79 @Mock
80 private RequestConfig defaultConfig;
81 @Mock
82 private Closeable closeable1;
83 @Mock
84 private Closeable closeable2;
85
86 private InternalHttpClient client;
87
88 @BeforeEach
89 public void setup() throws Exception {
90 MockitoAnnotations.openMocks(this);
91 client = new InternalHttpClient(connManager, requestExecutor, new ExecChainElement(execChain, null), routePlanner,
92 cookieSpecRegistry, authSchemeRegistry, cookieStore, credentialsProvider,
93 defaultConfig, Arrays.asList(closeable1, closeable2));
94
95 }
96
97 @Test
98 public void testExecute() throws Exception {
99 final HttpGet httpget = new HttpGet("http://somehost/stuff");
100 final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
101
102 Mockito.when(routePlanner.determineRoute(
103 Mockito.eq(new HttpHost("somehost")),
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 public 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.<HttpClientContext>any())).thenReturn(route);
125 Mockito.when(execChain.execute(
126 Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(
127 CloseableHttpResponse.adapt(new BasicClassicHttpResponse(200)));
128 Mockito.when(execChain.execute(
129 Mockito.any(),
130 Mockito.any(),
131 Mockito.any())).thenThrow(new HttpException());
132
133 Assertions.assertThrows(ClientProtocolException.class, () ->
134 client.execute(httpget, response -> null));
135 }
136
137 @Test
138 public void testExecuteDefaultContext() throws Exception {
139 final HttpGet httpget = new HttpGet("http://somehost/stuff");
140 final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
141
142 Mockito.when(routePlanner.determineRoute(
143 Mockito.eq(new HttpHost("somehost")),
144 Mockito.<HttpClientContext>any())).thenReturn(route);
145 Mockito.when(execChain.execute(
146 Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(
147 CloseableHttpResponse.adapt(new BasicClassicHttpResponse(200)));
148
149 final HttpClientContext context = HttpClientContext.create();
150 client.execute(httpget, context, response -> null);
151
152 Assertions.assertSame(cookieSpecRegistry, context.getCookieSpecRegistry());
153 Assertions.assertSame(authSchemeRegistry, context.getAuthSchemeRegistry());
154 Assertions.assertSame(cookieStore, context.getCookieStore());
155 Assertions.assertSame(credentialsProvider, context.getCredentialsProvider());
156 Assertions.assertSame(defaultConfig, context.getRequestConfig());
157 }
158
159 @Test
160 public void testExecuteRequestConfig() throws Exception {
161 final HttpGet httpget = new HttpGet("http://somehost/stuff");
162 final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
163
164 Mockito.when(routePlanner.determineRoute(
165 Mockito.eq(new HttpHost("somehost")),
166 Mockito.<HttpClientContext>any())).thenReturn(route);
167 Mockito.when(execChain.execute(
168 Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(
169 CloseableHttpResponse.adapt(new BasicClassicHttpResponse(200)));
170
171 final RequestConfig config = RequestConfig.custom().build();
172 httpget.setConfig(config);
173 final HttpClientContext context = HttpClientContext.create();
174 client.execute(httpget, context, response -> null);
175
176 Assertions.assertSame(config, context.getRequestConfig());
177 }
178
179 @Test
180 public void testExecuteLocalContext() throws Exception {
181 final HttpGet httpget = new HttpGet("http://somehost/stuff");
182 final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80));
183
184 Mockito.when(routePlanner.determineRoute(
185 Mockito.eq(new HttpHost("somehost")),
186 Mockito.<HttpClientContext>any())).thenReturn(route);
187 Mockito.when(execChain.execute(
188 Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(
189 CloseableHttpResponse.adapt(new BasicClassicHttpResponse(200)));
190
191 final HttpClientContext context = HttpClientContext.create();
192
193 final Lookup<CookieSpecFactory> localCookieSpecRegistry = Mockito.mock(Lookup.class);
194 final Lookup<AuthSchemeFactory> localAuthSchemeRegistry = Mockito.mock(Lookup.class);
195 final CookieStore localCookieStore = Mockito.mock(CookieStore.class);
196 final CredentialsProvider localCredentialsProvider = Mockito.mock(CredentialsProvider.class);
197 final RequestConfig localConfig = RequestConfig.custom().build();
198
199 context.setCookieSpecRegistry(localCookieSpecRegistry);
200 context.setAuthSchemeRegistry(localAuthSchemeRegistry);
201 context.setCookieStore(localCookieStore);
202 context.setCredentialsProvider(localCredentialsProvider);
203 context.setRequestConfig(localConfig);
204
205 client.execute(httpget, context, response -> null);
206
207 Assertions.assertSame(localCookieSpecRegistry, context.getCookieSpecRegistry());
208 Assertions.assertSame(localAuthSchemeRegistry, context.getAuthSchemeRegistry());
209 Assertions.assertSame(localCookieStore, context.getCookieStore());
210 Assertions.assertSame(localCredentialsProvider, context.getCredentialsProvider());
211 Assertions.assertSame(localConfig, context.getRequestConfig());
212 }
213
214 @Test
215 public void testClientClose() throws Exception {
216 client.close();
217
218 Mockito.verify(closeable1).close();
219 Mockito.verify(closeable2).close();
220 }
221
222 @Test
223 public void testClientCloseIOException() throws Exception {
224 Mockito.doThrow(new IOException()).when(closeable1).close();
225
226 client.close();
227
228 Mockito.verify(closeable1).close();
229 Mockito.verify(closeable2).close();
230 }
231
232 }