-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigureResponses.cs
More file actions
184 lines (146 loc) · 8.41 KB
/
Copy pathConfigureResponses.cs
File metadata and controls
184 lines (146 loc) · 8.41 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using static TestableHttpClient.Responses;
namespace TestableHttpClient.IntegrationTests;
public sealed class ConfigureResponses
{
[Fact]
public async Task UsingTestHandler_WithoutSettingUpResponse_Returns200OKWithoutContent()
{
using TestableHttpMessageHandler testHandler = new();
using HttpClient httpClient = new(testHandler);
HttpResponseMessage result = await httpClient.GetAsync("http://httpbin.org/status/200", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal(string.Empty, await result.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
}
[Fact]
public async Task UsingTestHandlerWithCustomResponse_ReturnsCustomResponse()
{
using TestableHttpMessageHandler testHandler = new();
testHandler.RespondWith(Text("HttpClient testing is easy"));
using HttpClient httpClient = new(testHandler);
HttpResponseMessage result = await httpClient.GetAsync("http://httpbin.org/status/200", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal("HttpClient testing is easy", await result.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
}
[Fact]
public async Task UsingTestHandlerWithMultipleCustomResponse_ReturnsLastCustomResponse()
{
using TestableHttpMessageHandler testHandler = new();
testHandler.RespondWith(Text("HttpClient testing is easy"));
testHandler.RespondWith(Json("Not Found", HttpStatusCode.NotFound));
using HttpClient httpClient = new(testHandler);
HttpResponseMessage result = await httpClient.GetAsync("http://httpbin.org/status/201", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.NotFound, result.StatusCode);
Assert.Equal("\"Not Found\"", await result.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
}
[Fact]
public async Task UsingTestHandlerWithCustomResponse_AlwaysReturnsSameCustomResponse()
{
using TestableHttpMessageHandler testHandler = new();
testHandler.RespondWith(Text("HttpClient testing is easy"));
using HttpClient httpClient = new(testHandler);
string[] urls = [
"http://httpbin.org/status/200",
"http://httpbin.org/status/201",
"http://httpbin.org/status/400",
"http://httpbin.org/status/401",
"http://httpbin.org/status/503",
];
foreach (string? url in urls)
{
HttpResponseMessage result = await httpClient.GetAsync(url, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal("HttpClient testing is easy", await result.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
}
}
[Fact]
public async Task UsingTestHandlerWithCustomResponseFactory_AllowsForAdvancedUseCases()
{
using TestableHttpMessageHandler testHandler = new();
static IResponse PathBasedResponse(HttpResponseContext context)
{
return context.HttpRequestMessage switch
{
_ when context.HttpRequestMessage.RequestUri?.AbsolutePath == "/status/200" => StatusCode(HttpStatusCode.OK),
_ when context.HttpRequestMessage.RequestUri?.AbsolutePath == "/status/400" => StatusCode(HttpStatusCode.BadRequest),
_ => StatusCode(HttpStatusCode.NotFound)
};
}
testHandler.RespondWith(SelectResponse(PathBasedResponse));
using HttpClient httpClient = new(testHandler);
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin/status/200", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
response = await httpClient.GetAsync("http://httpbin.org/status/400", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
response = await httpClient.GetAsync("http://httpbin.org/status/500", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
[Fact]
public async Task UsingTestHandlerWithRoute_AllowsForRoutingUseCases()
{
using TestableHttpMessageHandler testHandler = new();
testHandler.RespondWith(Route(builder =>
{
builder.Map("http://*", StatusCode(HttpStatusCode.Redirect));
builder.Map("/status/200", StatusCode(HttpStatusCode.OK));
builder.Map("/status/400", StatusCode(HttpStatusCode.BadRequest));
}));
using HttpClient httpClient = new(testHandler);
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin/status/200", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
response = await httpClient.GetAsync("https://httpbin/status/200", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
response = await httpClient.GetAsync("https://httpbin.org/status/400", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
response = await httpClient.GetAsync("https://httpbin.org/status/500", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
[Fact]
public async Task SimulateTimeout_WillThrowExceptionSimulatingTheTimeout()
{
using TestableHttpMessageHandler testHandler = new();
testHandler.RespondWith(Timeout());
using HttpClient httpClient = new(testHandler);
await Assert.ThrowsAsync<TaskCanceledException>(() => httpClient.GetAsync("https://httpbin.org/delay/500", TestContext.Current.CancellationToken));
}
[Fact]
public async Task UsingTestHandlerWithSequencedResponses_WillReturnDifferentResponseForEveryRequest()
{
using TestableHttpMessageHandler testHandler = new();
testHandler.RespondWith(Sequenced(
StatusCode(HttpStatusCode.OK),
StatusCode(HttpStatusCode.Unauthorized),
StatusCode(HttpStatusCode.NoContent),
StatusCode(HttpStatusCode.NotFound)
));
using HttpClient httpClient = new(testHandler);
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
// Last configured response is returned when all other responses are used.
response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
[Fact]
public async Task UsingTestHandlerWithDelayedResponses_WillDelayTheResponse()
{
using TestableHttpMessageHandler testHandler = new();
testHandler.RespondWith(Delayed(StatusCode(HttpStatusCode.OK), TimeSpan.FromSeconds(1)));
using HttpClient httpClient = new(testHandler);
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task UsingTestHandlerWithConfiguredResponses_WillConfigureTheResponse()
{
using TestableHttpMessageHandler testHandler = new();
testHandler.RespondWith(Configured(StatusCode(HttpStatusCode.NoContent), x => x.Headers.Add("server", "test")));
using HttpClient httpClient = new(testHandler);
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
Assert.Equal("test", response.Headers.Server.ToString());
}
}