From 11b8c2ea54855f79955b8d441a91f511406a266c Mon Sep 17 00:00:00 2001 From: Gaston Thea Date: Mon, 4 Dec 2023 14:11:50 -0300 Subject: [PATCH] Internal authenticator tests --- .../SplitUrlConnectionAuthenticatorTest.java | 80 ++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/src/test/java/io/split/android/client/network/SplitUrlConnectionAuthenticatorTest.java b/src/test/java/io/split/android/client/network/SplitUrlConnectionAuthenticatorTest.java index c1240e231..f163100ef 100644 --- a/src/test/java/io/split/android/client/network/SplitUrlConnectionAuthenticatorTest.java +++ b/src/test/java/io/split/android/client/network/SplitUrlConnectionAuthenticatorTest.java @@ -1,8 +1,18 @@ package io.split.android.client.network; +import static org.junit.Assert.assertSame; +import static org.mockito.ArgumentMatchers.any; 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.net.HttpURLConnection; +import java.util.HashMap; +import java.util.Map; public class SplitUrlConnectionAuthenticatorTest { @@ -15,5 +25,73 @@ public void setUp() { mAuthenticator = new SplitUrlConnectionAuthenticator(mSplitAuthenticator); } - //TODO + @Test + public void originalConnectionIsReturnedWhenAuthenticationResultIsNull() { + when(mSplitAuthenticator.authenticate(any())).thenReturn(null); + + HttpURLConnection mockConnection = mock(HttpURLConnection.class); + HttpURLConnection authenticate = mAuthenticator.authenticate(mockConnection); + + assertSame(authenticate, mockConnection); + } + + @Test + public void headersFromAuthenticatedRequestAreAddedToConnection() { + SplitAuthenticatedRequest authRequest = mock(SplitAuthenticatedRequest.class); + Map authHeaders = new HashMap<>(); + authHeaders.put("header", "value"); + authHeaders.put("header2", "value2"); + when(authRequest.getHeaders()).thenReturn(authHeaders); + when(mSplitAuthenticator.authenticate(any())).thenReturn(authRequest); + + HttpURLConnection mockConnection = mock(HttpURLConnection.class); + mAuthenticator.authenticate(mockConnection); + + verify(mockConnection).addRequestProperty("header", "value"); + verify(mockConnection).addRequestProperty("header2", "value2"); + } + + @Test + public void nullHeadersAreIgnored() { + SplitAuthenticatedRequest authRequest = mock(SplitAuthenticatedRequest.class); + Map authHeaders = new HashMap<>(); + authHeaders.put("header", "value"); + authHeaders.put("header2", null); + when(authRequest.getHeaders()).thenReturn(authHeaders); + when(mSplitAuthenticator.authenticate(any())).thenReturn(authRequest); + + HttpURLConnection mockConnection = mock(HttpURLConnection.class); + mAuthenticator.authenticate(mockConnection); + + verify(mockConnection).addRequestProperty("header", "value"); + verify(mockConnection, times(0)).addRequestProperty("header2", null); + } + + @Test + public void nullKeysInHeadersAreIgnored() { + SplitAuthenticatedRequest authRequest = mock(SplitAuthenticatedRequest.class); + Map authHeaders = new HashMap<>(); + authHeaders.put("header", "value"); + authHeaders.put(null, "value2"); + when(authRequest.getHeaders()).thenReturn(authHeaders); + when(mSplitAuthenticator.authenticate(any())).thenReturn(authRequest); + + HttpURLConnection mockConnection = mock(HttpURLConnection.class); + mAuthenticator.authenticate(mockConnection); + + verify(mockConnection).addRequestProperty("header", "value"); + verify(mockConnection, times(0)).addRequestProperty(null, "value2"); + } + + @Test + public void noHeadersAreAddedWhenHeadersFromAuthRequestIsNull() { + SplitAuthenticatedRequest authRequest = mock(SplitAuthenticatedRequest.class); + when(authRequest.getHeaders()).thenReturn(null); + when(mSplitAuthenticator.authenticate(any())).thenReturn(authRequest); + + HttpURLConnection mockConnection = mock(HttpURLConnection.class); + mAuthenticator.authenticate(mockConnection); + + verify(mockConnection, times(0)).addRequestProperty(any(), any()); + } }