-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Revise ConfigCat provider (#280)
Signed-off-by: Adam Simon <[email protected]>
- Loading branch information
Showing
5 changed files
with
176 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AutoFixture.Xunit2; | ||
using ConfigCat.Client; | ||
|
@@ -12,14 +14,58 @@ namespace OpenFeature.Contrib.ConfigCat.Test | |
{ | ||
public class ConfigCatProviderTest | ||
{ | ||
const string TestConfigJson = | ||
@" | ||
{ | ||
""f"": { | ||
""isAwesomeFeatureEnabled"": { | ||
""t"": 0, | ||
""v"": { | ||
""b"": true | ||
} | ||
}, | ||
""isPOCFeatureEnabled"": { | ||
""t"": 0, | ||
""r"": [ | ||
{ | ||
""c"": [ | ||
{ | ||
""u"": { | ||
""a"": ""Email"", | ||
""c"": 2, | ||
""l"": [ | ||
""@example.com"" | ||
] | ||
} | ||
} | ||
], | ||
""s"": { | ||
""v"": { | ||
""b"": true | ||
} | ||
} | ||
} | ||
], | ||
""v"": { | ||
""b"": false | ||
} | ||
} | ||
} | ||
} | ||
"; | ||
|
||
[Theory] | ||
[AutoData] | ||
public void CreateConfigCatProvider_WithSdkKey_CreatesProviderInstanceSuccessfully(string sdkKey) | ||
public async void CreateConfigCatProvider_WithSdkKey_CreatesProviderInstanceSuccessfully(string sdkKey) | ||
{ | ||
var configCatProvider = | ||
new ConfigCatProvider(sdkKey, options => { options.FlagOverrides = BuildFlagOverrides(); }); | ||
|
||
await configCatProvider.InitializeAsync(EvaluationContext.Empty); | ||
|
||
Assert.NotNull(configCatProvider.Client); | ||
|
||
await configCatProvider.ShutdownAsync(); | ||
} | ||
|
||
[Theory] | ||
|
@@ -93,33 +139,70 @@ public async Task GetStructureValueAsync_ForFeature_ReturnExpectedResult(string | |
var configCatProvider = new ConfigCatProvider(sdkKey, | ||
options => { options.FlagOverrides = BuildFlagOverrides(("example-feature", defaultValue.AsString)); }); | ||
|
||
await configCatProvider.InitializeAsync(EvaluationContext.Empty); | ||
|
||
var result = await configCatProvider.ResolveStructureValueAsync("example-feature", defaultValue); | ||
|
||
Assert.Equal(defaultValue.AsString, result.Value.AsString); | ||
Assert.Equal("example-feature", result.FlagKey); | ||
Assert.Equal(ErrorType.None, result.ErrorType); | ||
|
||
await configCatProvider.ShutdownAsync(); | ||
} | ||
|
||
[Theory] | ||
[InlineAutoData("[email protected]", false)] | ||
[InlineAutoData("[email protected]", true)] | ||
public async Task OpenFeatureAPI_EndToEnd_Test(string email, bool expectedValue) | ||
{ | ||
var configCatProvider = new ConfigCatProvider("fake-67890123456789012/1234567890123456789012", options => | ||
{ options.ConfigFetcher = new FakeConfigFetcher(TestConfigJson); }); | ||
|
||
await OpenFeature.Api.Instance.SetProviderAsync(configCatProvider); | ||
|
||
var client = OpenFeature.Api.Instance.GetClient(); | ||
|
||
var evaluationContext = EvaluationContext.Builder() | ||
.Set("email", email) | ||
.Build(); | ||
|
||
var result = await client.GetBooleanDetailsAsync("isPOCFeatureEnabled", false, evaluationContext); | ||
|
||
Assert.Equal(expectedValue, result.Value); | ||
Assert.Equal("isPOCFeatureEnabled", result.FlagKey); | ||
Assert.Equal(ErrorType.None, result.ErrorType); | ||
|
||
await OpenFeature.Api.Instance.ShutdownAsync(); | ||
} | ||
|
||
private static async Task ExecuteResolveTest<T>(object value, T defaultValue, T expectedValue, string sdkKey, Func<ConfigCatProvider, string, T, Task<ResolutionDetails<T>>> resolveFunc) | ||
{ | ||
var configCatProvider = new ConfigCatProvider(sdkKey, | ||
options => { options.FlagOverrides = BuildFlagOverrides(("example-feature", value)); }); | ||
|
||
await configCatProvider.InitializeAsync(EvaluationContext.Empty); | ||
|
||
var result = await resolveFunc(configCatProvider, "example-feature", defaultValue); | ||
|
||
Assert.Equal(expectedValue, result.Value); | ||
Assert.Equal("example-feature", result.FlagKey); | ||
Assert.Equal(ErrorType.None, result.ErrorType); | ||
|
||
await configCatProvider.ShutdownAsync(); | ||
} | ||
|
||
private static async Task ExecuteResolveErrorTest<T>(object value, T defaultValue, ErrorType expectedErrorType, string sdkKey, Func<ConfigCatProvider, string, T, Task<ResolutionDetails<T>>> resolveFunc) | ||
{ | ||
var configCatProvider = new ConfigCatProvider(sdkKey, | ||
options => { options.FlagOverrides = BuildFlagOverrides(("example-feature", value)); }); | ||
|
||
await configCatProvider.InitializeAsync(EvaluationContext.Empty); | ||
|
||
var exception = await Assert.ThrowsAsync<FeatureProviderException>(() => resolveFunc(configCatProvider, "example-feature", defaultValue)); | ||
|
||
Assert.Equal(expectedErrorType, exception.ErrorType); | ||
|
||
await configCatProvider.ShutdownAsync(); | ||
} | ||
|
||
private static FlagOverrides BuildFlagOverrides(params (string key, object value)[] values) | ||
|
@@ -132,5 +215,22 @@ private static FlagOverrides BuildFlagOverrides(params (string key, object value | |
|
||
return FlagOverrides.LocalDictionary(dictionary, OverrideBehaviour.LocalOnly); | ||
} | ||
|
||
private sealed class FakeConfigFetcher : IConfigCatConfigFetcher | ||
{ | ||
private readonly string configJson; | ||
|
||
public FakeConfigFetcher(string configJson) | ||
{ | ||
this.configJson = configJson; | ||
} | ||
|
||
public void Dispose() { } | ||
|
||
public Task<FetchResponse> FetchAsync(FetchRequest request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(new FetchResponse(HttpStatusCode.OK, reasonPhrase: null, headers: Array.Empty<KeyValuePair<string, string>>(), this.configJson)); | ||
} | ||
} | ||
} | ||
} |