Skip to content

Commit

Permalink
User agent format update (#195)
Browse files Browse the repository at this point in the history
* Trivial update to user agent string to improve splunk boards visibility into react native

* Added unit test of user agent string format

---------

Co-authored-by: Evan Masseau <>
  • Loading branch information
evan-masseau authored Oct 30, 2024
1 parent 798d03d commit ce74a1a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ internal object DeviceProperties {
}

val userAgent: String by lazy {
"$applicationLabel/$appVersion ($applicationId; build:$appVersionCode; $platform $osVersion) klaviyo-android/$sdkVersion"
val sdkAgent = "klaviyo-${sdkName.replace("_","-")}"
"$applicationLabel/$appVersion ($applicationId; build:$appVersionCode; $platform $osVersion) $sdkAgent/$sdkVersion"
}

private val packageInfo: PackageInfo by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkObject
import io.mockk.verify
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Test

Expand Down Expand Up @@ -52,6 +53,12 @@ internal class DevicePropertiesTest : BaseTest() {
versionCode = mockVersionCode
}

@After
override fun cleanup() {
unmockDeviceProperties()
super.cleanup()
}

@Test
fun `getVersionCodeCompat detects platform properly`() {
setFinalStatic(Build.VERSION::class.java.getField("SDK_INT"), 23)
Expand All @@ -66,4 +73,24 @@ internal class DevicePropertiesTest : BaseTest() {
mockPackageInfo.longVersionCode
}
}

@Test
fun `User agent reflects SDK name override`() {
mockDeviceProperties()
// Use some more realistic values for this, so the expected user agent string actually matches our regexes
every { DeviceProperties.applicationLabel } returns "MockApp"
every { DeviceProperties.appVersion } returns "1.0.0"
every { DeviceProperties.appVersionCode } returns "2"
every { DeviceProperties.sdkVersion } returns "3.0.0"
every { DeviceProperties.applicationId } returns "com.mock.app"
every { DeviceProperties.platform } returns "Android"
every { DeviceProperties.osVersion } returns "4"
every { DeviceProperties.sdkName } returns "cross_platform"
every { DeviceProperties.userAgent } answers { callOriginal() }

assertEquals(
"MockApp/1.0.0 (com.mock.app; build:2; Android 4) klaviyo-cross-platform/3.0.0",
DeviceProperties.userAgent
)
}
}

0 comments on commit ce74a1a

Please sign in to comment.