diff --git a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java index 240eb7fa7..f46c52fa7 100644 --- a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java +++ b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java @@ -37,8 +37,11 @@ private WhatsappApiServiceGenerator() { } static { + sharedClient = createDefaultHttpClient(); + } - sharedClient = new OkHttpClient.Builder()// + public static OkHttpClient createDefaultHttpClient(){ + return new OkHttpClient.Builder()// .callTimeout(20, TimeUnit.SECONDS)// .pingInterval(20, TimeUnit.SECONDS)// .build(); @@ -58,7 +61,7 @@ private WhatsappApiServiceGenerator() { * *

* @param host the host (Not null) - * @param port the port (Not null) + * @param port the port * @param username the username * @param pwd the pwd * @see Proxy Selector @@ -66,20 +69,21 @@ private WhatsappApiServiceGenerator() { */ public static void setHttpProxy(String host, int port, String username, String pwd) { Objects.requireNonNull(host, "Host cannot be null"); - Objects.requireNonNull(port, "Http Port cannot be null"); CustomHttpProxySelector proxySelector = new CustomHttpProxySelector(host, port); + sharedClient = sharedClient.newBuilder() + .proxySelector(proxySelector) + .build(); + if (username == null || pwd == null) { - sharedClient = sharedClient.newBuilder() - .proxySelector(proxySelector) - .build(); + //Without authentication return; } CustomProxyAuthenticator proxyAuthenticator = new CustomProxyAuthenticator(username, pwd); sharedClient = sharedClient.newBuilder() - .authenticator(proxyAuthenticator) + .proxyAuthenticator(proxyAuthenticator) .build(); } diff --git a/src/main/java/com/whatsapp/api/utils/proxy/CustomHttpProxySelector.java b/src/main/java/com/whatsapp/api/utils/proxy/CustomHttpProxySelector.java index de5bed1dd..a0256c4b4 100644 --- a/src/main/java/com/whatsapp/api/utils/proxy/CustomHttpProxySelector.java +++ b/src/main/java/com/whatsapp/api/utils/proxy/CustomHttpProxySelector.java @@ -26,4 +26,6 @@ public List select(URI uri) { public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { // Do something here } + + } diff --git a/src/main/java/com/whatsapp/api/utils/proxy/CustomProxyAuthenticator.java b/src/main/java/com/whatsapp/api/utils/proxy/CustomProxyAuthenticator.java index f258ccee7..610102cbc 100644 --- a/src/main/java/com/whatsapp/api/utils/proxy/CustomProxyAuthenticator.java +++ b/src/main/java/com/whatsapp/api/utils/proxy/CustomProxyAuthenticator.java @@ -10,16 +10,16 @@ public class CustomProxyAuthenticator implements Authenticator { - private final String credential; + private final String CREDENTIALS; public CustomProxyAuthenticator(final String username, final String password) { - credential = Credentials.basic(username, password); + CREDENTIALS = Credentials.basic(username, password); } @Override public Request authenticate(final Route route, final Response response) throws IOException { return response.request().newBuilder() - .header("Proxy-Authorization", credential) + .header("Proxy-Authorization", CREDENTIALS) .build(); } } diff --git a/src/test/java/com/whatsapp/api/WhatsappApiServiceGeneratorTest.java b/src/test/java/com/whatsapp/api/WhatsappApiServiceGeneratorTest.java new file mode 100644 index 000000000..8678205d7 --- /dev/null +++ b/src/test/java/com/whatsapp/api/WhatsappApiServiceGeneratorTest.java @@ -0,0 +1,119 @@ +package com.whatsapp.api; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.IOException; +import java.net.ProxySelector; +import java.net.URISyntaxException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.whatsapp.api.domain.errors.WhatsappApiError; +import com.whatsapp.api.exception.WhatsappApiException; +import com.whatsapp.api.utils.proxy.CustomHttpProxySelector; +import com.whatsapp.api.utils.proxy.CustomProxyAuthenticator; + +import okhttp3.Authenticator; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.ResponseBody; +import retrofit2.Response; + +public class WhatsappApiServiceGeneratorTest extends TestUtils { + + @BeforeEach + void resetProxy() { + WhatsappApiServiceGenerator.sharedClient = WhatsappApiServiceGenerator.createDefaultHttpClient(); + } + + /** + * Method under test: + * {@link WhatsappApiServiceGenerator#getSharedClient} + */ + @Test + void testGetSharedClient() { + + assertNotNull(WhatsappApiServiceGenerator.getSharedClient(), "Shared client should not be null"); + assertEquals(WhatsappApiServiceGenerator.getSharedClient().getClass(), OkHttpClient.class, "Shared client should be OkHttpClient"); + + } + + /** + * Method under test: + * {@link WhatsappApiServiceGenerator#getWhatsappApiError} + * + * @throws IOException + * @throws URISyntaxException + * @throws WhatsappApiException + */ + @Test + void testGetWhatsappApiError() throws IOException, URISyntaxException { + + String verifyCodeErrorBody = fromResource("/phone/verifyCodeError.json"); + + Response response = Response.error(400, ResponseBody.create(verifyCodeErrorBody, MediaType.parse("application/json"))); + WhatsappApiError apiError = WhatsappApiServiceGenerator.getWhatsappApiError(response); + + assertEquals(136025, apiError.error().code(), "Error code should be 136025"); + assertEquals(2388093, apiError.error().errorSubcode(), "Error code should be 136025"); + assertEquals(false, apiError.error().isTransient(), "Error code should be 136025"); + assertEquals("O c\u00F3digo inserido est\u00E1 incorreto.", apiError.error().errorUserMsg(), "Error code should be 136025"); + assertEquals("N\u00E3o foi poss\u00EDvel verificar o c\u00F3digo", apiError.error().errorUserSubtitle(), "Error code should be 136025"); + + } + + /** + * Method under test: + * {@link WhatsappApiServiceGenerator#setHttpProxy(String, int, String, String)} + */ + @Test + void testSetHttpProxy_WithoutAuthentication() { + + // Pre-condition Proxy + assertNull(WhatsappApiServiceGenerator.getSharedClient().proxy(), "Proxy should be null"); + assertEquals(ProxySelector.getDefault(), WhatsappApiServiceGenerator.getSharedClient().proxySelector(), + "Proxy selector should be null"); + + // Set proxy in shared client + WhatsappApiServiceGenerator.setHttpProxy("localhost", 8080, null, null); + + // Check if proxy is set + assertNotNull(WhatsappApiServiceGenerator.getSharedClient().proxySelector(), "Proxy selector should not be null"); + assertEquals(WhatsappApiServiceGenerator.getSharedClient().proxySelector().getClass(), CustomHttpProxySelector.class, "Proxy selector should be CustomHttpProxySelector"); + + // Check if authenticator is NONE + assertEquals(Authenticator.NONE, WhatsappApiServiceGenerator.getSharedClient().authenticator(), "Authenticator should be NONE"); + } + + /** + * Method under test: + * {@link WhatsappApiServiceGenerator#setHttpProxy(String, int, String, String)} + */ + @Test + void testSetHttpProxy_WithAuthentication() { + + // Pre-condition Proxy + assertNull(WhatsappApiServiceGenerator.getSharedClient().proxy(), "Proxy should be null"); + assertEquals(ProxySelector.getDefault(), WhatsappApiServiceGenerator.getSharedClient().proxySelector(), + "Proxy selector should be ProxySelector.getDefault()"); + + // Pre-condition Authenticator + assertEquals(Authenticator.NONE, WhatsappApiServiceGenerator.getSharedClient().authenticator(), "Authenticator should be NONE"); + assertEquals(Authenticator.NONE, WhatsappApiServiceGenerator.getSharedClient().proxyAuthenticator(), "Authenticator should be NONE"); + + // Set proxy with Authentication in shared client + WhatsappApiServiceGenerator.setHttpProxy("localhost", 8080, "Proxy-User", "Proxy-Pwd"); + + // Check if proxy is set + assertNotNull(WhatsappApiServiceGenerator.getSharedClient().proxySelector(), "Proxy selector should not be null"); + assertEquals(WhatsappApiServiceGenerator.getSharedClient().proxySelector().getClass(), CustomHttpProxySelector.class, "Proxy selector should be CustomHttpProxySelector"); + + // Check if authenticator is CustomProxyAuthenticator + assertNotNull(WhatsappApiServiceGenerator.getSharedClient().proxyAuthenticator(), "Proxy Authenticator should not be null"); + assertEquals(CustomProxyAuthenticator.class, WhatsappApiServiceGenerator.getSharedClient().proxyAuthenticator().getClass(), "Authenticator should be CustomProxyAuthenticator"); + + } +}