generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMockHttpClientTest.cs
More file actions
559 lines (471 loc) · 26.7 KB
/
MockHttpClientTest.cs
File metadata and controls
559 lines (471 loc) · 26.7 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
using Moq;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using UnitTestEx.NUnit;
using UnitTestEx.NUnit.Test.Model;
using static System.Net.Mime.MediaTypeNames;
namespace UnitTestEx.NUnit.Test
{
[TestFixture]
public class MockHttpClientTest
{
[Test]
public async Task UriOnly_Single()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test")).Request(HttpMethod.Get, "products/xyz").Respond.With(HttpStatusCode.NotFound);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}
[Test]
public async Task UriOnly_Encoded()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test")).Request(HttpMethod.Get, "person/xyz?search=email eq \"bob@email.com\"").Respond.With(HttpStatusCode.NotFound);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.GetAsync("person/xyz?search=email eq \"bob@email.com\"").ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
res = await hc.GetAsync("person/xyz?search=email%20eq%20%22bob@email.com%22").ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}
[Test]
public async Task UriOnly_Multi()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
mc.Request(HttpMethod.Get, "products/xyz").Respond.With(HttpStatusCode.NotFound);
mc.Request(HttpMethod.Get, "products/abc").Respond.With(HttpStatusCode.NoContent);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
res = await hc.GetAsync("products/abc").ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
}
[Test]
public async Task UriAndBody_String_Single()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test")).Request(HttpMethod.Post, "products/xyz").WithBody("Bananas").Respond.With(HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsync("products/xyz", new StringContent("Bananas")).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
}
[Test]
public async Task UriAndBody_String_Multi()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
mc.Request(HttpMethod.Post, "products/xyz").WithBody("Bananas").Respond.With(HttpStatusCode.Accepted);
mc.Request(HttpMethod.Post, "products/xyz").WithBody("Apples").Respond.With(HttpStatusCode.NoContent);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsync("products/xyz", new StringContent("Bananas")).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
res = await hc.PostAsync("products/xyz", new StringContent("Apples")).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
}
[Test]
public void UriAndBody_Invalid()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test")).Request(HttpMethod.Post, "products/xyz").WithBody("Bananas").Respond.With(HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient("XXX");
Assert.ThrowsAsync<MockHttpClientException>(() => hc.PostAsync("products/xyz", new StringContent("Apples")));
}
[Test]
public async Task UriAndBody_Json_Single()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test")).Request(HttpMethod.Post, "products/xyz").WithJsonBody(new Person { FirstName = "Bob", LastName = "Jane" }).Respond.With(HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
}
[Test]
public async Task UriAndBody_Json_Multi()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
mc.Request(HttpMethod.Post, "products/xyz").WithJsonBody("{ \"firstName\": \"Bob\", \"lastName\": \"Jane\" }").Respond.With(HttpStatusCode.Accepted);
mc.Request(HttpMethod.Post, "products/xyz").WithJsonBody(new Person { FirstName = "Jenny", LastName = "Browne" }).Respond.With(HttpStatusCode.OK);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
res = await hc.PostAsync("products/xyz", new StringContent("{ \"firstName\": \"Jenny\", \"lastName\": \"Browne\" }", Encoding.UTF8, MediaTypeNames.Application.Json)).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.OK));
}
[Test]
public async Task UriAndBody_WithJsonResponse()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Post, "products/xyz").WithJsonBody(new Person { FirstName = "Bob", LastName = "Jane" })
.Respond.WithJson(new Person2 { First = "Bob", Last = "Jane" }, HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
Assert.That(await res.Content.ReadAsStringAsync().ConfigureAwait(false), Is.EqualTo("{\"first\":\"Bob\",\"last\":\"Jane\"}"));
});
}
[Test]
public async Task UriAndBody_WithJsonResponse2()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Post, "products/xyz").WithJsonBody(new Person { FirstName = "Bob", LastName = "Jane" })
.Respond.WithJson("{\"first\":\"Bob\",\"last\":\"Jane\"}", HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
Assert.That(await res.Content.ReadAsStringAsync().ConfigureAwait(false), Is.EqualTo("{\"first\":\"Bob\",\"last\":\"Jane\"}"));
});
}
[Test]
public async Task UriAndBody_WithJsonResponse3()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Post, "products/xyz").WithJsonBody(new Person { FirstName = "Bob", LastName = "Jane" })
.Respond.WithJsonResource("MockHttpClientTest-UriAndBody_WithJsonResponse3.json", HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
Assert.That(await res.Content.ReadAsStringAsync().ConfigureAwait(false), Is.EqualTo("{\"first\":\"Bob\",\"last\":\"Jane\"}"));
});
}
[Test]
public void VerifyMock_NotExecuted()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Post, "products/xyz").WithJsonBody(new Person { FirstName = "Bob", LastName = "Jane" })
.Respond.WithJsonResource("MockHttpClientTest-UriAndBody_WithJsonResponse3.json", HttpStatusCode.Accepted);
try
{
mcf.VerifyAll();
Assert.Fail();
}
catch (MockHttpClientException mhcex)
{
Assert.That(mhcex.Message, Is.EqualTo("The request was invoked 0 times; expected AtLeastOnce. Request: <XXX> POST https://d365test/products/xyz {\"firstName\":\"Bob\",\"lastName\":\"Jane\"} (application/json)"));
}
}
[Test]
public async Task VerifyMock_NotExecuted_Multi()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
mc.Request(HttpMethod.Post, "products/xyz").WithJsonBody(new Person { FirstName = "Bob", LastName = "Jane" })
.Respond.WithJsonResource("MockHttpClientTest-UriAndBody_WithJsonResponse3.json", HttpStatusCode.Accepted);
mc.Request(HttpMethod.Post, "products/abc").WithJsonBody(new Person { FirstName = "David", LastName = "Jane" })
.Respond.WithJsonResource("MockHttpClientTest-UriAndBody_WithJsonResponse3.json", HttpStatusCode.Accepted);
try
{
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
mcf.VerifyAll();
Assert.Fail();
}
catch (MockHttpClientException mhcex)
{
Assert.That(mhcex.Message, Is.EqualTo("The request was invoked 0 times; expected AtLeastOnce. Request: <XXX> POST https://d365test/products/abc {\"firstName\":\"David\",\"lastName\":\"Jane\"} (application/json)"));
}
}
[Test]
public async Task VerifyMock_NotExecuted_Times()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
mc.Request(HttpMethod.Post, "products/xyz").Times(Times.Exactly(2)).WithJsonBody(new Person { FirstName = "Bob", LastName = "Jane" })
.Respond.WithJsonResource("MockHttpClientTest-UriAndBody_WithJsonResponse3.json", HttpStatusCode.Accepted);
try
{
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
mcf.VerifyAll();
Assert.Fail();
}
catch (MockHttpClientException mhcex)
{
Assert.That(mhcex.Message, Is.EqualTo("The request was invoked 3 times; expected Exactly(2). Request: <XXX> POST https://d365test/products/xyz {\"firstName\":\"Bob\",\"lastName\":\"Jane\"} (application/json)"));
}
}
[Test]
public async Task VerifyMock_Executed()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Post, "products/xyz").WithJsonBody(new Person { FirstName = "Bob", LastName = "Jane" })
.Respond.WithJsonResource("MockHttpClientTest-UriAndBody_WithJsonResponse3.json", HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient("XXX");
await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
mcf.VerifyAll();
}
[Test]
public async Task UriAndAnyBody()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Post, "products/xyz").WithAnyBody()
.Respond.WithJson("{\"first\":\"Bob\",\"last\":\"Jane\"}", HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
Assert.That(await res.Content.ReadAsStringAsync().ConfigureAwait(false), Is.EqualTo("{\"first\":\"Bob\",\"last\":\"Jane\"}"));
Assert.That(res.RequestMessage, Is.Not.Null);
});
Assert.ThrowsAsync<MockHttpClientException>(async () => await hc.SendAsync(new HttpRequestMessage(HttpMethod.Post, "products/xyz")));
}
[Test]
public async Task MockSequence_Body()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
mc.Request(HttpMethod.Post, "products/xyz").WithAnyBody().Respond.WithSequence(s =>
{
s.Respond().With(HttpStatusCode.Accepted);
s.Respond().With(HttpStatusCode.OK);
});
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.OK));
}
[Test]
public async Task MockSequence()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
mc.Request(HttpMethod.Get, "products/xyz").Respond.WithSequence(s =>
{
s.Respond().With(HttpStatusCode.NotModified);
s.Respond().With(HttpStatusCode.NotFound);
});
var hc = mcf.GetHttpClient("XXX");
var res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NotModified));
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}
[Test]
public async Task MockDelay()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test")).Request(HttpMethod.Get, "products/xyz").Respond.Delay(500).With(HttpStatusCode.NotFound);
var hc = mcf.GetHttpClient("XXX");
var sw = Stopwatch.StartNew();
var res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
sw.Stop();
Assert.Multiple(() =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
Assert.That(sw.ElapsedMilliseconds, Is.GreaterThanOrEqualTo(495));
});
}
[Test]
public async Task MockSequenceDelay()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
mc.Request(HttpMethod.Get, "products/xyz").Respond.WithSequence(s =>
{
s.Respond().Delay(250).With(HttpStatusCode.NotModified);
s.Respond().Delay(100, 150).With(HttpStatusCode.NotFound);
});
var hc = mcf.GetHttpClient("XXX");
var sw = Stopwatch.StartNew();
var res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
sw.Stop();
Assert.Multiple(() =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NotModified));
Assert.That(sw.ElapsedMilliseconds, Is.GreaterThanOrEqualTo(245));
});
sw.Restart();
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
sw.Stop();
Assert.Multiple(() =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
Assert.That(sw.ElapsedMilliseconds, Is.GreaterThanOrEqualTo(95));
});
}
[Test]
public async Task MockReuseSameAndReset()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
mc.Request(HttpMethod.Get, "products/xyz").Respond.With("some-some", HttpStatusCode.OK);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
var txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.That(txt, Is.EqualTo("some-some"));
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.That(txt, Is.EqualTo("some-some"));
// Now let's reset.
mc.Reset();
// Should throw as no matched requests.
Console.WriteLine("No-match");
Assert.ThrowsAsync<MockHttpClientException>(async () => await hc.GetAsync("products/xyz").ConfigureAwait(false));
// Add the request back in.
mc.Request(HttpMethod.Get, "products/xyz").Respond.With("some-some", HttpStatusCode.OK);
mc.Request(HttpMethod.Get, "products/abc").Respond.With("a-blue-carrot", HttpStatusCode.OK);
// Try again and should work.
Console.WriteLine("Yes-Match");
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.That(txt, Is.EqualTo("some-some"));
res = await hc.GetAsync("products/abc").ConfigureAwait(false);
txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.That(txt, Is.EqualTo("a-blue-carrot"));
}
[Test]
public async Task MockOnTheFlyChange()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
var mcr = mc.Request(HttpMethod.Get, "products/xyz");
var hc = mcf.GetHttpClient("XXX");
// Set the response.
mcr.Times(Times.Once()).Respond.With("some-some", HttpStatusCode.OK);
// Get the response and verify.
var res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
var txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.That(txt, Is.EqualTo("some-some"));
mcr.Verify();
mcr.Times(Times.Once()).Respond.With("some-other", HttpStatusCode.Accepted);
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.That(txt, Is.EqualTo("some-other"));
mcr.Verify();
mcr.Respond.WithSequence(s =>
{
s.Respond().With("some-one", HttpStatusCode.OK);
s.Respond().With("some-two", HttpStatusCode.OK);
});
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.That(txt, Is.EqualTo("some-one"));
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.That(txt, Is.EqualTo("some-two"));
mcr.Verify();
}
[Test]
public async Task UriAndBody_WithXmlRequest()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Post, "products/xyz").WithBody("<Person xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/UnitTestEx.NUnit.Test.Model\"><FirstName>Bob</FirstName><LastName>Jane</LastName></Person>", MediaTypeNames.Application.Xml)
.Respond.With(new StringContent("<person><first>Bob</first><last>Jane</last></person>", Encoding.UTF8, MediaTypeNames.Application.Xml), HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsXmlAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
Assert.That(await res.Content.ReadAsStringAsync().ConfigureAwait(false), Is.EqualTo("<person><first>Bob</first><last>Jane</last></person>"));
});
}
[Test]
public async Task UriAndBody_WithAnyTypeRequest()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://d365test"))
.Request(HttpMethod.Post, "testing").WithBody("--my--custom--format--", "application/custom-format")
.Respond.With(new StringContent("--ok--", Encoding.UTF8, "application/custom-format"), HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsync("testing", new StringContent("--my--custom--format--", Encoding.UTF8, "application/custom-format")).ConfigureAwait(false);
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
Assert.That(await res.Content.ReadAsStringAsync().ConfigureAwait(false), Is.EqualTo("--ok--"));
});
}
[Test]
public async Task DefaultHttpClient()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateDefaultClient(new Uri("https://d365test"))
.Request(HttpMethod.Post, "products/xyz").WithAnyBody()
.Respond.Headers([new("x-blah", "abc"), new("Age", "55")]).WithJson("{\"first\":\"Bob\",\"last\":\"Jane\"}", HttpStatusCode.Accepted);
var hc = mcf.GetHttpClient();
var res = await hc.PostAsJsonAsync("products/xyz", new Person { LastName = "Jane", FirstName = "Bob" }).ConfigureAwait(false);
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
Assert.That(await res.Content.ReadAsStringAsync().ConfigureAwait(false), Is.EqualTo("{\"first\":\"Bob\",\"last\":\"Jane\"}"));
Assert.That(res.RequestMessage, Is.Not.Null);
Assert.That(res.Headers.GetValues("x-blah").Single(), Is.EqualTo("abc"));
Assert.That(res.Headers.Age, Is.EqualTo(TimeSpan.FromSeconds(55)));
});
Assert.ThrowsAsync<MockHttpClientException>(async () => await hc.SendAsync(new HttpRequestMessage(HttpMethod.Post, "products/xyz")));
}
[Test]
public async Task WithYamlRequests()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX").WithRequestsFromResource("mock.unittestex.yaml");
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new { DoesNotMatter = true });
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.Accepted));
ObjectComparer.AssertJson("{\"product\":\"xyz\",\"quantity\":1}", await res.Content.ReadAsStringAsync().ConfigureAwait(false));
});
res = await hc.GetAsync("people/123");
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.OK));
ObjectComparer.AssertJson("{\"first\":\"Bob\",\"last\":\"Jane\"}", await res.Content.ReadAsStringAsync().ConfigureAwait(false));
Assert.That(res.Headers.GetValues("x-blah").Single(), Is.EqualTo("abc"));
Assert.That(res.Headers.Age, Is.EqualTo(TimeSpan.FromSeconds(55)));
});
mcf.VerifyAll();
}
[Test]
public async Task WithYamlSequence()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX").WithRequestsFromResource("sequence.unittestex.yaml");
var hc = mcf.GetHttpClient("XXX");
var res = await hc.GetAsync("people/123");
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.OK));
ObjectComparer.AssertJson("{\"first\":\"Bob\",\"last\":\"Jane\"}", await res.Content.ReadAsStringAsync().ConfigureAwait(false));
});
res = await hc.GetAsync("people/123");
Assert.Multiple(async () =>
{
Assert.That(res.StatusCode, Is.EqualTo(HttpStatusCode.OK));
ObjectComparer.AssertJson("{\"first\":\"Sarah\",\"last\":\"Johns\"}", await res.Content.ReadAsStringAsync().ConfigureAwait(false));
});
mcf.VerifyAll();
}
}
}