Skip to content

Commit

Permalink
fixed okhttp decorator errors and updated tests
Browse files Browse the repository at this point in the history
Bilal Al committed Sep 13, 2024
1 parent 568b511 commit 84eeea5
Showing 9 changed files with 79 additions and 52 deletions.
2 changes: 1 addition & 1 deletion okhttp-modules/pom.xml
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
<version>4.13.0-rc3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<version>4.13.0-rc3</version>
<artifactId>okhttp-modules</artifactId>
<packaging>jar</packaging>
<name>http-modules</name>
Original file line number Diff line number Diff line change
@@ -91,7 +91,19 @@ public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, List<Str
okhttp3.Request.Builder requestBuilder = getRequestBuilder();
requestBuilder.url(uri.toString());
Map<String, List<String>> headers = mergeHeaders(buildBasicHeaders(), additionalHeaders);
requestBuilder = OkHttpRequestDecorator.decorate(headers, requestBuilder, _decorator);
Map<String, List<String>> decorateHeaders = OkHttpRequestDecorator.decorate(headers, _decorator);
Map<String, List<String>> finalHeaders;
if (decorateHeaders.isEmpty()) {
finalHeaders = headers;
} else {
finalHeaders = decorateHeaders;
}
for (Map.Entry<String, List<String>> e : finalHeaders.entrySet()) {
for (String headerValue : e.getValue()) {
requestBuilder.addHeader(e.getKey(), headerValue);
}
}

if (options.cacheControlHeadersEnabled()) {
requestBuilder.addHeader(HEADER_CACHE_CONTROL_NAME, HEADER_CACHE_CONTROL_VALUE);
}
@@ -133,10 +145,21 @@ public SplitHttpResponse post(URI url, String entity,
okhttp3.Request.Builder requestBuilder = getRequestBuilder();
requestBuilder.url(url.toString());
Map<String, List<String>> headers = mergeHeaders(buildBasicHeaders(), additionalHeaders);
requestBuilder = OkHttpRequestDecorator.decorate(headers, requestBuilder, _decorator);
Map<String, List<String>> decorateHeaders = OkHttpRequestDecorator.decorate(headers, _decorator);
Map<String, List<String>> finalHeaders;
if (decorateHeaders.isEmpty()) {
finalHeaders = headers;
} else {
finalHeaders = decorateHeaders;
}
for (Map.Entry<String, List<String>> e : finalHeaders.entrySet()) {
for (String headerValue : e.getValue()) {
requestBuilder.addHeader(e.getKey(), headerValue);
}
}
requestBuilder.addHeader("Accept-Encoding", "gzip");
requestBuilder.addHeader("Content-Type", "application/json");
RequestBody postBody = RequestBody.create(MediaType.parse("application/json; charset=utf-16"), entity);
RequestBody postBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), entity);
requestBuilder.post(postBody);

Request request = requestBuilder.build();
@@ -172,7 +195,7 @@ protected Request getRequest(okhttp3.Request.Builder requestBuilder) {
return requestBuilder.build();
}

private Map<String, List<String>> buildBasicHeaders() {
protected Map<String, List<String>> buildBasicHeaders() {
Map<String, List<String>> h = new HashMap<>();
h.put(HEADER_API_KEY, Collections.singletonList("Bearer " + _apikey));
h.put(HEADER_CLIENT_VERSION, Collections.singletonList(_metadata.getSdkVersion()));
@@ -184,16 +207,17 @@ private Map<String, List<String>> buildBasicHeaders() {
return h;
}

private static Map<String, List<String>> mergeHeaders(Map<String, List<String>> headers,
protected Map<String, List<String>> mergeHeaders(Map<String, List<String>> headers,
Map<String, List<String>> toAdd) {
if (toAdd == null || toAdd.size() == 0) {
return headers;
}

for (Map.Entry<String, List<String>> entry : toAdd.entrySet()) {
headers.computeIfPresent(entry.getKey(),
(k, oldValue) -> Stream.concat(oldValue.stream(), entry.getValue().stream())
.collect(Collectors.toList()));
headers.put(entry.getKey(), entry.getValue());
// headers.computeIfPresent(entry.getKey(),
// (k, oldValue) -> Stream.concat(oldValue.stream(), entry.getValue().stream())
// .collect(Collectors.toList()));
}

return headers;
Original file line number Diff line number Diff line change
@@ -8,14 +8,8 @@

class OkHttpRequestDecorator {

public static okhttp3.Request.Builder decorate(Map<String, List<String>> headers, okhttp3.Request.Builder b,
public static Map<String, List<String>> decorate(Map<String, List<String>> headers,
RequestDecorator decorator) {
headers = decorator.decorateHeaders(new RequestContext(headers)).headers();
for (Map.Entry<String, List<String>> e : headers.entrySet()) {
for (String headerValue : e.getValue()) {
b.addHeader(e.getKey(), headerValue);
}
}
return b;
return decorator.decorateHeaders(new RequestContext(headers)).headers();
}
}
Original file line number Diff line number Diff line change
@@ -62,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>();
@@ -82,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");
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.split.httpmodules.okhttp;

import com.sun.tools.javac.util.StringUtils;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.reflect.Whitebox;

@@ -10,7 +9,6 @@
import io.split.client.impressions.Impression;
import io.split.client.utils.Json;
import io.split.client.utils.SDKMetadata;
import io.split.client.utils.Utils;
import io.split.client.dtos.SplitHttpResponse.Header;
import io.split.engine.common.FetchOptions;

@@ -58,7 +56,7 @@ public void testGetWithSpecialCharacters() throws IOException, InterruptedExcept
} finally {
br.close();
}
/*

server.enqueue(new MockResponse().setBody(body).addHeader("via", "HTTP/1.1 s_proxy_rio1"));
server.start();
HttpUrl baseUrl = server.url("/v1/");
@@ -76,16 +74,16 @@ public void testGetWithSpecialCharacters() throws IOException, InterruptedExcept
Map<String, List<String>> additionalHeaders = Collections.singletonMap("AdditionalHeader",
Collections.singletonList("add"));
FetchOptions fetchOptions = new FetchOptions.Builder().cacheControlHeaders(true).build();
RequestDecorator requestDecorator = new RequestDecorator(null);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).get(rootTarget, fetchOptions, additionalHeaders);
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder();
requestBuilder.url(rootTarget.toString());
PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder();
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setBasicHeaders(requestBuilder);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders();
Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata());
Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty");
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setAdditionalAndDecoratedHeaders(requestBuilder, additionalHeaders);
RequestDecorator requestDecorator = new RequestDecorator(null);
// Whitebox.setInternalState(okHttpClientImpl, "_requestDecorator", requestDecorator);
Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), additionalHeaders);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any());
PowerMockito.doReturn(requestBuilder.build()).when(okHttpClientImpl).getRequest(requestBuilder);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).getRequest(requestBuilder);
@@ -148,12 +146,12 @@ public void testGetErrors() throws IOException, InterruptedException {
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder();
requestBuilder.url(rootTarget.toString());
PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder();
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setBasicHeaders(requestBuilder);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders();
Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata());
Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty");
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setAdditionalAndDecoratedHeaders(requestBuilder, additionalHeaders);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), additionalHeaders);
RequestDecorator requestDecorator = new RequestDecorator(null);
// Whitebox.setInternalState(okHttpClientImpl, "_requestDecorator", requestDecorator);
Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any());

SplitHttpResponse splitHttpResponse = okHttpClientImpl.get(rootTarget,
@@ -215,11 +213,11 @@ public Map<String, List<String>> getHeaderOverrides(RequestContext context) {
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder();
requestBuilder.url(rootTarget.toString());
PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder();
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setBasicHeaders(requestBuilder);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders();
Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata());
Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty");
// Whitebox.setInternalState(okHttpClientImpl, "_requestDecorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setAdditionalAndDecoratedHeaders(requestBuilder, null);
Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), null);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any());
FetchOptions options = new FetchOptions.Builder().cacheControlHeaders(true).build();

@@ -230,9 +228,9 @@ public Map<String, List<String>> getHeaderOverrides(RequestContext context) {
Headers requestHeaders = request.getHeaders();

assertThat(requestHeaders.get("Cache-Control"), is(equalTo("no-cache")));
// assertThat(requestHeaders.get("first"), is(equalTo("1")));
// assertThat(requestHeaders.values("second"), is(equalTo(Arrays.asList("2.1","2.2"))));
// assertThat(requestHeaders.get("third"), is(equalTo("3")));
assertThat(requestHeaders.get("first"), is(equalTo("1")));
assertThat(requestHeaders.values("second"), is(equalTo(Arrays.asList("2.1","2.2"))));
assertThat(requestHeaders.get("third"), is(equalTo("3")));
Assert.assertEquals("/splitChanges?since=1234567", request.getPath());
assertThat(request.getMethod(), is(equalTo("GET")));
}
@@ -256,11 +254,11 @@ public void testException() throws URISyntaxException, IOException {
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder();
requestBuilder.url(rootTarget.toString());
PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder();
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setBasicHeaders(requestBuilder);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders();
Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata());
Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty");
Whitebox.setInternalState(okHttpClientImpl, "_requestDecorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setAdditionalAndDecoratedHeaders(requestBuilder, null);
Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), null);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any());
FetchOptions options = new FetchOptions.Builder().cacheControlHeaders(true).build();

@@ -294,11 +292,11 @@ public void testPost() throws IOException, InterruptedException {
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder();
requestBuilder.url(rootTarget.toString());
PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder();
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setBasicHeaders(requestBuilder);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders();
Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata());
Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty");
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setAdditionalAndDecoratedHeaders(requestBuilder, additionalHeaders);
// Whitebox.setInternalState(okHttpClientImpl, "_requestDecorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), additionalHeaders);
Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any());
// Send impressions
List<TestImpressions> toSend = Arrays.asList(new TestImpressions("t1", Arrays.asList(
@@ -358,11 +356,11 @@ public void testPostErrors() throws IOException, InterruptedException {
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder();
requestBuilder.url(rootTarget.toString());
PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder();
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setBasicHeaders(requestBuilder);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders();
Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata());
Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty");
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setAdditionalAndDecoratedHeaders(requestBuilder, additionalHeaders);
// Whitebox.setInternalState(okHttpClientImpl, "_requestDecorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), additionalHeaders);
Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any());

String data = Json.toJson("<>");
@@ -394,13 +392,14 @@ public void testPosttException() throws URISyntaxException, IOException {
Collections.singletonList("OPTIMIZED"));

okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder();
RequestDecorator requestDecorator = new RequestDecorator(null);
requestBuilder.url(rootTarget.toString());
PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder();
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setBasicHeaders(requestBuilder);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders();
Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata());
Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty");
PowerMockito.doCallRealMethod().when(okHttpClientImpl).setAdditionalAndDecoratedHeaders(requestBuilder, additionalHeaders);
Whitebox.setInternalState(okHttpClientImpl, "_requestDecorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), additionalHeaders);
Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator);
PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any());

String data = Json.toJson("<>");
@@ -409,12 +408,22 @@ public void testPosttException() throws URISyntaxException, IOException {

SplitHttpResponse splitHttpResponse = okHttpClientImpl.post(rootTarget, data,
additionalHeaders);
*/
}

private SDKMetadata metadata() {
return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP");
}

private Map<String, List<String>> buildBasicHeaders() {
Map<String, List<String>> h = new HashMap<>();
h.put("Authorization", Collections.singletonList("Bearer qwerty"));
h.put("SplitSDKVersion", Collections.singletonList(metadata().getSdkVersion()));
h.put("SplitSDKMachineIP", Collections.singletonList(metadata().getMachineIp()));
h.put("SplitSDKMachineName", Collections.singletonList(metadata().getMachineName()));
h.put("SplitSDKClientKey", Collections.singletonList("qwerty".length() > 4
? "qwerty".substring("qwerty".length() - 4)
: "qwerty"));
return h;
}

}
Original file line number Diff line number Diff line change
@@ -85,12 +85,12 @@ public void testCreateClient() throws Exception {
.then((Answer<OkHttpClientImpl>) invocationOnMock -> {
assertThat("qwerty", is(equalTo((String) invocationOnMock.getArguments()[0])));
assertThat(sdkMetadata, is(equalTo((SDKMetadata) invocationOnMock.getArguments()[1])));
// assertThat(requestDecorator, is(equalTo((RequestDecorator) invocationOnMock.getArguments()[2])));
assertThat(proxy, is(equalTo((Proxy) invocationOnMock.getArguments()[2])));
assertThat("bilal@bilal", is(equalTo((String) invocationOnMock.getArguments()[3])));
assertThat(false, is(equalTo((Boolean) invocationOnMock.getArguments()[4])));
assertThat(11000, is(equalTo((Integer) invocationOnMock.getArguments()[5])));
assertThat(12000, is(equalTo((Integer) invocationOnMock.getArguments()[6])));
assertThat(requestDecorator, is(equalTo((RequestDecorator) invocationOnMock.getArguments()[7])));
argsCaptured.set(true);
return mockclient;
}
Original file line number Diff line number Diff line change
@@ -35,12 +35,12 @@ public void testFactoryCreatingClient() throws Exception {
.then((Answer<OkHttpClientImpl>) invocationOnMock -> {
assertThat("qwerty", is(equalTo((String) invocationOnMock.getArguments()[0])));
assertThat((SDKMetadata) invocationOnMock.getArguments()[1], instanceOf(SDKMetadata.class));
// assertThat((RequestDecorator) invocationOnMock.getArguments()[2], instanceOf(RequestDecorator.class));
assertThat(proxy, is(equalTo((Proxy) invocationOnMock.getArguments()[2])));
assertThat("bilal@bilal", is(equalTo((String) invocationOnMock.getArguments()[3])));
assertThat(false, is(equalTo((Boolean) invocationOnMock.getArguments()[4])));
assertThat(11000, is(equalTo((Integer) invocationOnMock.getArguments()[5])));
assertThat(12000, is(equalTo((Integer) invocationOnMock.getArguments()[6])));
assertThat((RequestDecorator) invocationOnMock.getArguments()[7], instanceOf(RequestDecorator.class));
argsCaptured.set(true);
return mockclient;
}
2 changes: 1 addition & 1 deletion pluggable-storage/pom.xml
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
<version>4.13.0-rc3</version>
</parent>

<version>2.1.0</version>
<version>4.13.0-rc3</version>
<artifactId>pluggable-storage</artifactId>
<packaging>jar</packaging>
<name>Package for Pluggable Storage</name>
2 changes: 1 addition & 1 deletion redis-wrapper/pom.xml
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
<version>4.13.0-rc3</version>
</parent>
<artifactId>redis-wrapper</artifactId>
<version>3.1.0</version>
<version>4.13.0-rc3</version>
<packaging>jar</packaging>
<name>Package for Redis Wrapper Implementation</name>
<description>Implements Redis Pluggable Storage</description>

0 comments on commit 84eeea5

Please sign in to comment.