Skip to content

Commit

Permalink
added tests for httpmodule
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Al committed Sep 11, 2024
1 parent cfdc429 commit aac8a1b
Show file tree
Hide file tree
Showing 11 changed files with 712 additions and 473 deletions.
5 changes: 0 additions & 5 deletions okhttp-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
<artifactId>logging-interceptor</artifactId>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.0.3</version>
</dependency>
<dependency>
<groupId>io.split.client</groupId>
<artifactId>java-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.split.service.SplitHttpClient;

import okhttp3.*;
import okhttp3.OkHttpClient.Builder;
import okhttp3.OkHttpClient.*;
import okhttp3.Request.*;
import okhttp3.logging.HttpLoggingInterceptor;
import org.slf4j.Logger;
Expand All @@ -31,7 +31,7 @@
import java.util.concurrent.TimeUnit;

public class OkHttpClientImpl implements SplitHttpClient {
public final OkHttpClient httpClient;
protected OkHttpClient httpClient;
private static final Logger _log = LoggerFactory.getLogger(OkHttpClientImpl.class);
private static final String HEADER_CACHE_CONTROL_NAME = "Cache-Control";
private static final String HEADER_CACHE_CONTROL_VALUE = "no-cache";
Expand All @@ -42,15 +42,26 @@ public class OkHttpClientImpl implements SplitHttpClient {
private static final String HEADER_CLIENT_VERSION = "SplitSDKVersion";
private RequestDecorator _requestDecorator;
private String _apikey;
private SDKMetadata _metadata;
protected SDKMetadata _metadata;

public OkHttpClientImpl(String apiToken, SDKMetadata sdkMetadata, RequestDecorator requestDecorator,
Proxy proxy, String proxyAuthKerberosPrincipalName, boolean debugEnabled,
int readTimeout, int connectionTimeout) throws IOException {
_apikey = apiToken;
_metadata = sdkMetadata;
_requestDecorator = requestDecorator;
setHttpClient(proxy, proxyAuthKerberosPrincipalName, debugEnabled,
readTimeout, connectionTimeout);
}

protected void setHttpClient(Proxy proxy, String proxyAuthKerberosPrincipalName, boolean debugEnabled,
int readTimeout, int connectionTimeout) throws IOException {
httpClient = initializeClient(proxy, proxyAuthKerberosPrincipalName, debugEnabled,
readTimeout, connectionTimeout);
}

protected OkHttpClient initializeClient(Proxy proxy, String proxyAuthKerberosPrincipalName, boolean debugEnabled,
int readTimeout, int connectionTimeout) throws IOException {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
if (debugEnabled) {
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
Expand All @@ -66,7 +77,7 @@ public OkHttpClientImpl(String apiToken, SDKMetadata sdkMetadata, RequestDecorat

Authenticator proxyAuthenticator = getProxyAuthenticator(proxyAuthKerberosPrincipalName, kerberosOptions);

httpClient = new okhttp3.OkHttpClient.Builder()
return new okhttp3.OkHttpClient.Builder()
.proxy(proxy)
.readTimeout(readTimeout, TimeUnit.MILLISECONDS)
.connectTimeout(connectionTimeout, TimeUnit.MILLISECONDS)
Expand All @@ -83,7 +94,7 @@ public HTTPKerberosAuthInterceptor getProxyAuthenticator(String proxyKerberosPri
@Override
public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, List<String>> additionalHeaders) {
try {
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder();
okhttp3.Request.Builder requestBuilder = getRequestBuilder();
requestBuilder.url(uri.toString());
setBasicHeaders(requestBuilder);
setAdditionalAndDecoratedHeaders(requestBuilder, additionalHeaders);
Expand Down Expand Up @@ -135,7 +146,8 @@ public SplitHttpResponse post(URI url, HttpEntity entity,
RequestBody postBody = RequestBody.create(post.getBytes());
requestBuilder.post(postBody);

Request request = getRequest(requestBuilder);
Request request = requestBuilder.build();
System.out.println(request);
_log.debug(String.format("Request Headers: %s", request.headers()));

Response response = httpClient.newCall(request).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import org.slf4j.LoggerFactory;

public class OkHttpModule implements CustomHttpModule {
private static final int DEFAULT_CONNECTION_TIMEOUT = 10000;
private static final int DEFAULT_READ_TIMEOUT = 10000;
private final boolean _debugEnabled;
private final int _connectionTimeout;
private final int _readTimeout;
private static final int DEFAULT_CONNECTION_TIMEOUT = 15000;
private static final int DEFAULT_READ_TIMEOUT = 15000;
private final Boolean _debugEnabled;
private final Integer _connectionTimeout;
private final Integer _readTimeout;
private final Proxy _proxy;
private final ProxyAuthScheme _proxyAuthScheme;
private final String _proxyAuthKerberosPrincipalName;
Expand All @@ -29,9 +29,9 @@ public static Builder builder() {
private OkHttpModule(ProxyAuthScheme proxyAuthScheme,
String proxyAuthKerberosPrincipalName,
Proxy proxy,
int connectionTimeout,
int readTimeout,
boolean debugEnabled) {
Integer connectionTimeout,
Integer readTimeout,
Boolean debugEnabled) {
_proxyAuthScheme = proxyAuthScheme;
_proxyAuthKerberosPrincipalName = proxyAuthKerberosPrincipalName;
_proxy = proxy;
Expand All @@ -54,24 +54,24 @@ public ProxyAuthScheme proxyAuthScheme() {
return _proxyAuthScheme;
}
public String proxyKerberosPrincipalName() { return _proxyAuthKerberosPrincipalName; }
public int connectionTimeout() {
public Integer connectionTimeout() {
return _connectionTimeout;
}
public boolean debugEnabled() {
public Boolean debugEnabled() {
return _debugEnabled;
}
public int readTimeout() {
public Integer readTimeout() {
return _readTimeout;
}

public static final class Builder {
private int _connectionTimeout = 15000;
private int _readTimeout = 15000;
private Integer _connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
private Integer _readTimeout = DEFAULT_READ_TIMEOUT;
private String _proxyHost = "localhost";
private int _proxyPort = -1;
private ProxyAuthScheme _proxyAuthScheme = null;
private String _proxyAuthKerberosPrincipalName = null;
private boolean _debugEnabled = false;
private Boolean _debugEnabled = false;

public Builder() {
}
Expand Down Expand Up @@ -133,6 +133,28 @@ public Builder proxyAuthKerberosPrincipalName(String proxyAuthKerberosPrincipalN
return this;
}

/**
* HTTP Connection Timeout
*
* @param connectionTimeout
* @return this builder
*/
public Builder connectionTimeout(int connectionTimeout) {
_connectionTimeout = connectionTimeout;
return this;
}

/**
* HTTP Read Timeout
*
* @param readTimeout
* @return this builder
*/
public Builder readTimeout(int readTimeout) {
_readTimeout = readTimeout;
return this;
}

private void verifyAuthScheme() {
if (_proxyAuthScheme == ProxyAuthScheme.KERBEROS) {
if (proxy() == null) {
Expand All @@ -145,10 +167,10 @@ private void verifyAuthScheme() {
}

private void verifyTimeouts() {
if (_connectionTimeout <= 0 || _connectionTimeout > DEFAULT_CONNECTION_TIMEOUT) {
if (_connectionTimeout <= 0) {
_connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
}
if (_readTimeout <= 0 || _readTimeout > DEFAULT_READ_TIMEOUT) {
if (_readTimeout <= 0) {
_readTimeout = DEFAULT_READ_TIMEOUT;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.split.httpmodules.okhttp;

import io.split.httpmodules.okhttp.HTTPKerberosAuthInterceptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
Expand Down Expand Up @@ -29,7 +28,6 @@
@RunWith(PowerMockRunner.class)
@PrepareForTest(HTTPKerberosAuthInterceptor.class)
public class HTTPKerberosAuthIntercepterTest {
/*
@Test
public void testBasicFlow() throws Exception {
System.setProperty("java.security.krb5.conf", "src/test/resources/krb5.conf");
Expand Down Expand Up @@ -64,7 +62,7 @@ public void testBasicFlow() throws Exception {
okhttp3.Request request = kerberosAuthInterceptor.authenticate(null, response);
assertThat(request.headers("Proxy-authorization"), is(equalTo(Arrays.asList("Negotiate secured-token"))));
}
/*
@Test
public void testKerberosLoginConfiguration() {
Map<String, String> kerberosOptions = new HashMap<String, String>();
Expand All @@ -84,7 +82,7 @@ public void testKerberosLoginConfigurationException() {
HTTPKerberosAuthInterceptor.KerberosLoginConfiguration kerberosConfig = new HTTPKerberosAuthInterceptor.KerberosLoginConfiguration();
AppConfigurationEntry[] appConfig = kerberosConfig.getAppConfigurationEntry("");
}
*/
@Test
public void testBuildAuthorizationHeader() throws LoginException, PrivilegedActionException {
System.setProperty("java.security.krb5.conf", "src/test/resources/krb5.conf");
Expand All @@ -110,6 +108,4 @@ public void testBuildAuthorizationHeader() throws LoginException, PrivilegedActi

assertThat("secret-token", is(equalTo(kerberosAuthInterceptor.buildAuthorizationHeader("bilal"))));
}
*/
}
Loading

0 comments on commit aac8a1b

Please sign in to comment.