Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Nov 28, 2023
1 parent 5edaeb0 commit eac19a6
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public static class Builder {
private long connectionTimeout = -1;
private DevelopmentSslConfig developmentSslConfig = null;
private Context mHostAppContext;
private InternalAuthenticatorFactory<Authenticator> mInternalAuthenticatorFactory = new OkHttpAuthenticatorFactory();

public Builder setContext(Context context) {
mHostAppContext = context;
Expand Down Expand Up @@ -174,6 +173,7 @@ private OkHttpClient createOkHttpClient(Proxy proxy,
}

if (proxyAuthenticator != null) {
Logger.d("Setting proxy authenticator");
builder.proxyAuthenticator(proxyAuthenticator);
}

Expand Down Expand Up @@ -214,7 +214,7 @@ public Request authenticate(@Nullable Route route, @NonNull Response response) t
}

private okhttp3.Authenticator createAuthenticator(SplitAuthenticator authenticator) {
return mInternalAuthenticatorFactory.getAuthenticator(authenticator);
return new OkHttp3Authenticator(authenticator);
}

private void forceTls12OnOldAndroid(OkHttpClient.Builder okHttpBuilder, Context context) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.split.android.client.network;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;
import java.util.Map;

import io.split.android.client.utils.logger.Logger;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

public class OkHttp3Authenticator implements okhttp3.Authenticator {

private final SplitAuthenticator mSplitAuthenticator;

public OkHttp3Authenticator(SplitAuthenticator splitAuthenticator) {
mSplitAuthenticator = splitAuthenticator;
}

@Nullable
@Override
public Request authenticate(@Nullable Route route, @NonNull Response response) {

try {
SplitAuthenticatedRequest authenticatedRequestResult = mSplitAuthenticator.authenticate(new SplitAuthenticatedRequest(response));
if (authenticatedRequestResult == null ||
authenticatedRequestResult.getRequest() == null) {
return null;
}

Request.Builder builder = response.request()
.newBuilder();

if (authenticatedRequestResult.getHeaders() != null) {
for (Map.Entry<String, List<String>> header : authenticatedRequestResult.getHeaders().entrySet()) {
for (String value : header.getValue()) {
builder.addHeader(header.getKey(), value);
}
}
}

return builder.build();
} catch (Exception exception) {
Logger.e("Error authenticating request: ", exception.getMessage());
return null;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,49 @@
import java.util.List;
import java.util.Map;

import okhttp3.Request;
import okhttp3.Response;

public class SplitAuthenticatedRequest implements AuthenticatedRequest<Response> {
public class SplitAuthenticatedRequest implements AuthenticatedRequest<Request> {

private final Response mResponse;
private final int mStatusCode;
private Request mRequest;

SplitAuthenticatedRequest(Response request) {
mResponse = request;
SplitAuthenticatedRequest(Response response) {
mStatusCode = response.code();
mRequest = response.request();
}

@Override
public void setHeader(@NonNull String name, @NonNull String value) {
mResponse.newBuilder().header(name, value);
mRequest = mRequest.newBuilder().header(name, value).build();
}

@Nullable
@Override
public String getHeader(@NonNull String name) {
return mResponse.header(name);
return mRequest.header(name);
}

@Nullable
@Override
public Map<String, List<String>> getHeaders() {
return mResponse.headers().toMultimap();
return mRequest.headers().toMultimap();
}

@Nullable
@Override
public Response getRequest() {
return mResponse;
public Request getRequest() {
return mRequest;
}

@Override
public int getStatusCode() {
return mResponse.code();
return mStatusCode;
}

@Override
public String getRequestUrl() {
return mResponse.request().url().toString();
return mRequest.url().toString();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package io.split.android.client.network;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.Objects;

import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

public class OkHttpAuthenticatorTest {

private OkHttp3Authenticator mAuthenticator;
private SplitAuthenticator mSplitAuthenticator;

@Before
public void setUp() {
mSplitAuthenticator = mock(SplitAuthenticator.class);
mAuthenticator = new OkHttp3Authenticator(mSplitAuthenticator);
}

@Test
public void callingAuthenticateCallsAuthenticateOnTheSplitAuthenticator() throws IOException {
mAuthenticator.authenticate(mock(Route.class), mock(Response.class));

verify(mSplitAuthenticator).authenticate(argThat(Objects::nonNull));
}

@Test
public void resultIsNullIfSplitAuthenticatorReturnsNull() throws IOException {
Request authenticate = mAuthenticator.authenticate(mock(Route.class), mock(Response.class));

assertNull(authenticate);
}

@Test
public void resultIsNullIfSplitAuthenticatorReturnsNullRequest() throws IOException {
SplitAuthenticatedRequest mockAuthRequest = mock(SplitAuthenticatedRequest.class);
when(mockAuthRequest.getRequest()).thenReturn(null);
when(mSplitAuthenticator.authenticate(any())).thenReturn(mockAuthRequest);

Request authenticate = mAuthenticator.authenticate(mock(Route.class), mock(Response.class));

assertNull(authenticate);
}

@Test
public void headersFromAuthenticationAreNotAddedToResultWhenTheyAreNull() throws IOException {
Response mockResponse = mock(Response.class);
Request mockRequest = mock(Request.class);
Request.Builder mockBuilder = mock(Request.Builder.class);
Request mockResult = mock(Request.class);

when(mockRequest.newBuilder()).thenReturn(mockBuilder);
when(mockResponse.request()).thenReturn(mockRequest);
when(mockBuilder.build()).thenReturn(mockResult);

SplitAuthenticatedRequest mockAuthRequest = mock(SplitAuthenticatedRequest.class);
when(mockAuthRequest.getRequest()).thenReturn(mockRequest);
when(mockAuthRequest.getHeaders()).thenReturn(null);
when(mSplitAuthenticator.authenticate(any())).thenReturn(mockAuthRequest);

Request result = mAuthenticator.authenticate(mock(Route.class), mockResponse);

verify(mockRequest).newBuilder();
verify(mockBuilder, times(0)).addHeader(any(), any());
verify(mockBuilder).build();
assertEquals(mockResult, result);
}

@Test
public void exceptionInSplitAuthenticatorCausesResultToBeNull() throws IOException {
when(mSplitAuthenticator.authenticate(any())).thenThrow(new RuntimeException());

Request result = mAuthenticator.authenticate(mock(Route.class), mock(Response.class));

assertNull(result);
}

@Test
public void authorizationHeadersAreAddedToResultRequest() {
Response mockResponse = mock(Response.class);
Request mockRequest = mock(Request.class);
Request.Builder mockBuilder = mock(Request.Builder.class);
Request mockResult = mock(Request.class);

when(mockRequest.newBuilder()).thenReturn(mockBuilder);
when(mockResponse.request()).thenReturn(mockRequest);
when(mockBuilder.build()).thenReturn(mockResult);

SplitAuthenticatedRequest mockAuthRequest = mock(SplitAuthenticatedRequest.class);
when(mockAuthRequest.getRequest()).thenReturn(mockRequest);
when(mockAuthRequest.getHeaders()).thenReturn(Collections.singletonMap("Authorization", Collections.singletonList("Bearer 1234567890")));
when(mSplitAuthenticator.authenticate(any())).thenReturn(mockAuthRequest);

Request result = mAuthenticator.authenticate(mock(Route.class), mockResponse);

verify(mockRequest).newBuilder();
verify(mockBuilder).addHeader("Authorization", "Bearer 1234567890");
verify(mockBuilder).build();
assertNotNull(result);
}
}

0 comments on commit eac19a6

Please sign in to comment.