-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathHealthMonitorExchangeClientTests.cs
More file actions
84 lines (75 loc) · 3.47 KB
/
Copy pathHealthMonitorExchangeClientTests.cs
File metadata and controls
84 lines (75 loc) · 3.47 KB
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using HealthMonitoring.Model;
using HealthMonitoring.Monitors.Core.Exchange.Client;
using HealthMonitoring.Security;
using HealthMonitoring.TimeManagement;
using Moq;
using Xunit;
namespace HealthMonitoring.Monitors.Core.UnitTests
{
public class HealthMonitorExchangeClientTests
{
class TestableHealthMonitorExchangeClient : HealthMonitorExchangeClient
{
private readonly HttpMessageHandler _handler;
public TestableHealthMonitorExchangeClient(HttpMessageHandler handler, ITimeCoordinator timeCoordinator, ICredentialsProvider credentialsProvider)
: base("http://mock", timeCoordinator, credentialsProvider)
{
_handler = handler;
}
protected override HttpClient CreateClient()
{
return new HttpClient(_handler);
}
}
class MockHttpMessageHandler : HttpMessageHandler
{
private readonly List<Tuple<Func<HttpRequestMessage, bool>, Func<HttpRequestMessage, HttpResponseMessage>>> _handlers = new List<Tuple<Func<HttpRequestMessage, bool>, Func<HttpRequestMessage, HttpResponseMessage>>>();
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
foreach (var handler in _handlers)
{
if (handler.Item1(request))
return Task.FromResult(handler.Item2(request));
}
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound));
}
public void Setup(Func<HttpRequestMessage, bool> predicate, Func<HttpRequestMessage, HttpResponseMessage> action)
{
_handlers.Add(Tuple.Create(predicate, action));
}
public void Setup(string url, Func<HttpRequestMessage, HttpResponseMessage> action)
{
Setup(req => req.RequestUri.ToString().Equals(url, StringComparison.OrdinalIgnoreCase), action);
}
}
[Fact]
public async Task UploadHealthAsync_should_pass_clientCurrentTime_with_request()
{
var mockHttp = new MockHttpMessageHandler();
var mockTimeCoordinator = new Mock<ITimeCoordinator>();
var mockCredentialsProvider = new Mock<ICredentialsProvider>();
var currentTimeUtc = DateTime.UtcNow;
mockTimeCoordinator.Setup(c => c.UtcNow).Returns(currentTimeUtc);
var client = new TestableHealthMonitorExchangeClient(mockHttp, mockTimeCoordinator.Object, mockCredentialsProvider.Object);
mockHttp.Setup(
$"http://mock/api/endpoints/health?clientCurrentTime={currentTimeUtc.ToString("u", CultureInfo.InvariantCulture)}",
req => new HttpResponseMessage(HttpStatusCode.OK));
try
{
var update = new EndpointHealthUpdate(Guid.NewGuid(), new EndpointHealth(DateTime.UtcNow, TimeSpan.Zero, EndpointStatus.Faulty));
await client.UploadHealthAsync(new[] { update }, CancellationToken.None);
}
catch (Exception e)
{
throw new InvalidOperationException($"Client should not throw, but it did: {e.Message}", e);
}
}
}
}