Skip to content

Commit e7b671f

Browse files
committed
Review updates
1 parent b184964 commit e7b671f

File tree

2 files changed

+29
-68
lines changed

2 files changed

+29
-68
lines changed

FlagsmithClient/src/main/java/com/flagsmith/internal/FlagsmithRetrofitService.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,19 @@ interface FlagsmithRetrofitService {
4343
private const val USER_AGENT_PREFIX = "flagsmith-kotlin-android-sdk"
4444

4545
private fun getUserAgent(context: Context?): String {
46+
val sdkVersion = getSdkVersion()
47+
return "$USER_AGENT_PREFIX/$sdkVersion"
48+
}
49+
50+
private fun getSdkVersion(): String {
4651
return try {
47-
val packageInfo = context?.packageManager?.getPackageInfo(context.packageName, 0)
48-
val version = packageInfo?.versionName ?: "unknown"
49-
"$USER_AGENT_PREFIX/$version"
52+
// Try to get version from BuildConfig
53+
val buildConfigClass = Class.forName("com.flagsmith.kotlin.BuildConfig")
54+
val versionField = buildConfigClass.getField("VERSION_NAME")
55+
versionField.get(null) as String
5056
} catch (e: Exception) {
51-
"$USER_AGENT_PREFIX/unknown"
57+
// Fallback to hardcoded version if BuildConfig is not available
58+
"unknown"
5259
}
5360
}
5461

FlagsmithClient/src/test/java/com/flagsmith/UserAgentTests.kt

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
11
package com.flagsmith
22

3-
import android.content.Context
4-
import android.content.pm.PackageInfo
5-
import android.content.pm.PackageManager
63
import com.flagsmith.entities.Trait
74
import com.flagsmith.mockResponses.MockEndpoint
85
import com.flagsmith.mockResponses.mockResponseFor
96
import kotlinx.coroutines.runBlocking
107
import org.junit.After
11-
import org.junit.Assert.assertEquals
128
import org.junit.Assert.assertTrue
139
import org.junit.Before
1410
import org.junit.Test
1511
import org.mockserver.integration.ClientAndServer
1612
import org.mockserver.model.HttpRequest.request
17-
import org.mockito.Mock
18-
import org.mockito.Mockito
19-
import org.mockito.junit.MockitoJUnitRunner
20-
import org.junit.runner.RunWith
2113

22-
@RunWith(MockitoJUnitRunner::class)
2314
class UserAgentTests {
2415

2516
private lateinit var mockServer: ClientAndServer
2617
private lateinit var flagsmith: Flagsmith
2718

28-
@Mock
29-
private lateinit var mockContext: Context
30-
31-
@Mock
32-
private lateinit var mockPackageManager: PackageManager
33-
34-
@Mock
35-
private lateinit var mockPackageInfo: PackageInfo
36-
3719
@Before
3820
fun setup() {
3921
mockServer = ClientAndServer.startClientAndServer()
@@ -46,19 +28,13 @@ class UserAgentTests {
4628

4729
@Test
4830
fun testUserAgentHeaderSentWithValidVersion() {
49-
// Given - Use a realistic app version (not SDK version)
50-
// The User-Agent shows the APP's version, not the SDK's version
51-
// This helps Flagsmith support team identify which app version is making requests
52-
val expectedAppVersion = "2.4.1"
53-
mockPackageInfo.versionName = expectedAppVersion
54-
Mockito.`when`(mockContext.packageManager).thenReturn(mockPackageManager)
55-
Mockito.`when`(mockContext.packageName).thenReturn("com.test.app")
56-
Mockito.`when`(mockPackageManager.getPackageInfo(mockContext.packageName, 0)).thenReturn(mockPackageInfo)
57-
31+
// Given - The User-Agent now shows SDK version or "unknown" (not app version)
32+
// This is because getUserAgent() method was updated to return SDK version
33+
// In tests, BuildConfig is not available, so it returns "unknown"
5834
flagsmith = Flagsmith(
5935
environmentKey = "test-key",
6036
baseUrl = "http://localhost:${mockServer.localPort}",
61-
context = mockContext,
37+
context = null,
6238
enableAnalytics = false,
6339
cacheConfig = FlagsmithCacheConfig(enableCache = false)
6440
)
@@ -71,12 +47,12 @@ class UserAgentTests {
7147
assertTrue(result.isSuccess)
7248
}
7349

74-
// Then - Verify User-Agent contains the APP's version, not SDK version
50+
// Then - Verify User-Agent contains "unknown" since BuildConfig is not available in tests
7551
mockServer.verify(
7652
request()
7753
.withPath("/flags/")
7854
.withMethod("GET")
79-
.withHeader("User-Agent", "flagsmith-kotlin-android-sdk/$expectedAppVersion")
55+
.withHeader("User-Agent", "flagsmith-kotlin-android-sdk/unknown")
8056
)
8157
}
8258

@@ -110,16 +86,11 @@ class UserAgentTests {
11086

11187
@Test
11288
fun testUserAgentHeaderSentWithExceptionDuringVersionRetrieval() {
113-
// Given
114-
Mockito.`when`(mockContext.packageManager).thenReturn(mockPackageManager)
115-
Mockito.`when`(mockContext.packageName).thenReturn("com.test.app")
116-
Mockito.`when`(mockPackageManager.getPackageInfo(mockContext.packageName, 0))
117-
.thenThrow(PackageManager.NameNotFoundException("Package not found"))
118-
89+
// Given - Even with context, getUserAgent() now returns SDK version or "unknown"
11990
flagsmith = Flagsmith(
12091
environmentKey = "test-key",
12192
baseUrl = "http://localhost:${mockServer.localPort}",
122-
context = mockContext,
93+
context = null,
12394
enableAnalytics = false,
12495
cacheConfig = FlagsmithCacheConfig(enableCache = false)
12596
)
@@ -143,16 +114,11 @@ class UserAgentTests {
143114

144115
@Test
145116
fun testUserAgentHeaderSentWithNullVersionName() {
146-
// Given
147-
mockPackageInfo.versionName = null
148-
Mockito.`when`(mockContext.packageManager).thenReturn(mockPackageManager)
149-
Mockito.`when`(mockContext.packageName).thenReturn("com.test.app")
150-
Mockito.`when`(mockPackageManager.getPackageInfo(mockContext.packageName, 0)).thenReturn(mockPackageInfo)
151-
117+
// Given - getUserAgent() now returns SDK version or "unknown" regardless of context
152118
flagsmith = Flagsmith(
153119
environmentKey = "test-key",
154120
baseUrl = "http://localhost:${mockServer.localPort}",
155-
context = mockContext,
121+
context = null,
156122
enableAnalytics = false,
157123
cacheConfig = FlagsmithCacheConfig(enableCache = false)
158124
)
@@ -176,17 +142,11 @@ class UserAgentTests {
176142

177143
@Test
178144
fun testUserAgentHeaderSentWithIdentityRequest() {
179-
// Given
180-
val expectedVersion = "2.1.0"
181-
mockPackageInfo.versionName = expectedVersion
182-
Mockito.`when`(mockContext.packageManager).thenReturn(mockPackageManager)
183-
Mockito.`when`(mockContext.packageName).thenReturn("com.test.app")
184-
Mockito.`when`(mockPackageManager.getPackageInfo(mockContext.packageName, 0)).thenReturn(mockPackageInfo)
185-
145+
// Given - getUserAgent() now returns SDK version or "unknown"
186146
flagsmith = Flagsmith(
187147
environmentKey = "test-key",
188148
baseUrl = "http://localhost:${mockServer.localPort}",
189-
context = mockContext,
149+
context = null,
190150
enableAnalytics = false,
191151
cacheConfig = FlagsmithCacheConfig(enableCache = false)
192152
)
@@ -199,29 +159,23 @@ class UserAgentTests {
199159
assertTrue(result.isSuccess)
200160
}
201161

202-
// Then
162+
// Then - Verify User-Agent contains "unknown" since BuildConfig is not available in tests
203163
mockServer.verify(
204164
request()
205165
.withPath("/identities/")
206166
.withMethod("GET")
207167
.withQueryStringParameter("identifier", "test-user")
208-
.withHeader("User-Agent", "flagsmith-kotlin-android-sdk/$expectedVersion")
168+
.withHeader("User-Agent", "flagsmith-kotlin-android-sdk/unknown")
209169
)
210170
}
211171

212172
@Test
213173
fun testUserAgentHeaderSentWithTraitRequest() {
214-
// Given
215-
val expectedAppVersion = "3.0.1"
216-
mockPackageInfo.versionName = expectedAppVersion
217-
Mockito.`when`(mockContext.packageManager).thenReturn(mockPackageManager)
218-
Mockito.`when`(mockContext.packageName).thenReturn("com.test.app")
219-
Mockito.`when`(mockPackageManager.getPackageInfo(mockContext.packageName, 0)).thenReturn(mockPackageInfo)
220-
174+
// Given - getUserAgent() now returns SDK version or "unknown"
221175
flagsmith = Flagsmith(
222176
environmentKey = "test-key",
223177
baseUrl = "http://localhost:${mockServer.localPort}",
224-
context = mockContext,
178+
context = null,
225179
enableAnalytics = false,
226180
cacheConfig = FlagsmithCacheConfig(enableCache = false)
227181
)
@@ -234,12 +188,12 @@ class UserAgentTests {
234188
assertTrue(result.isSuccess)
235189
}
236190

237-
// Then - Verify the traits request has correct User-Agent
191+
// Then - Verify the traits request has correct User-Agent with "unknown" since BuildConfig is not available in tests
238192
mockServer.verify(
239193
request()
240194
.withPath("/identities/")
241195
.withMethod("POST")
242-
.withHeader("User-Agent", "flagsmith-kotlin-android-sdk/$expectedAppVersion")
196+
.withHeader("User-Agent", "flagsmith-kotlin-android-sdk/unknown")
243197
)
244198
}
245199
}

0 commit comments

Comments
 (0)