Skip to content

Commit

Permalink
New authenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 7, 2023
1 parent 488dba3 commit 5461f25
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

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

interface AuthenticatedRequest<T> {
Expand All @@ -14,9 +13,7 @@ interface AuthenticatedRequest<T> {
String getHeader(@NonNull String name);

@Nullable
Map<String, List<String>> getHeaders();

int getStatusCode();
Map<String, String> getHeaders();

@Nullable
String getRequestUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,40 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import okhttp3.Request;
import okhttp3.Response;
public class SplitAuthenticatedRequest implements AuthenticatedRequest<HttpURLConnection> {

public class SplitAuthenticatedRequest implements AuthenticatedRequest<Request> {
private final String mUrl;
private final Map<String, String> mHeaders = new ConcurrentHashMap<>();

private final int mStatusCode;
private Request mRequest;

SplitAuthenticatedRequest(Response response) {
mStatusCode = response.code();
mRequest = response.request();
SplitAuthenticatedRequest(HttpURLConnection connection) {
mUrl = (connection != null && connection.getURL() != null) ? connection.getURL().toString() : null;
}

@Override
public void setHeader(@NonNull String name, @NonNull String value) {
if (mRequest == null) {
return;
}

mRequest = mRequest.newBuilder().header(name, value).build();
mHeaders.put(name, value);
}

@Nullable
@Override
public String getHeader(@NonNull String name) {
if (mRequest == null) {
return null;
}

return mRequest.header(name);
return mHeaders.get(name);
}

@Nullable
@Override
public Map<String, List<String>> getHeaders() {
if (mRequest == null) {
return null;
}

return mRequest.headers().toMultimap();
}

@Override
public int getStatusCode() {
return mStatusCode;
public Map<String, String> getHeaders() {
return new HashMap<>(mHeaders);
}

@Nullable
@Override
public String getRequestUrl() {
if (mRequest == null) {
return null;
}

return mRequest.url().toString();
return mUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.split.android.client.network;

import java.net.HttpURLConnection;
import java.util.Map;

class SplitUrlConnectionAuthenticator {

private final SplitAuthenticator mProxyAuthenticator;

SplitUrlConnectionAuthenticator(SplitAuthenticator splitAuthenticator) {
mProxyAuthenticator = splitAuthenticator;
}

HttpURLConnection authenticate(HttpURLConnection connection) {
SplitAuthenticatedRequest authenticatedRequest = mProxyAuthenticator.authenticate(new SplitAuthenticatedRequest(connection));
if (authenticatedRequest != null) {
Map<String, String> headers = authenticatedRequest.getHeaders();

if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
if (entry == null || entry.getKey() == null || entry.getValue() == null) {
continue;
}

connection.addRequestProperty(entry.getKey(), entry.getValue());
}

return connection;
}
}

return connection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ public AuthenticatedRequest<MockRequest> authenticate(@NonNull AuthenticatedRequ
};

AuthenticatedMockRequest request = new AuthenticatedMockRequest(new MockRequest());
Map<String, List<String>> initialHeaders = new HashMap<>(request.getHeaders());
Map<String, String> initialHeaders = new HashMap<>(request.getHeaders());

splitAuthenticator.authenticate(request);

Map<String, List<String>> finalHeaders = new HashMap<>(request.getHeaders());
Map<String, String> finalHeaders = new HashMap<>(request.getHeaders());

assertEquals(2, initialHeaders.size());
assertTrue(initialHeaders.containsKey("header1"));
assertTrue(initialHeaders.containsKey("header2"));
assertEquals(Collections.singletonList("value1"), initialHeaders.get("header1"));
assertEquals(Collections.singletonList("value2"), initialHeaders.get("header2"));
assertEquals("value1", initialHeaders.get("header1"));
assertEquals("value2", initialHeaders.get("header2"));
assertEquals(3, finalHeaders.size());
assertTrue(finalHeaders.containsKey("header1"));
assertTrue(finalHeaders.containsKey("header2"));
assertTrue(finalHeaders.containsKey("new-header"));
assertEquals(Collections.singletonList("value1"), finalHeaders.get("header1"));
assertEquals(Collections.singletonList("value2"), finalHeaders.get("header2"));
assertEquals(Collections.singletonList("value"), finalHeaders.get("new-header"));
assertEquals("value1", finalHeaders.get("header1"));
assertEquals("value2", finalHeaders.get("header2"));
assertEquals("value", finalHeaders.get("new-header"));
}

private static class AuthenticatedMockRequest implements AuthenticatedRequest<MockRequest> {
Expand All @@ -68,46 +68,33 @@ public String getHeader(@NonNull String name) {

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

@Override
public int getStatusCode() {
return 0;
}

@Override
public String getRequestUrl() {
return null;
}
}

private static class MockRequest {
private final Map<String, List<String>> mHeaders = new ConcurrentHashMap<>();
private final Map<String, String> mHeaders = new ConcurrentHashMap<>();

MockRequest() {
mHeaders.put("header1", Collections.singletonList("value1"));
mHeaders.put("header2", Collections.singletonList("value2"));
mHeaders.put("header1", "value1");
mHeaders.put("header2", "value2");
}

public void setHeader(@NonNull String name, @NonNull String value) {
if (mHeaders.get(name) != null) {
mHeaders.get(name).add(value);
} else {
mHeaders.put(name, Collections.singletonList(value));
}
mHeaders.put(name, value);
}

public String getHeader(@NonNull String name) {
if (mHeaders.get(name) != null && mHeaders.get(name).size() > 0) {
return mHeaders.get(name).get(0);
}

return null;
return mHeaders.get(name);
}

public Map<String, List<String>> getHeaders() {
public Map<String, String> getHeaders() {
return mHeaders;
}
}
Expand Down

This file was deleted.

Loading

0 comments on commit 5461f25

Please sign in to comment.