-
Notifications
You must be signed in to change notification settings - Fork 573
/
Copy pathMockHttpClientFixture.cs
54 lines (47 loc) · 1.5 KB
/
MockHttpClientFixture.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
namespace StripeTests
{
using System;
using System.Net.Http;
using System.Threading;
using Moq;
using Moq.Protected;
using Stripe;
public class MockHttpClientFixture : IDisposable
{
private readonly Mock<HttpClientHandler> mockHandler;
private readonly HttpMessageHandler origHandler;
public MockHttpClientFixture()
{
this.mockHandler = new Mock<HttpClientHandler>
{
CallBase = true
};
this.origHandler = StripeConfiguration.HttpMessageHandler;
StripeConfiguration.HttpMessageHandler = this.mockHandler.Object;
}
public void Dispose()
{
StripeConfiguration.HttpMessageHandler = this.origHandler;
}
/// <summary>
/// Resets all invocations recorded for the mock client.
/// </summary>
public void Reset()
{
this.mockHandler.Invocations.Clear();
}
/// <summary>
/// Asserts that a single HTTP request was made with the specified method and path.
/// </summary>
public void AssertRequest(HttpMethod method, string path)
{
this.mockHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == method &&
m.RequestUri.AbsolutePath == path),
ItExpr.IsAny<CancellationToken>());
}
}
}