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
28
29
30
31
32
33
34 package fr.paris.lutece.util.httpaccess;
35
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertFalse;
38
39 import java.io.IOException;
40 import java.io.UnsupportedEncodingException;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 import org.apache.commons.fileupload.FileItem;
48 import org.apache.log4j.Logger;
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Test;
52
53 import com.fasterxml.jackson.core.JsonProcessingException;
54 import com.fasterxml.jackson.databind.JsonMappingException;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56
57 import okhttp3.mockwebserver.Dispatcher;
58 import okhttp3.mockwebserver.MockResponse;
59 import okhttp3.mockwebserver.MockWebServer;
60 import okhttp3.mockwebserver.RecordedRequest;
61
62
63
64
65 public class HttpAccessTest {
66
67 private MockWebServer mockWebServer;
68 private ObjectMapper _objectMapper = new ObjectMapper();
69 private Logger _logger = Logger.getLogger(this.getClass());
70
71 @Test
72 public void testDoGet() throws IOException, HttpAccessException {
73
74 String strUrlTestHttp = mockWebServer.url("/test?param1=1&parma2=2").toString();
75 HttpClientConfiguration configuration = new HttpClientConfiguration();
76 configuration.setConnectionTimeout(10000);
77 configuration.setSocketTimeout(10000);
78
79
80
81
82 Map<String, String> mapHeaders = new HashMap<String, String>();
83 Map<String, String> mapHeadersResponse = new HashMap<String, String>();
84
85 mapHeaders.put("Authorization", " Basic token");
86
87 HttpAccessService httpAccessService = new HttpAccessService(configuration);
88 HttpAccess httpAccess = new HttpAccess(httpAccessService, new MockResponseStatusValidator());
89
90 String strTest = httpAccess.doGet(strUrlTestHttp, null, null, mapHeaders, mapHeadersResponse);
91
92 HttpRequestResult jsonRespone = _objectMapper.readValue(strTest, HttpRequestResult.class);
93 assertEquals("GET", jsonRespone.getMethodName());
94 _logger.debug(strTest);
95
96 }
97
98 @Test
99 public void testDoDelete() throws IOException, HttpAccessException {
100
101 String strUrlTestHttp = mockWebServer.url("test?id_delete=125").toString();
102
103 HttpClientConfiguration configuration = new HttpClientConfiguration();
104 configuration.setConnectionTimeout(1000);
105 configuration.setSocketTimeout(10000);
106
107 configuration.setNoProxyFor("*.paris.mdp");
108 configuration.setProxyPort("8080");
109
110 Map<String, String> mapHeaders = new HashMap<String, String>();
111 Map<String, String> mapHeadersResponse = new HashMap<String, String>();
112
113 mapHeaders.put("Authorization", " Basic token");
114
115 HttpAccessService httpAccessService = new HttpAccessService(configuration);
116 HttpAccess httpAccess = new HttpAccess(httpAccessService, new MockResponseStatusValidator());
117 String strTest = httpAccess.doDelete(strUrlTestHttp, null, null, mapHeaders, mapHeadersResponse);
118 HttpRequestResult jsonRespone = _objectMapper.readValue(strTest, HttpRequestResult.class);
119 assertEquals("DELETE", jsonRespone.getMethodName());
120 _logger.debug(strTest);
121 }
122
123 @Test
124 public void testDoPost() throws IOException, HttpAccessException {
125
126 String strUrlTestHttp = mockWebServer.url("/test").toString();
127
128 HttpClientConfiguration configuration = new HttpClientConfiguration();
129 configuration.setConnectionTimeout(10000);
130 configuration.setSocketTimeout(10000);
131
132 configuration.setNoProxyFor("*.paris.mdp");
133 configuration.setProxyPort("8080");
134
135 Map<String, String> mapHeaders = new HashMap<String, String>();
136 Map<String, String> mapParameters = new HashMap<String, String>();
137
138 Map<String, String> mapHeadersResponse = new HashMap<String, String>();
139
140 mapHeaders.put("Authorization", " Basic ");
141 mapParameters.put("grant_type", "client_credentials");
142 HttpAccessService httpAccessService = new HttpAccessService(configuration);
143 HttpAccess httpAccess = new HttpAccess(httpAccessService, new MockResponseStatusValidator());
144 String strTest = httpAccess.doPost(strUrlTestHttp, mapParameters, null, null, mapHeaders, mapHeadersResponse);
145 HttpRequestResult jsonRespone = _objectMapper.readValue(strTest, HttpRequestResult.class);
146 assertEquals("POST", jsonRespone.getMethodName());
147 _logger.debug(strTest);
148
149 }
150
151 @Test
152 public void testDoPut() throws HttpAccessException, JsonMappingException, JsonProcessingException {
153
154 String strUrlTestHttp = mockWebServer.url("/test/225").toString();
155
156 HttpClientConfiguration configuration = new HttpClientConfiguration();
157 configuration.setConnectionTimeout(10000);
158 configuration.setSocketTimeout(10000);
159
160 configuration.setNoProxyFor("*.paris.mdp");
161 configuration.setProxyPort("8080");
162
163 Map<String, String> mapHeaders = new HashMap<String, String>();
164 Map<String, String> mapParameters = new HashMap<String, String>();
165
166 Map<String, String> mapHeadersResponse = new HashMap<String, String>();
167
168 mapHeaders.put("Authorization", " Basic ");
169 mapParameters.put("grant_type", "client_credentials");
170 HttpAccessService httpAccessService = new HttpAccessService(configuration);
171 HttpAccess httpAccess = new HttpAccess(httpAccessService, new MockResponseStatusValidator());
172
173 String strTest = httpAccess.doPut(strUrlTestHttp, null, null, mapParameters, mapHeaders, mapHeadersResponse);
174 HttpRequestResult jsonRespone = _objectMapper.readValue(strTest, HttpRequestResult.class);
175 assertEquals("PUT", jsonRespone.getMethodName());
176 _logger.debug(strTest);
177
178 }
179
180 @Test
181 public void testDoPostMultipart()
182 throws UnsupportedEncodingException, HttpAccessException, JsonMappingException, JsonProcessingException {
183
184 String strUrlTestHttp = mockWebServer.url("/test").toString();
185
186 HttpClientConfiguration configuration = new HttpClientConfiguration();
187 configuration.setConnectionTimeout(1000);
188 configuration.setSocketTimeout(10000);
189
190 configuration.setNoProxyFor("*.paris.mdp");
191 configuration.setProxyPort("8080");
192
193 Map<String, String> mapHeaders = new HashMap<String, String>();
194
195 Map<String, List<String>> mapParameters = new HashMap<String, List<String>>();
196 List<String> params = new ArrayList<String>();
197 params.add("test1 é");
198 params.add("test2");
199
200 mapParameters.put("identityChange", params);
201 Map<String, String> mapHeadersResponse = new HashMap<String, String>();
202
203
204 mapHeaders.put("client_code", "RhssoFranceConnect");
205
206
207 HttpAccessService httpAccessService = new HttpAccessService(configuration);
208 HttpAccess httpAccess = new HttpAccess(httpAccessService, new MockResponseStatusValidator());
209
210 String strXml = "<xml> éttezez</xml>";
211
212 MemoryFileItem meFileItem = new MemoryFileItem(strXml.getBytes("UTF-8"), "test_file.xml",
213 strXml.getBytes("UTF-8").length, "text/xml; charset=UTF-8");
214
215 Map<String, FileItem> mapFileItem = new HashMap();
216
217 mapFileItem.put("file_download_param_1", meFileItem);
218
219 String strTest = httpAccess.doPostMultiPart(strUrlTestHttp, mapParameters, mapFileItem, null, null,
220 mapHeadersResponse, mapHeadersResponse);
221 HttpRequestResult jsonRespone = _objectMapper.readValue(strTest, HttpRequestResult.class);
222 assertEquals("POST", jsonRespone.getMethodName());
223 _logger.debug(strTest);
224
225 }
226
227 @Test
228 public void testDoPostJson() throws HttpAccessException, JsonMappingException, JsonProcessingException {
229 String strJson = "{\n" + " \"identity_change\" : {\n" + " \"identity\" : {\n"
230 + " \"customer_id\" : \"b59f9424-6c5f-4bc7-a12545\",\n" + " \"attributes\" : {\n"
231 + " \"birthcountry\" : {\n" + " \"key\" : \"birthcountry\",\n"
232 + " \"type\" : \"string\",\n" + " \"value\" : \"FRANCE\",\n"
233 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
234 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
235 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
236 + " \"certifier_code\" : \"fccertifier\",\n"
237 + " \"certifier_name\" : \"France Connect Certifier\",\n"
238 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
239 + " }\n" + " },\n" + " \"birthdate\" : {\n" + " \"key\" : \"birthdate\",\n"
240 + " \"type\" : \"string\",\n" + " \"value\" : \"24/08/1982\",\n"
241 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
242 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
243 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
244 + " \"certifier_code\" : \"fccertifier\",\n"
245 + " \"certifier_name\" : \"France Connect Certifier\",\n"
246 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
247 + " }\n" + " },\n" + " \"gender\" : {\n" + " \"key\" : \"gender\",\n"
248 + " \"type\" : \"string\",\n" + " \"value\" : \"1\",\n"
249 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
250 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
251 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
252 + " \"certifier_code\" : \"fccertifier\",\n"
253 + " \"certifier_name\" : \"France Connect Certifier\",\n"
254 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
255 + " }\n" + " },\n" + " \"birthplace\" : {\n" + " \"key\" : \"birthplace\",\n"
256 + " \"type\" : \"string\",\n" + " \"value\" : \"PARIS 07\",\n"
257 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
258 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
259 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
260 + " \"certifier_code\" : \"fccertifier\",\n"
261 + " \"certifier_name\" : \"France Connect Certifier\",\n"
262 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
263 + " }\n" + " },\n" + " \"login\" : {\n" + " \"key\" : \"login\",\n"
264 + " \"type\" : \"string\",\n" + " \"value\" : \"testbp2022@yopmail.com\",\n"
265 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
266 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
267 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
268 + " \"certifier_code\" : \"fccertifier\",\n"
269 + " \"certifier_name\" : \"France Connect Certifier\",\n"
270 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
271 + " }\n" + " },\n" + " \"family_name\" : {\n" + " \"key\" : \"family_name\",\n"
272 + " \"type\" : \"string\",\n" + " \"value\" : \"DUBOIS\",\n"
273 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
274 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
275 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
276 + " \"certifier_code\" : \"fccertifier\",\n"
277 + " \"certifier_name\" : \"France Connect Certifier\",\n"
278 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
279 + " }\n" + " },\n" + " \"first_name\" : {\n" + " \"key\" : \"first_name\",\n"
280 + " \"type\" : \"string\",\n" + " \"value\" : \"Angela Claire Louise Bernard\",\n"
281 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
282 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
283 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
284 + " \"certifier_code\" : \"fccertifier\",\n"
285 + " \"certifier_name\" : \"France Connect Certifier\",\n"
286 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
287 + " }\n" + " },\n" + " \"email\" : {\n" + " \"key\" : \"email\",\n"
288 + " \"type\" : \"string\",\n" + " \"value\" : \"testbp2022@yopmail.com\",\n"
289 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
290 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : false,\n"
291 + " \"writable\" : true,\n" + " \"certificate\" : null\n" + " },\n"
292 + " \"fc_birthdate\" : {\n" + " \"key\" : \"fc_birthdate\",\n"
293 + " \"type\" : \"string\",\n" + " \"value\" : \"1962-08-85\",\n"
294 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
295 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
296 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
297 + " \"certifier_code\" : \"fccertifier\",\n"
298 + " \"certifier_name\" : \"France Connect Certifier\",\n"
299 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
300 + " }\n" + " },\n" + " \n" + " \"fc_given_name\" : {\n"
301 + " \"key\" : \"fc_given_name\",\n" + " \"type\" : \"string\",\n"
302 + " \"value\" : \"Angela Claire Louise Bernard 3\",\n"
303 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
304 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
305 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
306 + " \"certifier_code\" : \"fccertifier\",\n"
307 + " \"certifier_name\" : \"France Connect Certifier\",\n"
308 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
309 + " }\n" + " },\n" + " \"fc_birthplace\" : {\n"
310 + " \"key\" : \"fc_birthplace\",\n" + " \"type\" : \"string\",\n"
311 + " \"value\" : \"75107\",\n" + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
312 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
313 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
314 + " \"certifier_code\" : \"fccertifier\",\n"
315 + " \"certifier_name\" : \"France Connect Certifier\",\n"
316 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
317 + " }\n" + " },\n" + " \n" + " \"fc_gender\" : {\n"
318 + " \"key\" : \"fc_gender\",\n" + " \"type\" : \"string\",\n"
319 + " \"value\" : \"female\",\n"
320 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
321 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
322 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
323 + " \"certifier_code\" : \"fccertifier\",\n"
324 + " \"certifier_name\" : \"France Connect Certifier\",\n"
325 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
326 + " }\n" + " },\n" + " \n" + " \"fc_family_name\" : {\n"
327 + " \"key\" : \"fc_family_name\",\n" + " \"type\" : \"string\",\n"
328 + " \"value\" : \"DUBOIS\",\n"
329 + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
330 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
331 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
332 + " \"certifier_code\" : \"fccertifier\",\n"
333 + " \"certifier_name\" : \"France Connect Certifier\",\n"
334 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
335 + " }\n" + " },\n" + " \"fc_birthcountry\" : {\n"
336 + " \"key\" : \"fc_birthcountry\",\n" + " \"type\" : \"string\",\n"
337 + " \"value\" : \"99100\",\n" + " \"application_last_update\" : \"RhssoFranceConnect\",\n"
338 + " \"date_last_update\" : 1622468105000,\n" + " \"certified\" : true,\n"
339 + " \"writable\" : true,\n" + " \"certificate\" : {\n"
340 + " \"certifier_code\" : \"fccertifier\",\n"
341 + " \"certifier_name\" : \"France Connect Certifier\",\n"
342 + " \"certificate_level\" : 3,\n" + " \"certificate_exp_date\" : null\n"
343 + " }\n" + " }\n" + " }\n" + "}\n" + ",\n" + " \"author\" : {\n"
344 + " \"id\" : \"usager\",\n" + " \"type\" : 1,\n"
345 + " \"application_code\" : \"RhssoFranceConnect\"\n" + " }\n" + " }\n" + "}";
346
347 String strUrlTestHttp = mockWebServer.url("/test").toString();
348
349 HttpClientConfiguration configuration = new HttpClientConfiguration();
350 configuration.setConnectionTimeout(1000);
351 configuration.setSocketTimeout(10000);
352
353 configuration.setNoProxyFor("*.paris.mdp");
354 configuration.setProxyPort("8080");
355
356 Map<String, String> mapHeaders = new HashMap<String, String>();
357
358 Map<String, List<String>> mapParameters = new HashMap<String, List<String>>();
359 List<String> params = new ArrayList<String>();
360 params.add(strJson);
361 mapParameters.put("identityChange", params);
362 Map<String, String> mapHeadersResponse = new HashMap<String, String>();
363
364
365 mapHeaders.put("client_code", "RhssoFranceConnect");
366
367
368 HttpAccessService httpAccessService = new HttpAccessService(configuration);
369 HttpAccess httpAccess = new HttpAccess(httpAccessService, new MockResponseStatusValidator());
370
371 String strTest = httpAccess.doPostJSON(strUrlTestHttp, strJson, mapHeaders, mapHeadersResponse);
372
373 HttpRequestResult jsonRespone = _objectMapper.readValue(strTest, HttpRequestResult.class);
374 assertEquals("POST", jsonRespone.getMethodName());
375 _logger.debug(strTest);
376
377
378 }
379
380 @Test
381 public void testDoDownloadFile() throws HttpAccessException {
382
383 String strUrlTestHttp = mockWebServer.url("/test").toString();
384
385 HttpClientConfiguration configuration = new HttpClientConfiguration();
386 configuration.setConnectionTimeout(1000);
387 configuration.setSocketTimeout(10000);
388 configuration.setProxyHost("");
389 configuration.setNoProxyFor("*.paris.mdp");
390 configuration.setProxyPort("8080");
391
392
393 HttpAccessService httpAccessService = new HttpAccessService(configuration);
394 HttpAccess httpAccess = new HttpAccess(httpAccessService, new MockResponseStatusValidator());
395
396 httpAccess.downloadFile(strUrlTestHttp, "/tmp/favicon-16x16.png");
397
398 }
399
400 @Test
401 public void testGetFileName() throws HttpAccessException {
402
403 String strUrlTestHttp = mockWebServer.url("/test").toString();
404
405 HttpClientConfiguration configuration = new HttpClientConfiguration();
406 configuration.setConnectionTimeout(1000);
407 configuration.setSocketTimeout(10000);
408 configuration.setProxyHost("");
409 configuration.setNoProxyFor("*.paris.mdp");
410 configuration.setProxyPort("8080");
411
412
413 HttpAccessService httpAccessService = new HttpAccessService(configuration);
414 HttpAccess httpAccess = new HttpAccess(httpAccessService, new MockResponseStatusValidator());
415
416 httpAccess.getFileName(strUrlTestHttp);
417
418 }
419
420 @Test
421 public void testConnectionPool() {
422
423 List<Integer> listOfNumbersGet = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
424 19, 20);
425 String strUrlTestHttp = mockWebServer.url("/test").toString();
426
427 HttpClientConfiguration configuration = new HttpClientConfiguration();
428 configuration.setConnectionTimeout(10000);
429 configuration.setSocketTimeout(100000);
430
431 configuration.setNoProxyFor("*.paris.mdp");
432 configuration.setProxyPort("8080");
433 configuration.setConnectionPoolEnabled(true);
434 configuration.setConnectionPoolMaxTotalConnection(3);
435 configuration.setConnectionPoolMaxConnectionPerHost(3);
436
437 Map<String, String> mapHeaders = new HashMap<String, String>();
438 Map<String, String> mapHeadersResponse = new HashMap<String, String>();
439
440 mapHeaders.put("Authorization", " Basic token with connection pool");
441 HttpAccessService httpAccessService = new HttpAccessService(configuration);
442 listOfNumbersGet.parallelStream().forEach(x -> {
443
444
445
446 HttpAccess httpAccess = new HttpAccess(httpAccessService, new MockResponseStatusValidator());
447
448 String strTest;
449 try {
450 strTest = httpAccess.doGet(strUrlTestHttp, null, null, mapHeaders, mapHeadersResponse);
451 HttpRequestResult jsonRespone= _objectMapper.readValue(strTest, HttpRequestResult.class);
452 assertEquals("GET", jsonRespone.getMethodName());
453 _logger.debug(strTest);
454 } catch (HttpAccessException e) {
455 assertFalse(true);
456 }
457
458 catch (JsonProcessingException e) {
459
460 assertFalse(true);
461 }
462
463
464
465
466
467
468 });
469 }
470
471 @Before
472 public void init() throws IOException {
473 this.mockWebServer = new MockWebServer();
474 this.mockWebServer.setDispatcher(new Dispatcher() {
475
476 @Override
477 public MockResponse dispatch(RecordedRequest request) {
478
479 return new MockResponse().addHeader("Content-Type", "application/json; charset=utf-8")
480 .setResponseCode(200).setBody(printRequest(request));
481
482 }
483 });
484
485 this.mockWebServer.start(18080);
486
487 }
488
489 @After
490 public void stopServer() throws IOException {
491
492 this.mockWebServer.shutdown();
493 }
494
495 private String printRequest(RecordedRequest request) {
496
497 try {
498 return _objectMapper.writeValueAsString(new HttpRequestResult(request.getMethod(),
499 request.getHeaders().getNamesAndValues$okhttp(), request.getBody().readUtf8(), request.getPath()));
500 } catch (JsonProcessingException e) {
501
502 e.printStackTrace();
503 }
504
505 return "";
506
507 }
508
509 }