|
| 1 | +package ly.count.android.sdk; |
| 2 | + |
| 3 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 4 | +import java.net.URLConnection; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests for runtime custom header manipulation via Countly.addCustomNetworkRequestHeaders(Map<String,String>) |
| 15 | + */ |
| 16 | +@RunWith(AndroidJUnit4.class) |
| 17 | +public class CustomHeaderRuntimeTests { |
| 18 | + |
| 19 | + Countly mCountly; |
| 20 | + |
| 21 | + @Before |
| 22 | + public void setUp() { |
| 23 | + final CountlyStore countlyStore = new CountlyStore(TestUtils.getContext(), mock(ModuleLog.class)); |
| 24 | + countlyStore.clear(); |
| 25 | + |
| 26 | + mCountly = new Countly(); |
| 27 | + mCountly.init(new CountlyConfig(TestUtils.getContext(), "appkey", "http://test.count.ly").setDeviceId("1234").setLoggingEnabled(true)); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testRuntimeAddAndOverrideHeaders() throws Exception { |
| 32 | + // Add initial headers at runtime |
| 33 | + Map<String, String> initial = new HashMap<>(); |
| 34 | + initial.put("X-First", "One"); |
| 35 | + initial.put("X-Second", "Two"); |
| 36 | + mCountly.requestQueue().addCustomNetworkRequestHeaders(initial); |
| 37 | + |
| 38 | + // Override one and add a new one |
| 39 | + Map<String, String> second = new HashMap<>(); |
| 40 | + second.put("X-Second", "TwoOverride"); |
| 41 | + second.put("X-Third", "Three"); |
| 42 | + mCountly.requestQueue().addCustomNetworkRequestHeaders(second); |
| 43 | + |
| 44 | + ConnectionProcessor cp = new ConnectionProcessor( |
| 45 | + "http://test.count.ly", |
| 46 | + mCountly.countlyStore, |
| 47 | + mCountly.config_.deviceIdProvider, |
| 48 | + mCountly.config_.configProvider, |
| 49 | + mCountly.connectionQueue_.requestInfoProvider, |
| 50 | + null, |
| 51 | + mCountly.requestHeaderCustomValues, |
| 52 | + mCountly.L, |
| 53 | + mCountly.config_.healthTracker, |
| 54 | + mock(Runnable.class) |
| 55 | + ); |
| 56 | + |
| 57 | + URLConnection urlConnection = cp.urlConnectionForServerRequest("a=b", null); |
| 58 | + |
| 59 | + Assert.assertEquals("One", urlConnection.getRequestProperty("X-First")); |
| 60 | + Assert.assertEquals("TwoOverride", urlConnection.getRequestProperty("X-Second")); |
| 61 | + Assert.assertEquals("Three", urlConnection.getRequestProperty("X-Third")); |
| 62 | + } |
| 63 | +} |
0 commit comments