-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Send a standard user-agent header #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| package com.flagsmith | ||
|
|
||
| import com.flagsmith.entities.Trait | ||
| import com.flagsmith.mockResponses.MockEndpoint | ||
| import com.flagsmith.mockResponses.mockResponseFor | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.After | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.mockserver.integration.ClientAndServer | ||
| import org.mockserver.model.HttpRequest.request | ||
|
|
||
| class UserAgentTests { | ||
|
|
||
| private lateinit var mockServer: ClientAndServer | ||
| private lateinit var flagsmith: Flagsmith | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| mockServer = ClientAndServer.startClientAndServer() | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| mockServer.stop() | ||
| } | ||
|
|
||
| @Test | ||
| fun testUserAgentHeaderSentWithValidVersion() { | ||
| // Given - The User-Agent now shows SDK version or "unknown" (not app version) | ||
| // This is because getUserAgent() method was updated to return SDK version | ||
| // In tests, BuildConfig is not available, so it returns "unknown" | ||
| flagsmith = Flagsmith( | ||
| environmentKey = "test-key", | ||
| baseUrl = "http://localhost:${mockServer.localPort}", | ||
| context = null, | ||
| enableAnalytics = false, | ||
| cacheConfig = FlagsmithCacheConfig(enableCache = false) | ||
| ) | ||
|
|
||
| mockServer.mockResponseFor(MockEndpoint.GET_FLAGS) | ||
|
|
||
| // When | ||
| runBlocking { | ||
| val result = flagsmith.getFeatureFlagsSync() | ||
| assertTrue(result.isSuccess) | ||
| } | ||
|
|
||
| // Then - Verify User-Agent contains "unknown" since BuildConfig is not available in tests | ||
| mockServer.verify( | ||
| request() | ||
| .withPath("/flags/") | ||
| .withMethod("GET") | ||
| .withHeader("User-Agent", "flagsmith-kotlin-android-sdk/unknown") | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testUserAgentHeaderSentWithNullContext() { | ||
| // Given | ||
| flagsmith = Flagsmith( | ||
| environmentKey = "test-key", | ||
| baseUrl = "http://localhost:${mockServer.localPort}", | ||
| context = null, | ||
| enableAnalytics = false, | ||
| cacheConfig = FlagsmithCacheConfig(enableCache = false) | ||
| ) | ||
|
|
||
| mockServer.mockResponseFor(MockEndpoint.GET_FLAGS) | ||
|
|
||
| // When | ||
| runBlocking { | ||
| val result = flagsmith.getFeatureFlagsSync() | ||
| assertTrue(result.isSuccess) | ||
| } | ||
|
|
||
| // Then | ||
| mockServer.verify( | ||
| request() | ||
| .withPath("/flags/") | ||
| .withMethod("GET") | ||
| .withHeader("User-Agent", "flagsmith-kotlin-android-sdk/unknown") | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testUserAgentHeaderSentWithExceptionDuringVersionRetrieval() { | ||
| // Given - Even with context, getUserAgent() now returns SDK version or "unknown" | ||
| flagsmith = Flagsmith( | ||
| environmentKey = "test-key", | ||
| baseUrl = "http://localhost:${mockServer.localPort}", | ||
| context = null, | ||
| enableAnalytics = false, | ||
| cacheConfig = FlagsmithCacheConfig(enableCache = false) | ||
| ) | ||
|
|
||
| mockServer.mockResponseFor(MockEndpoint.GET_FLAGS) | ||
|
|
||
| // When | ||
| runBlocking { | ||
| val result = flagsmith.getFeatureFlagsSync() | ||
| assertTrue(result.isSuccess) | ||
| } | ||
|
|
||
| // Then | ||
| mockServer.verify( | ||
| request() | ||
| .withPath("/flags/") | ||
| .withMethod("GET") | ||
| .withHeader("User-Agent", "flagsmith-kotlin-android-sdk/unknown") | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testUserAgentHeaderSentWithNullVersionName() { | ||
| // Given - getUserAgent() now returns SDK version or "unknown" regardless of context | ||
| flagsmith = Flagsmith( | ||
| environmentKey = "test-key", | ||
| baseUrl = "http://localhost:${mockServer.localPort}", | ||
| context = null, | ||
| enableAnalytics = false, | ||
| cacheConfig = FlagsmithCacheConfig(enableCache = false) | ||
| ) | ||
|
|
||
| mockServer.mockResponseFor(MockEndpoint.GET_FLAGS) | ||
|
|
||
| // When | ||
| runBlocking { | ||
| val result = flagsmith.getFeatureFlagsSync() | ||
| assertTrue(result.isSuccess) | ||
| } | ||
|
|
||
| // Then | ||
| mockServer.verify( | ||
| request() | ||
| .withPath("/flags/") | ||
| .withMethod("GET") | ||
| .withHeader("User-Agent", "flagsmith-kotlin-android-sdk/unknown") | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testUserAgentHeaderSentWithIdentityRequest() { | ||
| // Given - getUserAgent() now returns SDK version or "unknown" | ||
| flagsmith = Flagsmith( | ||
| environmentKey = "test-key", | ||
| baseUrl = "http://localhost:${mockServer.localPort}", | ||
| context = null, | ||
| enableAnalytics = false, | ||
| cacheConfig = FlagsmithCacheConfig(enableCache = false) | ||
| ) | ||
|
|
||
| mockServer.mockResponseFor(MockEndpoint.GET_IDENTITIES) | ||
|
|
||
| // When | ||
| runBlocking { | ||
| val result = flagsmith.getIdentitySync("test-user") | ||
| assertTrue(result.isSuccess) | ||
| } | ||
|
|
||
| // Then - Verify User-Agent contains "unknown" since BuildConfig is not available in tests | ||
| mockServer.verify( | ||
| request() | ||
| .withPath("/identities/") | ||
| .withMethod("GET") | ||
| .withQueryStringParameter("identifier", "test-user") | ||
| .withHeader("User-Agent", "flagsmith-kotlin-android-sdk/unknown") | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testUserAgentHeaderSentWithTraitRequest() { | ||
| // Given - getUserAgent() now returns SDK version or "unknown" | ||
| flagsmith = Flagsmith( | ||
| environmentKey = "test-key", | ||
| baseUrl = "http://localhost:${mockServer.localPort}", | ||
| context = null, | ||
| enableAnalytics = false, | ||
| cacheConfig = FlagsmithCacheConfig(enableCache = false) | ||
| ) | ||
|
|
||
| mockServer.mockResponseFor(MockEndpoint.SET_TRAIT) | ||
|
|
||
| // When | ||
| runBlocking { | ||
| val result = flagsmith.setTraitSync(Trait(key = "test-key", traitValue = "test-value"), "test-user") | ||
| assertTrue(result.isSuccess) | ||
| } | ||
|
|
||
| // Then - Verify the traits request has correct User-Agent with "unknown" since BuildConfig is not available in tests | ||
| mockServer.verify( | ||
| request() | ||
| .withPath("/identities/") | ||
| .withMethod("POST") | ||
| .withHeader("User-Agent", "flagsmith-kotlin-android-sdk/unknown") | ||
| ) | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing Test Coverage: SSE and Analytics Great test coverage for REST API calls! However, the User-Agent interceptor is also added to:
Recommendation: Add tests to verify User-Agent is sent with SSE and analytics requests: @Test
fun testUserAgentHeaderSentWithSSEConnection() {
// Test that User-Agent is included when connecting to SSE endpoint
}
@Test
fun testUserAgentHeaderSentWithAnalyticsRequest() {
// Test that User-Agent is included when posting analytics
}This ensures complete coverage of all network request types.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be a total pain, I think we can leave this |
||
Uh oh!
There was an error while loading. Please reload this page.