-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransmitterTests.cs
113 lines (93 loc) · 3.94 KB
/
TransmitterTests.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
110
111
112
113
using FluentAssertions;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace TitanShark.Thresher.Core.Tests
{
public class TransmitterTests
{
private readonly ITestOutputHelper _output;
public TransmitterTests(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public async Task Custom_SendFunction_Is_Possible()
{
// prepares
const string responseContentText = "Hello World!";
var transmitter = new Transmitter
(
(callId, request, cancellationToken) =>
{
_output.WriteLine($"Request to '{request.RequestUri}' was sent out.");
return Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.Created)
{
Content = new StringContent(responseContentText)
});
}
);
var handler = new InterceptableHttpClientHandler(transmitter: transmitter);
var sut = new HttpClient(handler);
// acts
var response = await sut.GetAsync("https://testing.only").ConfigureAwait(false);
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
// asserts
response.StatusCode.Should().Be(System.Net.HttpStatusCode.Created);
content.Should().Be(responseContentText);
// cleans up
sut.Dispose();
}
[Fact]
public async Task Mock_Is_Possible()
{
// prepares
var transmitter = new Transmitter
(
(callId, request, cancellationToken) =>
{
_output.WriteLine($"Request to '{request.RequestUri}' was sent out.");
var response = Mock.BuildResponse(request);
return Task.FromResult(response);
}
);
var handler = new InterceptableHttpClientHandler(transmitter: transmitter);
var sut = new HttpClient(handler);
// acts
var correctResponse = await sut.GetAsync("https://testing.only/ResourceA/1").ConfigureAwait(false);
var correctContent = await correctResponse.Content.ReadFromJsonAsync<Mock.ResInfo>().ConfigureAwait(false);
var incorrectResponse = await sut.GetAsync("https://testing.only/TypeA/1").ConfigureAwait(false);
var incorrectContent = await incorrectResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
// asserts
correctResponse.StatusCode.Should().Be(System.Net.HttpStatusCode.Created);
correctContent.Resource.Should().Be("ResourceA");
correctContent.Id.Should().Be(1);
incorrectResponse.StatusCode.Should().Be(System.Net.HttpStatusCode.NotFound);
incorrectContent.Should().Be(Mock.NotFound);
// cleans up
sut.Dispose();
}
private class Mock
{
public const string NotFound = "Not found!";
public static HttpResponseMessage BuildResponse(HttpRequestMessage request) => request.RequestUri.PathAndQuery switch
{
"/ResourceA/1" => new HttpResponseMessage(System.Net.HttpStatusCode.Created)
{
Content = JsonContent.Create(new ResInfo { Resource = "ResourceA", Id = 1 })
},
_ => new HttpResponseMessage(System.Net.HttpStatusCode.NotFound)
{
Content = new StringContent(NotFound)
}
};
public class ResInfo
{
public string Resource { get; set; }
public int Id { get; set; }
}
}
}
}