Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Common.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>5.9.1</Version>
<Version>5.9.2</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
20 changes: 5 additions & 15 deletions src/UnitTestEx/Mocking/MockHttpClientRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -154,21 +159,6 @@ private static async Task<HttpResponseMessage> CreateResponseAsync(HttpRequestMe
return httpResponse;
}

/// <summary>
/// Converts the body to a string.
/// </summary>
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}]",
};
}

/// <summary>
/// Check the request and content for a match.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/UnitTestEx/Mocking/MockHttpClientResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ public void With(HttpContent? content = null, HttpStatusCode? statusCode = null,
ResponseAction = response;

if (_rule != null)
{
_rule.Responses = null;
_clientRequest.MockResponse();
}
}

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

ArgumentNullException.ThrowIfNull(sequence);

_rule.Response = null;
_rule.Responses ??= [];

var s = new MockHttpClientResponseSequence(_clientRequest, _rule);
Expand Down
23 changes: 20 additions & 3 deletions tests/UnitTestEx.NUnit.Test/MockHttpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading