Skip to content

Commit 8b9e072

Browse files
committed
Additional tests
1 parent e16ad94 commit 8b9e072

File tree

3 files changed

+114
-4
lines changed

3 files changed

+114
-4
lines changed

src/main/java/io/split/android/client/network/HttpRequestHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static void checkPins(HttpURLConnection connection, @Nullable CertificateChecker
126126

127127
private static void addHeaders(HttpURLConnection request, Map<String, String> headers) {
128128
for (Map.Entry<String, String> entry : headers.entrySet()) {
129-
if (entry == null) {
129+
if (entry == null || entry.getKey() == null) {
130130
continue;
131131
}
132132

src/main/java/io/split/android/client/network/ProxyCacertConnectionHandler.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ public HttpResponse executeRequest(@NonNull HttpProxy httpProxy,
103103
serverCertificates
104104
);
105105
} finally {
106-
// Note: We don't close finalSocket here if it's different from tunnelSocket
107-
// because when autoClose=true in createSocket, the tunnelSocket will be closed
108-
// when finalSocket is closed. If we close both, we'd get "socket closed" errors.
106+
// If we have are tunelling, finalSocket is the tunnel socket
109107
if (finalSocket != null && finalSocket != tunnelSocket) {
110108
try {
111109
finalSocket.close();
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package io.split.android.client.network;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.Mockito.never;
5+
import static org.mockito.Mockito.times;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.when;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.mockito.Mock;
12+
import org.mockito.MockitoAnnotations;
13+
14+
import java.lang.reflect.Method;
15+
import java.net.HttpURLConnection;
16+
import java.net.Proxy;
17+
import java.net.URL;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import javax.net.ssl.HttpsURLConnection;
22+
import javax.net.ssl.SSLPeerUnverifiedException;
23+
import javax.net.ssl.SSLSocketFactory;
24+
25+
public class HttpRequestHelperTest {
26+
27+
@Mock
28+
private HttpURLConnection mockConnection;
29+
@Mock
30+
private HttpsURLConnection mockHttpsConnection;
31+
@Mock
32+
private URL mockUrl;
33+
@Mock
34+
private SplitUrlConnectionAuthenticator mockAuthenticator;
35+
@Mock
36+
private SSLSocketFactory mockSslSocketFactory;
37+
@Mock
38+
private DevelopmentSslConfig mockDevelopmentSslConfig;
39+
@Mock
40+
private CertificateChecker mockCertificateChecker;
41+
42+
@Before
43+
public void setUp() throws Exception {
44+
MockitoAnnotations.openMocks(this);
45+
when(mockUrl.openConnection()).thenReturn(mockConnection);
46+
when(mockUrl.openConnection(any(Proxy.class))).thenReturn(mockConnection);
47+
when(mockAuthenticator.authenticate(any(HttpURLConnection.class))).thenReturn(mockConnection);
48+
}
49+
50+
@Test
51+
public void addHeaders() throws Exception {
52+
Map<String, String> headers = new HashMap<>();
53+
headers.put("Content-Type", "application/json");
54+
headers.put("Authorization", "Bearer token123");
55+
headers.put(null, "This should be ignored");
56+
57+
Method addHeadersMethod = HttpRequestHelper.class.getDeclaredMethod(
58+
"addHeaders", HttpURLConnection.class, Map.class);
59+
addHeadersMethod.setAccessible(true);
60+
addHeadersMethod.invoke(null, mockConnection, headers);
61+
62+
verify(mockConnection).addRequestProperty("Content-Type", "application/json");
63+
verify(mockConnection).addRequestProperty("Authorization", "Bearer token123");
64+
verify(mockConnection, never()).addRequestProperty(null, "This should be ignored");
65+
}
66+
67+
@Test
68+
public void applyTimeouts() {
69+
HttpRequestHelper.applyTimeouts(5000, 3000, mockConnection);
70+
verify(mockConnection).setReadTimeout(5000);
71+
verify(mockConnection).setConnectTimeout(3000);
72+
73+
HttpRequestHelper.applyTimeouts(0, 0, mockConnection);
74+
verify(mockConnection, times(1)).setReadTimeout(any(Integer.class));
75+
verify(mockConnection, times(1)).setConnectTimeout(any(Integer.class));
76+
77+
HttpRequestHelper.applyTimeouts(-1000, -500, mockConnection);
78+
verify(mockConnection, times(1)).setReadTimeout(any(Integer.class));
79+
verify(mockConnection, times(1)).setConnectTimeout(any(Integer.class));
80+
}
81+
82+
@Test
83+
public void applySslConfigWithDevelopmentSslConfig() {
84+
when(mockDevelopmentSslConfig.getSslSocketFactory()).thenReturn(mockSslSocketFactory);
85+
86+
HttpRequestHelper.applySslConfig(null, mockDevelopmentSslConfig, mockHttpsConnection);
87+
88+
verify(mockHttpsConnection).setSSLSocketFactory(mockSslSocketFactory);
89+
verify(mockHttpsConnection).setHostnameVerifier(any());
90+
}
91+
92+
@Test
93+
public void pinsAreCheckedWithCertificateChecker() throws SSLPeerUnverifiedException {
94+
HttpRequestHelper.checkPins(mockHttpsConnection, mockCertificateChecker);
95+
96+
verify(mockCertificateChecker).checkPins(mockHttpsConnection);
97+
}
98+
99+
@Test
100+
public void pinsAreNotCheckedWithoutCertificateChecker() throws SSLPeerUnverifiedException {
101+
HttpRequestHelper.checkPins(mockHttpsConnection, null);
102+
103+
verify(mockCertificateChecker, never()).checkPins(any());
104+
}
105+
106+
@Test
107+
public void pinsAreNotCheckedForNonHttpsConnections() throws SSLPeerUnverifiedException {
108+
HttpRequestHelper.checkPins(mockConnection, mockCertificateChecker);
109+
110+
verify(mockCertificateChecker, never()).checkPins(any());
111+
}
112+
}

0 commit comments

Comments
 (0)