-
Notifications
You must be signed in to change notification settings - Fork 17
feat: send user agent header #77
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
Open
gazreese
wants to merge
3
commits into
Flagsmith:main
Choose a base branch
from
foresightmobile:feature/send-user-agent-header
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
FlagsmithClient/src/test/java/com/flagsmith/UserAgentTests.kt
|
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. Is it possible to somehow mock the version so it can be seen in at least one test? All current tests only verify |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the Android machinery used to obtain the Flagsmith client version more reliable than iOS'? I'm asking considering the equivalent pull request to iOS (Flagsmith/flagsmith-ios-client#95) falls back to a hardcoded version string when obtaining the lib version fails, and I wonder why the same strategy isn't used here as well. (I'm not asking we implement it)