Skip to content

Commit

Permalink
test httpclient
Browse files Browse the repository at this point in the history
  • Loading branch information
grada84 committed Nov 4, 2024
1 parent 63214ed commit fdcde2a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 40 deletions.
84 changes: 44 additions & 40 deletions src/main/java/net/lacnic/portal/auth/client/PortalHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.security.KeyStore;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
Expand All @@ -22,38 +23,7 @@
@SuppressWarnings("deprecation")
public class PortalHttpClient {

// public static HttpClient getNewHttpClient2() {
// try {
// KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
// trustStore.load(null, null);
//
// MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
// sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//
// HttpParams params = new BasicHttpParams();
// HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
// HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
//
// SchemeRegistry registry = new SchemeRegistry();
// registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// registry.register(new Scheme("https", sf, 443));
//
// ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
//
// DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);
//
// int timeout = 40; // seconds
// HttpParams httpParams = httpClient.getParams();
// httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
// httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
// httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));
// return httpClient;
// } catch (Exception e) {
// return new DefaultHttpClient();
// }
// }

public static CloseableHttpClient getNewHttpClient() {
public static HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
Expand All @@ -70,18 +40,52 @@ public static CloseableHttpClient getNewHttpClient() {
registry.register(new Scheme("https", sf, 443));

ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
try (DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params)) {
int timeout = 40; // seconds
HttpParams httpParams = httpClient.getParams();
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));
return httpClient;
}
} catch (Exception e) {
return new DefaultHttpClient();
}
}

public static HttpClient getCloseableHttpClient() {
try {
// Initialize truststore and custom socket factory (sf)
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

CloseableHttpClient httpClient = new DefaultHttpClient(ccm, params);
// Create HttpParams and SchemeRegistry
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));

// Initialize ClientConnectionManager
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

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

return httpClient;
return httpClient;
}
} catch (Exception e) {
return HttpClients.createDefault(); // Returns a default CloseableHttpClient instance
e.printStackTrace();
return HttpClients.createDefault(); // Fallback to default HttpClient
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package net.lacnic.portal.auth.client;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.junit.jupiter.api.Test;

public class PortalHttpClientTest {

@Test
void testGetNewHttpClient_returnsConfiguredHttpClient() {
// Call the method
HttpClient httpClient = PortalHttpClient.getNewHttpClient();

// Verify that HttpClient is not null
assertNotNull(httpClient);
assertTrue(httpClient instanceof DefaultHttpClient, "Expected instance of DefaultHttpClient");

// Check if parameters were set correctly
HttpParams httpParams = httpClient.getParams();
assertEquals(40000, httpParams.getIntParameter("http.connection.timeout", 0));
assertEquals(40000, httpParams.getIntParameter("http.socket.timeout", 0));
assertEquals(40000L, httpParams.getLongParameter("http.conn-manager.timeout", 0));
}

@Test
void testGetCloseableHttpClient_returnsConfiguredHttpClient() {
// Call the method
HttpClient httpClient = PortalHttpClient.getCloseableHttpClient();

// Verify that HttpClient is not null and is an instance of CloseableHttpClient
assertNotNull(httpClient);
assertTrue(httpClient instanceof CloseableHttpClient, "Expected instance of CloseableHttpClient");

// Check if parameters were set correctly
HttpParams httpParams = httpClient.getParams();
assertEquals(40000, httpParams.getIntParameter("http.connection.timeout", 0));
assertEquals(40000, httpParams.getIntParameter("http.socket.timeout", 0));
assertEquals(40000L, httpParams.getLongParameter("http.conn-manager.timeout", 0));
}

@Test
void testGetNewHttpClient_fallbackOnException() {
HttpClient httpClient = null;
try {
httpClient = PortalHttpClient.getNewHttpClient();
} catch (Exception ignored) {
}

// Verify that HttpClient is not null even if exceptions occur
assertNotNull(httpClient);
assertTrue(httpClient instanceof DefaultHttpClient);
}

@Test
void testGetCloseableHttpClient_fallbackOnException() {
HttpClient httpClient = null;
try {
httpClient = PortalHttpClient.getCloseableHttpClient();
} catch (Exception ignored) {
}

// Verify that HttpClient is not null even if exceptions occur
assertNotNull(httpClient);
assertTrue(httpClient instanceof CloseableHttpClient);
}
}

0 comments on commit fdcde2a

Please sign in to comment.