diff --git a/CHANGELOG.md b/CHANGELOG.md index 4692568..f86c03a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ Represents the **NuGet** versions. +## v5.9.2 +- *Fixed:* The `MockHttpClientRequest` now resets the internal count state when overridding a response to ensure `Verify()` functions correctly. + ## v5.9.1 - *Fixed:* The `MockHttpClientRequest` now caches the response content internally, and creates a new `HttpContent` instance for each request to ensure that the content can be read multiple times across multiple requests (where applicable); avoids potential object disposed error. - *Fixed:* The `MockHttpClient.Reset()` was incorrectly resetting the `MockHttpClient` instance to its default state, but was not resetting the internal request configuration which is used to determine the response. This has now been corrected to reset the internal mocked state only. diff --git a/Common.targets b/Common.targets index 183041f..36c86d4 100644 --- a/Common.targets +++ b/Common.targets @@ -1,6 +1,6 @@  - 5.9.1 + 5.9.2 preview Avanade Avanade diff --git a/src/UnitTestEx/Mocking/MockHttpClientRequest.cs b/src/UnitTestEx/Mocking/MockHttpClientRequest.cs index caefd27..4c03899 100644 --- a/src/UnitTestEx/Mocking/MockHttpClientRequest.cs +++ b/src/UnitTestEx/Mocking/MockHttpClientRequest.cs @@ -110,6 +110,11 @@ internal void MockResponse() // Mark as mock complete. IsMockComplete = true; + + // Reset counts and indices. + Rule.Response?.Count = 0; + Rule.ResponsesIndex = 0; + Rule.Responses?.ForEach(x => x.Count = 0); } /// @@ -154,21 +159,6 @@ private static async Task CreateResponseAsync(HttpRequestMe return httpResponse; } - /// - /// Converts the body to a string. - /// - private string BodyToString() - { - if (_mediaType == null || _content == null) - return "no"; - - return _mediaType.ToLowerInvariant() switch - { - MediaTypeNames.Application.Json => $"'{JsonSerializer.Serialize(_content, JsonWriteFormat.None)}' [{_mediaType}]", - _ => $"'{_content}' [{_mediaType}]", - }; - } - /// /// Check the request and content for a match. /// diff --git a/src/UnitTestEx/Mocking/MockHttpClientResponse.cs b/src/UnitTestEx/Mocking/MockHttpClientResponse.cs index c0bb1dc..f337edc 100644 --- a/src/UnitTestEx/Mocking/MockHttpClientResponse.cs +++ b/src/UnitTestEx/Mocking/MockHttpClientResponse.cs @@ -167,7 +167,10 @@ public void With(HttpContent? content = null, HttpStatusCode? statusCode = null, ResponseAction = response; if (_rule != null) + { + _rule.Responses = null; _clientRequest.MockResponse(); + } } /// @@ -250,6 +253,7 @@ public void WithSequence(Action sequence) ArgumentNullException.ThrowIfNull(sequence); + _rule.Response = null; _rule.Responses ??= []; var s = new MockHttpClientResponseSequence(_clientRequest, _rule); diff --git a/tests/UnitTestEx.NUnit.Test/MockHttpClientTest.cs b/tests/UnitTestEx.NUnit.Test/MockHttpClientTest.cs index 74a1b32..9526ca3 100644 --- a/tests/UnitTestEx.NUnit.Test/MockHttpClientTest.cs +++ b/tests/UnitTestEx.NUnit.Test/MockHttpClientTest.cs @@ -415,22 +415,39 @@ 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").Respond; + var mcr = mc.Request(HttpMethod.Get, "products/xyz"); var hc = mcf.GetHttpClient("XXX"); // Set the response. - mcr.With("some-some", HttpStatusCode.OK); + 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.With("some-other", HttpStatusCode.Accepted); + 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]