|
1 | 1 | using Moq; |
| 2 | +using NUnit.Framework; |
2 | 3 | using System; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
3 | 6 | using System.Net; |
4 | 7 | using System.Net.Http; |
5 | 8 | using System.Net.Mime; |
6 | 9 | using System.Text; |
7 | 10 | using System.Threading.Tasks; |
8 | | -using UnitTestEx.NUnit.Test.Model; |
9 | 11 | using UnitTestEx.NUnit; |
10 | | -using NUnit.Framework; |
11 | | -using System.Diagnostics; |
12 | | -using System.Linq; |
| 12 | +using UnitTestEx.NUnit.Test.Model; |
| 13 | +using static System.Net.Mime.MediaTypeNames; |
13 | 14 |
|
14 | 15 | namespace UnitTestEx.NUnit.Test |
15 | 16 | { |
@@ -371,6 +372,67 @@ public async Task MockSequenceDelay() |
371 | 372 | }); |
372 | 373 | } |
373 | 374 |
|
| 375 | + [Test] |
| 376 | + public async Task MockReuseSameAndReset() |
| 377 | + { |
| 378 | + var mcf = MockHttpClientFactory.Create(); |
| 379 | + var mc = mcf.CreateClient("XXX", new Uri("https://d365test")); |
| 380 | + mc.Request(HttpMethod.Get, "products/xyz").Respond.With("some-some", HttpStatusCode.OK); |
| 381 | + |
| 382 | + var hc = mcf.GetHttpClient("XXX"); |
| 383 | + var res = await hc.GetAsync("products/xyz").ConfigureAwait(false); |
| 384 | + var txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 385 | + Assert.That(txt, Is.EqualTo("some-some")); |
| 386 | + |
| 387 | + res = await hc.GetAsync("products/xyz").ConfigureAwait(false); |
| 388 | + txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 389 | + Assert.That(txt, Is.EqualTo("some-some")); |
| 390 | + |
| 391 | + // Now let's reset. |
| 392 | + mc.Reset(); |
| 393 | + |
| 394 | + // Should throw as no matched requests. |
| 395 | + Console.WriteLine("No-match"); |
| 396 | + Assert.ThrowsAsync<MockHttpClientException>(async () => await hc.GetAsync("products/xyz").ConfigureAwait(false)); |
| 397 | + |
| 398 | + // Add the request back in. |
| 399 | + mc.Request(HttpMethod.Get, "products/xyz").Respond.With("some-some", HttpStatusCode.OK); |
| 400 | + mc.Request(HttpMethod.Get, "products/abc").Respond.With("a-blue-carrot", HttpStatusCode.OK); |
| 401 | + |
| 402 | + // Try again and should work. |
| 403 | + Console.WriteLine("Yes-Match"); |
| 404 | + res = await hc.GetAsync("products/xyz").ConfigureAwait(false); |
| 405 | + txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 406 | + Assert.That(txt, Is.EqualTo("some-some")); |
| 407 | + |
| 408 | + res = await hc.GetAsync("products/abc").ConfigureAwait(false); |
| 409 | + txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 410 | + Assert.That(txt, Is.EqualTo("a-blue-carrot")); |
| 411 | + } |
| 412 | + |
| 413 | + [Test] |
| 414 | + public async Task MockOnTheFlyChange() |
| 415 | + { |
| 416 | + var mcf = MockHttpClientFactory.Create(); |
| 417 | + var mc = mcf.CreateClient("XXX", new Uri("https://d365test")); |
| 418 | + var mcr = mc.Request(HttpMethod.Get, "products/xyz").Respond; |
| 419 | + |
| 420 | + var hc = mcf.GetHttpClient("XXX"); |
| 421 | + |
| 422 | + // Set the response. |
| 423 | + mcr.With("some-some", HttpStatusCode.OK); |
| 424 | + |
| 425 | + // Get the response and verify. |
| 426 | + var res = await hc.GetAsync("products/xyz").ConfigureAwait(false); |
| 427 | + var txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 428 | + Assert.That(txt, Is.EqualTo("some-some")); |
| 429 | + |
| 430 | + mcr.With("some-other", HttpStatusCode.Accepted); |
| 431 | + res = await hc.GetAsync("products/xyz").ConfigureAwait(false); |
| 432 | + txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 433 | + Assert.That(txt, Is.EqualTo("some-other")); |
| 434 | + } |
| 435 | + |
374 | 436 | [Test] |
375 | 437 | public async Task UriAndBody_WithXmlRequest() |
376 | 438 | { |
|
0 commit comments