Skip to content

Commit 112eaa6

Browse files
authored
v5.9.2 (#101)
- *Fixed:* The `MockHttpClientRequest` now resets the internal count state when overridding a response to ensure `Verify()` functions correctly.
1 parent 715e57c commit 112eaa6

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Represents the **NuGet** versions.
44

5+
## v5.9.2
6+
- *Fixed:* The `MockHttpClientRequest` now resets the internal count state when overridding a response to ensure `Verify()` functions correctly.
7+
58
## v5.9.1
69
- *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.
710
- *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.

Common.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>5.9.1</Version>
3+
<Version>5.9.2</Version>
44
<LangVersion>preview</LangVersion>
55
<Authors>Avanade</Authors>
66
<Company>Avanade</Company>

src/UnitTestEx/Mocking/MockHttpClientRequest.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ internal void MockResponse()
110110

111111
// Mark as mock complete.
112112
IsMockComplete = true;
113+
114+
// Reset counts and indices.
115+
Rule.Response?.Count = 0;
116+
Rule.ResponsesIndex = 0;
117+
Rule.Responses?.ForEach(x => x.Count = 0);
113118
}
114119

115120
/// <summary>
@@ -154,21 +159,6 @@ private static async Task<HttpResponseMessage> CreateResponseAsync(HttpRequestMe
154159
return httpResponse;
155160
}
156161

157-
/// <summary>
158-
/// Converts the body to a string.
159-
/// </summary>
160-
private string BodyToString()
161-
{
162-
if (_mediaType == null || _content == null)
163-
return "no";
164-
165-
return _mediaType.ToLowerInvariant() switch
166-
{
167-
MediaTypeNames.Application.Json => $"'{JsonSerializer.Serialize(_content, JsonWriteFormat.None)}' [{_mediaType}]",
168-
_ => $"'{_content}' [{_mediaType}]",
169-
};
170-
}
171-
172162
/// <summary>
173163
/// Check the request and content for a match.
174164
/// </summary>

src/UnitTestEx/Mocking/MockHttpClientResponse.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ public void With(HttpContent? content = null, HttpStatusCode? statusCode = null,
167167
ResponseAction = response;
168168

169169
if (_rule != null)
170+
{
171+
_rule.Responses = null;
170172
_clientRequest.MockResponse();
173+
}
171174
}
172175

173176
/// <summary>
@@ -250,6 +253,7 @@ public void WithSequence(Action<MockHttpClientResponseSequence> sequence)
250253

251254
ArgumentNullException.ThrowIfNull(sequence);
252255

256+
_rule.Response = null;
253257
_rule.Responses ??= [];
254258

255259
var s = new MockHttpClientResponseSequence(_clientRequest, _rule);

tests/UnitTestEx.NUnit.Test/MockHttpClientTest.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,22 +415,39 @@ public async Task MockOnTheFlyChange()
415415
{
416416
var mcf = MockHttpClientFactory.Create();
417417
var mc = mcf.CreateClient("XXX", new Uri("https://d365test"));
418-
var mcr = mc.Request(HttpMethod.Get, "products/xyz").Respond;
418+
var mcr = mc.Request(HttpMethod.Get, "products/xyz");
419419

420420
var hc = mcf.GetHttpClient("XXX");
421421

422422
// Set the response.
423-
mcr.With("some-some", HttpStatusCode.OK);
423+
mcr.Times(Times.Once()).Respond.With("some-some", HttpStatusCode.OK);
424424

425425
// Get the response and verify.
426426
var res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
427427
var txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
428428
Assert.That(txt, Is.EqualTo("some-some"));
429+
mcr.Verify();
429430

430-
mcr.With("some-other", HttpStatusCode.Accepted);
431+
mcr.Times(Times.Once()).Respond.With("some-other", HttpStatusCode.Accepted);
431432
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
432433
txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
433434
Assert.That(txt, Is.EqualTo("some-other"));
435+
mcr.Verify();
436+
437+
mcr.Respond.WithSequence(s =>
438+
{
439+
s.Respond().With("some-one", HttpStatusCode.OK);
440+
s.Respond().With("some-two", HttpStatusCode.OK);
441+
});
442+
443+
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
444+
txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
445+
Assert.That(txt, Is.EqualTo("some-one"));
446+
447+
res = await hc.GetAsync("products/xyz").ConfigureAwait(false);
448+
txt = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
449+
Assert.That(txt, Is.EqualTo("some-two"));
450+
mcr.Verify();
434451
}
435452

436453
[Test]

0 commit comments

Comments
 (0)