|
| 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