Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adjust proxy implementation #122

Merged
merged 10 commits into from
Jan 26, 2024
18 changes: 11 additions & 7 deletions src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -58,28 +61,29 @@ private WhatsappApiServiceGenerator() {
* </ul>
* <p>
* @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 <a href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/proxy-selector/">Proxy Selector</a>
* @see <a href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/proxy-authenticator/">Proxy Authenticator</a>
*/
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public List<Proxy> select(URI uri) {
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
// Do something here
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 22 in src/main/java/com/whatsapp/api/utils/proxy/CustomProxyAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/whatsapp/api/utils/proxy/CustomProxyAuthenticator.java#L22

Added line #L22 was not covered by tests
.build();
}
}
119 changes: 119 additions & 0 deletions src/test/java/com/whatsapp/api/WhatsappApiServiceGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -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");

}
}