-
Notifications
You must be signed in to change notification settings - Fork 15
feat: Send a standard User-Agent: sdk-name/version header
#172
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c1c11f4
Initial plan
Copilot d29045a
Add User-Agent header to all HTTP requests
Copilot eaf7b3a
Fix SdkVersion to handle edge cases properly
Copilot e6e4736
Make SdkVersion thread-safe and fix build number check
Copilot 33e3630
Fix nullable reference warnings in tests
Copilot c03ba97
Run dotnet format to apply code style formatting
Copilot cab12bd
Address code review feedback and add SdkVersion tests
Copilot a337abb
Add explicit using Flagsmith directive to test files
Copilot a5fca48
Switch from Assembly-based version to hard-coded constant with releas…
Copilot d8c68d4
Remove redundant tests and use consistent assertions
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Flagsmith; | ||
| using Xunit; | ||
|
|
||
| namespace Flagsmith.FlagsmithClientTest | ||
| { | ||
| public class SdkVersionTest | ||
| { | ||
| [Fact] | ||
| public void TestGetUserAgentReturnsExpectedVersion() | ||
| { | ||
| // x-release-please-start-version | ||
| string expectedVersion = "8.0.2"; | ||
| // x-release-please-end | ||
|
|
||
| // When | ||
| var userAgent = SdkVersion.GetUserAgent(); | ||
|
|
||
| // Then | ||
| Assert.Equal($"flagsmith-dotnet-sdk/{expectedVersion}", userAgent); | ||
| } | ||
| } | ||
| } |
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,134 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Flagsmith; | ||
| using Moq; | ||
| using Xunit; | ||
|
|
||
| namespace Flagsmith.FlagsmithClientTest | ||
| { | ||
| public class UserAgentTest | ||
| { | ||
| [Fact] | ||
| public async Task TestUserAgentHeaderIsSentInGetEnvironmentFlags() | ||
| { | ||
| // Given | ||
| HttpRequestMessage capturedRequest = null!; | ||
|
|
||
| var httpClientMock = new Mock<HttpClient>(); | ||
| httpClientMock.Setup(x => x.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>())) | ||
| .Callback<HttpRequestMessage, CancellationToken>((request, token) => | ||
| { | ||
| capturedRequest = request; | ||
| }) | ||
| .ReturnsAsync(new HttpResponseMessage | ||
| { | ||
| StatusCode = HttpStatusCode.OK, | ||
| Content = new StringContent(Fixtures.ApiFlagResponse) | ||
| }); | ||
|
|
||
| var config = new FlagsmithConfiguration | ||
| { | ||
| EnvironmentKey = Fixtures.ApiKey, | ||
| HttpClient = httpClientMock.Object | ||
| }; | ||
|
|
||
| var client = new FlagsmithClient(config); | ||
|
|
||
| // When | ||
| await client.GetEnvironmentFlags(); | ||
|
|
||
| // Then | ||
| Assert.NotNull(capturedRequest); | ||
| Assert.True(capturedRequest.Headers.Contains("User-Agent")); | ||
|
|
||
| var userAgentValues = capturedRequest.Headers.GetValues("User-Agent").ToList(); | ||
| Assert.Single(userAgentValues); | ||
|
|
||
| var userAgent = userAgentValues[0]; | ||
| Assert.Equal(SdkVersion.GetUserAgent(), userAgent); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestUserAgentHeaderIsSentInGetIdentityFlags() | ||
| { | ||
| // Given | ||
| HttpRequestMessage capturedRequest = null!; | ||
|
|
||
| var httpClientMock = new Mock<HttpClient>(); | ||
| httpClientMock.Setup(x => x.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>())) | ||
| .Callback<HttpRequestMessage, CancellationToken>((request, token) => | ||
| { | ||
| capturedRequest = request; | ||
| }) | ||
| .ReturnsAsync(new HttpResponseMessage | ||
| { | ||
| StatusCode = HttpStatusCode.OK, | ||
| Content = new StringContent(Fixtures.ApiIdentityResponse) | ||
| }); | ||
|
|
||
| var config = new FlagsmithConfiguration | ||
| { | ||
| EnvironmentKey = Fixtures.ApiKey, | ||
| HttpClient = httpClientMock.Object | ||
| }; | ||
|
|
||
| var client = new FlagsmithClient(config); | ||
|
|
||
| // When | ||
| await client.GetIdentityFlags("test-identity"); | ||
|
|
||
| // Then | ||
| Assert.NotNull(capturedRequest); | ||
| Assert.True(capturedRequest.Headers.Contains("User-Agent")); | ||
|
|
||
| var userAgentValues = capturedRequest.Headers.GetValues("User-Agent").ToList(); | ||
| Assert.Single(userAgentValues); | ||
|
|
||
| var userAgent = userAgentValues[0]; | ||
| Assert.Equal(SdkVersion.GetUserAgent(), userAgent); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestUserAgentHeaderIsSentInAnalyticsFlush() | ||
| { | ||
| // Given | ||
| HttpRequestMessage capturedRequest = null!; | ||
|
|
||
| var httpClientMock = new Mock<HttpClient>(); | ||
| httpClientMock.Setup(x => x.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>())) | ||
| .Callback<HttpRequestMessage, CancellationToken>((request, token) => | ||
| { | ||
| capturedRequest = request; | ||
| }) | ||
| .ReturnsAsync(new HttpResponseMessage | ||
| { | ||
| StatusCode = HttpStatusCode.OK | ||
| }); | ||
|
|
||
| var analyticsProcessor = new AnalyticsProcessor( | ||
| httpClientMock.Object, | ||
| Fixtures.ApiKey, | ||
| Fixtures.ApiUrl | ||
| ); | ||
|
|
||
| // When | ||
| await analyticsProcessor.TrackFeature("test_feature"); | ||
| await analyticsProcessor.Flush(); | ||
|
|
||
| // Then | ||
| Assert.NotNull(capturedRequest); | ||
| Assert.True(capturedRequest.Headers.Contains("User-Agent")); | ||
|
|
||
| var userAgentValues = capturedRequest.Headers.GetValues("User-Agent").ToList(); | ||
| Assert.Single(userAgentValues); | ||
|
|
||
| var userAgent = userAgentValues[0]; | ||
| Assert.Equal(SdkVersion.GetUserAgent(), userAgent); | ||
| } | ||
| } | ||
| } |
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
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,22 @@ | ||
| #nullable enable | ||
|
|
||
| namespace Flagsmith | ||
| { | ||
| /// <summary> | ||
| /// Provides SDK version information for User-Agent header | ||
| /// </summary> | ||
| public static class SdkVersion | ||
| { | ||
| // x-release-please-start-version | ||
| private const string Version = "8.0.2"; | ||
| // x-release-please-end | ||
|
|
||
| /// <summary> | ||
| /// Gets the SDK version in the format "flagsmith-dotnet-sdk/version" | ||
| /// </summary> | ||
| public static string GetUserAgent() | ||
| { | ||
| return $"flagsmith-dotnet-sdk/{Version}"; | ||
| } | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.