Skip to content

Commit

Permalink
Add tests to increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Aug 27, 2024
1 parent 96c1aa1 commit b3f6d4c
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/ConfigCat.Client.Tests/HttpConfigFetcherTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<string, string>("CF-RAY", "CF-12345"),
new KeyValuePair<string, string>("ETag", "\"abc\""),
};

var configFetcherMock = new Mock<IConfigCatConfigFetcher>();
configFetcherMock
.Setup(m => m.FetchAsync(It.IsAny<FetchRequest>(), It.IsAny<CancellationToken>()))
.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<LogEvent>();
var logger = LoggingHelper.CreateCapturingLogger(logEvents, LogLevel.Info);

var responseHeader = new[]
{
new KeyValuePair<string, string>("ETag", "\"abc\""),
new KeyValuePair<string, string>("CF-RAY", "CF-12345"),
};

var configFetcherMock = new Mock<IConfigCatConfigFetcher>();
configFetcherMock
.Setup(m => m.FetchAsync(It.IsAny<FetchRequest>(), It.IsAny<CancellationToken>()))
.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);
}
}

0 comments on commit b3f6d4c

Please sign in to comment.