From d174341bbfb6fadbe6ad918eb2c16ea31ed9e5b0 Mon Sep 17 00:00:00 2001 From: Giuseppe Compierchio Date: Tue, 5 Sep 2023 09:28:27 +0200 Subject: [PATCH 1/7] fix: adjust proxy implementation --- .../com/whatsapp/api/WhatsappApiServiceGenerator.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java index 240eb7fa7..a0f9fe33f 100644 --- a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java +++ b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java @@ -58,7 +58,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,13 +66,14 @@ 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; } From b676c367a2f11b0a0981cff334e35eab07ad0985 Mon Sep 17 00:00:00 2001 From: Giuseppe Compierchio Date: Tue, 5 Sep 2023 09:44:02 +0200 Subject: [PATCH 2/7] fix: add proxy object injection in shared object --- .../java/com/whatsapp/api/WhatsappApiServiceGenerator.java | 1 + .../whatsapp/api/utils/proxy/CustomHttpProxySelector.java | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java index a0f9fe33f..2af6e96bf 100644 --- a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java +++ b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java @@ -70,6 +70,7 @@ public static void setHttpProxy(String host, int port, String username, String p sharedClient = sharedClient.newBuilder() .proxySelector(proxySelector) + .proxy(proxySelector.getProxy()) .build(); if (username == null || pwd == null) { 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..33daa8e04 100644 --- a/src/main/java/com/whatsapp/api/utils/proxy/CustomHttpProxySelector.java +++ b/src/main/java/com/whatsapp/api/utils/proxy/CustomHttpProxySelector.java @@ -13,6 +13,10 @@ public class CustomHttpProxySelector extends ProxySelector { private final Proxy proxy; + public Proxy getProxy() { + return proxy; + } + public CustomHttpProxySelector(String host, int port) { this.proxy = new Proxy(Type.HTTP, new InetSocketAddress(host, port)); } @@ -26,4 +30,6 @@ public List select(URI uri) { public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { // Do something here } + + } From ebb3c977785f0d29d38b296ce8ee2da559bf4ddf Mon Sep 17 00:00:00 2001 From: Giuseppe Compierchio Date: Tue, 5 Sep 2023 10:22:27 +0200 Subject: [PATCH 3/7] fix: removed proxy injection because have precedence to ProxySelector --- .../java/com/whatsapp/api/WhatsappApiServiceGenerator.java | 1 - .../com/whatsapp/api/utils/proxy/CustomHttpProxySelector.java | 4 ---- 2 files changed, 5 deletions(-) diff --git a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java index 2af6e96bf..a0f9fe33f 100644 --- a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java +++ b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java @@ -70,7 +70,6 @@ public static void setHttpProxy(String host, int port, String username, String p sharedClient = sharedClient.newBuilder() .proxySelector(proxySelector) - .proxy(proxySelector.getProxy()) .build(); if (username == null || pwd == null) { 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 33daa8e04..a0256c4b4 100644 --- a/src/main/java/com/whatsapp/api/utils/proxy/CustomHttpProxySelector.java +++ b/src/main/java/com/whatsapp/api/utils/proxy/CustomHttpProxySelector.java @@ -13,10 +13,6 @@ public class CustomHttpProxySelector extends ProxySelector { private final Proxy proxy; - public Proxy getProxy() { - return proxy; - } - public CustomHttpProxySelector(String host, int port) { this.proxy = new Proxy(Type.HTTP, new InetSocketAddress(host, port)); } From eb4780fe1e30a916393ed048a8a5011a5fe61efd Mon Sep 17 00:00:00 2001 From: Giuseppe Compierchio Date: Tue, 5 Sep 2023 11:48:58 +0200 Subject: [PATCH 4/7] fix: remove authenticator to use ProxyAutenthicator like ProxySelector --- src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java index a0f9fe33f..ef480e763 100644 --- a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java +++ b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java @@ -80,7 +80,7 @@ public static void setHttpProxy(String host, int port, String username, String p CustomProxyAuthenticator proxyAuthenticator = new CustomProxyAuthenticator(username, pwd); sharedClient = sharedClient.newBuilder() - .authenticator(proxyAuthenticator) + .proxyAuthenticator(proxyAuthenticator) .build(); } From dd2c43fd19066e66b9dd7e5dc29368cbb60e0160 Mon Sep 17 00:00:00 2001 From: Giuseppe Compierchio Date: Tue, 5 Sep 2023 11:49:18 +0200 Subject: [PATCH 5/7] fix: set credentials as final --- .../api/utils/proxy/CustomProxyAuthenticator.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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..26eec8e8b 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,20 @@ public class CustomProxyAuthenticator implements Authenticator { - private final String credential; + private final String CREDENTIALS; + + public String getCREDENTIALS() { + return 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(); } } From f8b18d9d51967d5d3ef43a94fd477095e8153d35 Mon Sep 17 00:00:00 2001 From: Giuseppe Compierchio Date: Tue, 5 Sep 2023 12:21:57 +0200 Subject: [PATCH 6/7] fix: add test for proxy integration --- .../api/WhatsappApiServiceGenerator.java | 5 +- .../api/WhatsappApiServiceGeneratorTest.java | 119 ++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/whatsapp/api/WhatsappApiServiceGeneratorTest.java diff --git a/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java b/src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java index ef480e763..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(); 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"); + + } +} From cf735496c9f3a046d69c7e9fe2a28479d1f4ca93 Mon Sep 17 00:00:00 2001 From: Giuseppe Compierchio Date: Tue, 5 Sep 2023 12:55:24 +0200 Subject: [PATCH 7/7] fix: remove credentials --- .../whatsapp/api/utils/proxy/CustomProxyAuthenticator.java | 4 ---- 1 file changed, 4 deletions(-) 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 26eec8e8b..610102cbc 100644 --- a/src/main/java/com/whatsapp/api/utils/proxy/CustomProxyAuthenticator.java +++ b/src/main/java/com/whatsapp/api/utils/proxy/CustomProxyAuthenticator.java @@ -12,10 +12,6 @@ public class CustomProxyAuthenticator implements Authenticator { private final String CREDENTIALS; - public String getCREDENTIALS() { - return CREDENTIALS; - } - public CustomProxyAuthenticator(final String username, final String password) { CREDENTIALS = Credentials.basic(username, password); }