-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacheauthnunit.cs
109 lines (87 loc) · 3.45 KB
/
cacheauthnunit.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
public class ApiClient
{
private readonly HttpClient _httpClient;
private readonly IMemoryCache _cache;
private readonly string _authToken;
public ApiClient(HttpClient httpClient, IMemoryCache cache, string authToken)
{
_httpClient = httpClient;
_cache = cache;
_authToken = authToken;
}
public async Task<string> GetDataAsync(string endpoint)
{
if (_cache.TryGetValue(endpoint, out string cachedData))
{
return cachedData;
}
var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
request.Headers.Add("Authorization", $"Bearer {_authToken}");
var response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var data = await response.Content.ReadAsStringAsync();
// Cache the result for future requests
_cache.Set(endpoint, data);
return data;
}
}
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Moq;
using Moq.Protected;
using NUnit.Framework;
[TestFixture]
public class ApiClientTests
{
[Test]
public async Task GetDataAsync_UsesCache_IfDataIsCached()
{
// Arrange
var cache = new MemoryCache(new MemoryCacheOptions());
var cachedValue = "cached result";
cache.Set("https://example.com/data", cachedValue);
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var apiClient = new ApiClient(httpClient, cache, "test-token");
// Act
var result = await apiClient.GetDataAsync("https://example.com/data");
// Assert
Assert.AreEqual(cachedValue, result);
mockHttpMessageHandler.Protected()
.Verify("SendAsync", Times.Never(), ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>());
}
[Test]
public async Task GetDataAsync_CallsApi_AndAddsToCache()
{
// Arrange
var cache = new MemoryCache(new MemoryCacheOptions());
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("api response")
});
var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var apiClient = new ApiClient(httpClient, cache, "test-token");
// Act
var result = await apiClient.GetDataAsync("https://example.com/data");
// Assert
Assert.AreEqual("api response", result);
Assert.IsTrue(cache.TryGetValue("https://example.com/data", out string cachedData));
Assert.AreEqual("api response", cachedData);
mockHttpMessageHandler.Protected()
.Verify("SendAsync", Times.Once(), ItExpr.Is<HttpRequestMessage>(req =>
req.Method == HttpMethod.Get &&
req.RequestUri == new System.Uri("https://example.com/data") &&
req.Headers.Authorization.ToString() == "Bearer test-token"
), ItExpr.IsAny<CancellationToken>());
}
}