Skip to content

Commit fdcde2a

Browse files
committed
test httpclient
1 parent 63214ed commit fdcde2a

File tree

2 files changed

+116
-40
lines changed

2 files changed

+116
-40
lines changed

src/main/java/net/lacnic/portal/auth/client/PortalHttpClient.java

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.security.KeyStore;
44

55
import org.apache.http.HttpVersion;
6+
import org.apache.http.client.HttpClient;
67
import org.apache.http.client.params.ClientPNames;
78
import org.apache.http.conn.ClientConnectionManager;
89
import org.apache.http.conn.scheme.PlainSocketFactory;
@@ -22,38 +23,7 @@
2223
@SuppressWarnings("deprecation")
2324
public class PortalHttpClient {
2425

25-
// public static HttpClient getNewHttpClient2() {
26-
// try {
27-
// KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
28-
// trustStore.load(null, null);
29-
//
30-
// MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
31-
// sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
32-
//
33-
// HttpParams params = new BasicHttpParams();
34-
// HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
35-
// HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
36-
//
37-
// SchemeRegistry registry = new SchemeRegistry();
38-
// registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
39-
// registry.register(new Scheme("https", sf, 443));
40-
//
41-
// ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
42-
//
43-
// DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);
44-
//
45-
// int timeout = 40; // seconds
46-
// HttpParams httpParams = httpClient.getParams();
47-
// httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
48-
// httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
49-
// httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));
50-
// return httpClient;
51-
// } catch (Exception e) {
52-
// return new DefaultHttpClient();
53-
// }
54-
// }
55-
56-
public static CloseableHttpClient getNewHttpClient() {
26+
public static HttpClient getNewHttpClient() {
5727
try {
5828
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
5929
trustStore.load(null, null);
@@ -70,18 +40,52 @@ public static CloseableHttpClient getNewHttpClient() {
7040
registry.register(new Scheme("https", sf, 443));
7141

7242
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
43+
try (DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params)) {
44+
int timeout = 40; // seconds
45+
HttpParams httpParams = httpClient.getParams();
46+
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
47+
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
48+
httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));
49+
return httpClient;
50+
}
51+
} catch (Exception e) {
52+
return new DefaultHttpClient();
53+
}
54+
}
55+
56+
public static HttpClient getCloseableHttpClient() {
57+
try {
58+
// Initialize truststore and custom socket factory (sf)
59+
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
60+
trustStore.load(null, null);
61+
MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
62+
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
7363

74-
CloseableHttpClient httpClient = new DefaultHttpClient(ccm, params);
64+
// Create HttpParams and SchemeRegistry
65+
HttpParams params = new BasicHttpParams();
66+
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
67+
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
68+
69+
SchemeRegistry registry = new SchemeRegistry();
70+
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
71+
registry.register(new Scheme("https", sf, 443));
72+
73+
// Initialize ClientConnectionManager
74+
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
7575

76-
int timeout = 40; // seconds
77-
HttpParams httpParams = httpClient.getParams();
78-
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
79-
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
80-
httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, (long) timeout * 1000);
76+
// Use try-with-resources to ensure httpClient is closed after use
77+
try (CloseableHttpClient httpClient = new DefaultHttpClient(ccm, params)) {
78+
int timeout = 40; // seconds
79+
HttpParams httpParams = httpClient.getParams();
80+
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
81+
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
82+
httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, (long) timeout * 1000);
8183

82-
return httpClient;
84+
return httpClient;
85+
}
8386
} catch (Exception e) {
84-
return HttpClients.createDefault(); // Returns a default CloseableHttpClient instance
87+
e.printStackTrace();
88+
return HttpClients.createDefault(); // Fallback to default HttpClient
8589
}
8690
}
8791
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package net.lacnic.portal.auth.client;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.apache.http.client.HttpClient;
8+
import org.apache.http.impl.client.CloseableHttpClient;
9+
import org.apache.http.impl.client.DefaultHttpClient;
10+
import org.apache.http.params.HttpParams;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class PortalHttpClientTest {
14+
15+
@Test
16+
void testGetNewHttpClient_returnsConfiguredHttpClient() {
17+
// Call the method
18+
HttpClient httpClient = PortalHttpClient.getNewHttpClient();
19+
20+
// Verify that HttpClient is not null
21+
assertNotNull(httpClient);
22+
assertTrue(httpClient instanceof DefaultHttpClient, "Expected instance of DefaultHttpClient");
23+
24+
// Check if parameters were set correctly
25+
HttpParams httpParams = httpClient.getParams();
26+
assertEquals(40000, httpParams.getIntParameter("http.connection.timeout", 0));
27+
assertEquals(40000, httpParams.getIntParameter("http.socket.timeout", 0));
28+
assertEquals(40000L, httpParams.getLongParameter("http.conn-manager.timeout", 0));
29+
}
30+
31+
@Test
32+
void testGetCloseableHttpClient_returnsConfiguredHttpClient() {
33+
// Call the method
34+
HttpClient httpClient = PortalHttpClient.getCloseableHttpClient();
35+
36+
// Verify that HttpClient is not null and is an instance of CloseableHttpClient
37+
assertNotNull(httpClient);
38+
assertTrue(httpClient instanceof CloseableHttpClient, "Expected instance of CloseableHttpClient");
39+
40+
// Check if parameters were set correctly
41+
HttpParams httpParams = httpClient.getParams();
42+
assertEquals(40000, httpParams.getIntParameter("http.connection.timeout", 0));
43+
assertEquals(40000, httpParams.getIntParameter("http.socket.timeout", 0));
44+
assertEquals(40000L, httpParams.getLongParameter("http.conn-manager.timeout", 0));
45+
}
46+
47+
@Test
48+
void testGetNewHttpClient_fallbackOnException() {
49+
HttpClient httpClient = null;
50+
try {
51+
httpClient = PortalHttpClient.getNewHttpClient();
52+
} catch (Exception ignored) {
53+
}
54+
55+
// Verify that HttpClient is not null even if exceptions occur
56+
assertNotNull(httpClient);
57+
assertTrue(httpClient instanceof DefaultHttpClient);
58+
}
59+
60+
@Test
61+
void testGetCloseableHttpClient_fallbackOnException() {
62+
HttpClient httpClient = null;
63+
try {
64+
httpClient = PortalHttpClient.getCloseableHttpClient();
65+
} catch (Exception ignored) {
66+
}
67+
68+
// Verify that HttpClient is not null even if exceptions occur
69+
assertNotNull(httpClient);
70+
assertTrue(httpClient instanceof CloseableHttpClient);
71+
}
72+
}

0 commit comments

Comments
 (0)