diff --git a/src/ConfigCat.Client.Tests/HttpConfigFetcherTests.cs b/src/ConfigCat.Client.Tests/HttpConfigFetcherTests.cs index a223e0fc..225c754b 100644 --- a/src/ConfigCat.Client.Tests/HttpConfigFetcherTests.cs +++ b/src/ConfigCat.Client.Tests/HttpConfigFetcherTests.cs @@ -1,10 +1,15 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Net; using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; +using ConfigCat.Client.Configuration; using ConfigCat.Client.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; namespace ConfigCat.Client.Tests; @@ -232,4 +237,78 @@ public async Task HttpConfigFetcher_FetchAsync_PendingOperationShouldBeJoined(bo Assert.IsNull(configFetcher.pendingFetch); } } + + [TestMethod] + public async Task CustomConfigFetcher_Success() + { + // Arrange + + var configJson = File.ReadAllText(Path.Combine(new ConfigLocation.LocalFile("data", "sample_v5.json").GetRealLocation())); + + var responseHeader = new[] + { + new KeyValuePair("CF-RAY", "CF-12345"), + new KeyValuePair("ETag", "\"abc\""), + }; + + var configFetcherMock = new Mock(); + configFetcherMock + .Setup(m => m.FetchAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new FetchResponse(HttpStatusCode.OK, reasonPhrase: null, responseHeader, configJson)); + + using var client = new ConfigCatClient("test-67890123456789012/1234567890123456789012", new ConfigCatClientOptions + { + ConfigFetcher = configFetcherMock.Object + }); + + // Act + + var value = await client.GetValueAsync("stringDefaultCat", ""); + + // Assert + + Assert.AreEqual("Cat", value); + } + + [TestMethod] + public async Task CustomConfigFetcher_Failure() + { + // Arrange + + var logEvents = new List(); + var logger = LoggingHelper.CreateCapturingLogger(logEvents, LogLevel.Info); + + var responseHeader = new[] + { + new KeyValuePair("ETag", "\"abc\""), + new KeyValuePair("CF-RAY", "CF-12345"), + }; + + var configFetcherMock = new Mock(); + configFetcherMock + .Setup(m => m.FetchAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new FetchResponse(HttpStatusCode.Forbidden, "Forbidden", responseHeader)); + + using var client = new ConfigCatClient("test-67890123456789012/1234567890123456789012", new ConfigCatClientOptions + { + ConfigFetcher = configFetcherMock.Object, + Logger = logger.AsWrapper() + }); + + // Act + + await client.ForceRefreshAsync(); + + // Assert + + var errors = logEvents.Where(evt => evt.EventId == 1100).ToArray(); + Assert.AreEqual(1, errors.Length); + + var error = errors[0].Message; + Assert.AreEqual(1, error.ArgValues.Length); + Assert.IsTrue(error.ArgValues[0] is string); + + var rayId = (string)error.ArgValues[0]!; + StringAssert.Contains(errors[0].Message.InvariantFormattedMessage, rayId); + } }