Skip to content

Commit 7a49bc0

Browse files
authored
Merge pull request #514 from Countly/header-dynamic-call
addCustomNetworkRequestHeaders
2 parents b4810dc + 294ad21 commit 7a49bc0

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* `SAFE_AREA` mode: Omits status bar, navigation bar and cutouts when displaying webviews.
77
* Added a new init config option `disableGradualRequestCleaner()` to change request queue overflow behavior. When enabled, all overflowing requests (plus one slot) are removed at once instead of being cleaned gradually in limited batches.
88

9+
* Added a new method `requestQueue().addCustomNetworkRequestHeaders(Map<String,String>)` for providing or overriding custom headers after init
10+
911
* Immediate requests now will be run by parallel executor instead of serial by default.
1012

1113
## 25.4.4
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

sdk/src/main/java/ly/count/android/sdk/Countly.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,49 @@ public void setLoggingEnabled(final boolean enableLogging) {
11571157
L.d("Enabling logging");
11581158
}
11591159

1160+
/**
1161+
* To add new header key/value pairs or override existing ones.
1162+
* A null or empty map is ignored. Null or empty keys, as well as null values, are ignored.
1163+
* Subsequent requests (including those created after overriding) will contain the updated header set.
1164+
*
1165+
* @param customHeaderValues map of header key/value pairs to add/override
1166+
* @return Returns the same Countly instance for convenient chaining
1167+
*/
1168+
/* package */ synchronized void addCustomNetworkRequestHeaders(Map<String, String> customHeaderValues) {
1169+
if (!isInitialized()) {
1170+
L.e("[addCustomNetworkRequestHeaders] SDK must be initialised before calling this method");
1171+
return;
1172+
}
1173+
1174+
if (customHeaderValues == null || customHeaderValues.isEmpty()) {
1175+
L.d("[addCustomNetworkRequestHeaders] Provided map was null or empty, ignoring");
1176+
return;
1177+
}
1178+
1179+
if (requestHeaderCustomValues == null) {
1180+
requestHeaderCustomValues = new HashMap<>();
1181+
}
1182+
1183+
int added = 0;
1184+
int overridden = 0;
1185+
for (Map.Entry<String, String> entry : customHeaderValues.entrySet()) {
1186+
String k = entry.getKey();
1187+
String v = entry.getValue();
1188+
if (k == null || k.isEmpty() || v == null) {
1189+
continue; // skip invalid entries
1190+
}
1191+
if (requestHeaderCustomValues.containsKey(k)) {
1192+
overridden++;
1193+
} else {
1194+
added++;
1195+
}
1196+
requestHeaderCustomValues.put(k, v);
1197+
}
1198+
1199+
connectionQueue_.setRequestHeaderCustomValues(requestHeaderCustomValues);
1200+
L.i("[addCustomNetworkRequestHeaders] Added:" + added + " Overridden:" + overridden + " TotalNow:" + requestHeaderCustomValues.size());
1201+
}
1202+
11601203
/**
11611204
* Check if logging has been enabled internally in the SDK
11621205
*

sdk/src/main/java/ly/count/android/sdk/ModuleRequestQueue.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,5 +456,21 @@ public void recordMetrics(@Nullable Map<String, String> metricsOverride) {
456456
recordMetricsInternal(tempMetricsOverride);
457457
}
458458
}
459+
460+
/**
461+
* To add new header key/value pairs or override existing ones.
462+
* A null or empty map is ignored. Null or empty keys, as well as null values,
463+
* are ignored.
464+
* Subsequent requests (including those created after overriding) will contain
465+
* the updated header set.
466+
*
467+
* @param customHeaderValues header key/value pairs to add or override
468+
*/
469+
public void addCustomNetworkRequestHeaders(@Nullable Map<String, String> customHeaderValues) {
470+
synchronized (_cly) {
471+
L.i("[RequestQueue] addCustomNetworkRequestHeaders, Calling addCustomNetworkRequestHeaders");
472+
_cly.addCustomNetworkRequestHeaders(customHeaderValues);
473+
}
474+
}
459475
}
460476
}

0 commit comments

Comments
 (0)