Skip to content

Commit 14eb6a7

Browse files
committed
Add support for custom header when setting flags
1 parent 4f524ec commit 14eb6a7

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

sdk/src/main/java/com/amplitude/experiment/DefaultExperimentClient.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,9 @@ internal class DefaultExperimentClient internal constructor(
435435
libraryVersion = BuildConfig.VERSION_NAME,
436436
timeoutMillis = config.fetchTimeoutMillis,
437437
),
438+
customRequestHeaders = user?.let {
439+
config.customRequestHeaders?.invoke(it)
440+
} ?: mapOf()
438441
) { flags ->
439442
synchronized(this.flags) {
440443
this.flags.clear()

sdk/src/main/java/com/amplitude/experiment/FlagApi.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal data class GetFlagsOptions(
2626
internal interface FlagApi {
2727
fun getFlags(
2828
options: GetFlagsOptions? = null,
29+
customRequestHeaders: Map<String, String> = mapOf(),
2930
callback: ((Map<String, EvaluationFlag>) -> Unit)? = null,
3031
): Future<Map<String, EvaluationFlag>>
3132
}
@@ -37,6 +38,7 @@ internal class SdkFlagApi(
3738
) : FlagApi {
3839
override fun getFlags(
3940
options: GetFlagsOptions?,
41+
customRequestHeaders: Map<String, String>,
4042
callback: ((Map<String, EvaluationFlag>) -> Unit)?,
4143
): Future<Map<String, EvaluationFlag>> {
4244
val url =
@@ -49,6 +51,11 @@ internal class SdkFlagApi(
4951
Request.Builder()
5052
.get()
5153
.url(url).addHeader("Authorization", "Api-Key $deploymentKey")
54+
.apply {
55+
customRequestHeaders.forEach { (name, value) ->
56+
addHeader(name, value)
57+
}
58+
}
5259

5360
options?.let {
5461
if (it.libraryName.isNotEmpty() && it.libraryVersion.isNotEmpty()) {

sdk/src/test/java/com/amplitude/experiment/ExperimentClientTest.kt

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ class ExperimentClientTest {
5454
private var mockStorage = MockStorage()
5555
private val testUser = ExperimentUser(userId = "test_user")
5656

57-
private val serverVariant = Variant(key = "on", value = "on", payload = "payload", metadata = mapOf("evaluationId" to ""))
57+
private val serverVariant = Variant(
58+
key = "on",
59+
value = "on",
60+
payload = "payload",
61+
metadata = mapOf("evaluationId" to "")
62+
)
5863
private val fallbackVariant = Variant(key = "fallback", value = "fallback")
5964
private val initialVariant = Variant(key = "initial", value = "initial")
6065
private val inlineVariant = Variant(key = "inline", value = "inline")
@@ -452,7 +457,12 @@ class ExperimentClientTest {
452457
debug = true,
453458
exposureTrackingProvider = exposureTrackingProvider,
454459
source = Source.INITIAL_VARIANTS,
455-
initialVariants = mapOf("flagKey" to Variant(key = "variant", expKey = "experimentKey")),
460+
initialVariants = mapOf(
461+
"flagKey" to Variant(
462+
key = "variant",
463+
expKey = "experimentKey"
464+
)
465+
),
456466
),
457467
OkHttpClient(),
458468
mockStorage,
@@ -1357,13 +1367,15 @@ class ExperimentClientTest {
13571367
}
13581368

13591369
@Test
1360-
fun `test config custom request headers added, http call includes headers`() {
1370+
fun `test config custom request headers added, http call on fetch includes headers`() {
13611371
val mockHttpClient = mockk<OkHttpClient>()
1372+
val testServerUrlHost = "api.test-server-url.com"
13621373
val testCountry = "South Africa"
13631374
val client =
13641375
DefaultExperimentClient(
13651376
API_KEY,
13661377
ExperimentConfig(
1378+
serverUrl = "https://$testServerUrlHost",
13671379
customRequestHeaders = { user ->
13681380
mapOf("country" to user.country!!)
13691381
}
@@ -1375,9 +1387,39 @@ class ExperimentClientTest {
13751387

13761388
client.fetch(ExperimentUser(country = testCountry))
13771389

1378-
verify {
1390+
verify(exactly = 1) {
1391+
mockHttpClient.newCall(match {
1392+
it.url.host == testServerUrlHost &&
1393+
it.headers["country"] == testCountry
1394+
})
1395+
}
1396+
}
1397+
1398+
@Test
1399+
fun `test config custom request headers added, http call on start for flags for flags includes headers`() {
1400+
val mockHttpClient = mockk<OkHttpClient>()
1401+
val testFlagsHost = "api.test-flags-server-url.com"
1402+
val testCountry = "South Africa"
1403+
val client =
1404+
DefaultExperimentClient(
1405+
API_KEY,
1406+
ExperimentConfig(
1407+
flagsServerUrl = "https://$testFlagsHost",
1408+
customRequestHeaders = { user ->
1409+
mapOf("country" to user.country!!)
1410+
}
1411+
),
1412+
mockHttpClient,
1413+
mockStorage,
1414+
Experiment.executorService,
1415+
)
1416+
1417+
client.start(ExperimentUser(country = testCountry))
1418+
1419+
verify(exactly = 1) {
13791420
mockHttpClient.newCall(match {
1380-
it.headers["country"] == testCountry
1421+
it.url.host == testFlagsHost &&
1422+
it.headers["country"] == testCountry
13811423
})
13821424
}
13831425
}

0 commit comments

Comments
 (0)