-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
182 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
src/main/java/io/split/android/client/network/InternalAuthenticatorFactory.java
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/main/java/io/split/android/client/network/OkHttp3Authenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
src/main/java/io/split/android/client/network/OkHttpAuthenticatorFactory.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
src/test/java/io/split/android/client/network/OkHttpAuthenticatorFactoryTest.java
This file was deleted.
Oops, something went wrong.
116 changes: 116 additions & 0 deletions
116
src/test/java/io/split/android/client/network/OkHttpAuthenticatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |