Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4.3.2 #73

Merged
merged 1 commit into from
May 17, 2024
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.

## v4.3.2
- *Fixed*: Added `TraceRequestComparisons` support to `MockHttpClient` to enable tracing for all requests.

## v4.3.1
- *Fixed*: Added `StringSyntaxAttribute` support to improve intellisense for JSON and URI specification.

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>4.3.1</Version>
<Version>4.3.2</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
21 changes: 21 additions & 0 deletions src/UnitTestEx/Mocking/MockHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public sealed class MockHttpClient : IDisposable
private bool _useHttpMessageHandlers;
private Type[] _excludeTypes = [];
private bool _useHttpClientConfigurations;
private bool _traceRequestComparisons;

/// <summary>
/// Initializes a new instance of the <see cref="MockHttpClient"/> class.
Expand Down Expand Up @@ -249,9 +250,29 @@ public MockHttpClientRequest Request(HttpMethod? method = null, string? requestU

var r = new MockHttpClientRequest(this, method ?? HttpMethod.Get, requestUri);
_requests.Add(r);
if (_traceRequestComparisons)
r.TraceRequestComparisons();

return r;
}

/// <summary>
/// Indicates whether the request content comparison differences should be trace logged to aid in debugging/troubleshooting.
/// </summary>
/// <returns>The <see cref="MockHttpClient"/> to support fluent-style method-chaining.</returns>
/// <remarks>By default the request content comparison differences are not traced. Where tracing is requested all existing and new <see cref="Request(HttpMethod?, string?)"/> configurations
/// will be traced.</remarks>
public MockHttpClient TraceRequestComparisons()
{
_traceRequestComparisons = true;
foreach (var r in _requests)
{
r.TraceRequestComparisons();
}

return this;
}

/// <summary>
/// Verifies that all verifiable <see cref="Mock"/> expectations have been met; being all requests have been invoked.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/UnitTestEx/Mocking/MockHttpClientRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private bool RequestPredicate(HttpRequestMessage request)
options.JsonSerializer ??= JsonSerializer;
var jcr = new JsonElementComparer(options).Compare(content, body, _pathsToIgnore);
if (jcr.HasDifferences && _traceRequestComparisons)
Implementor.WriteLine($"HTTP request JsonElementComparer differences:{Environment.NewLine}{jcr}");
Implementor.WriteLine($"UnitTestEx > Mismatched HTTP request {request.Method} {request.RequestUri} mocked vs actual trace comparison differences:{Environment.NewLine}{jcr}");

return jcr.AreEqual;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/UnitTestEx.MSTest.Test/MockHttpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ public async Task UriAndBody_Json_Multi()
Assert.AreEqual(HttpStatusCode.OK, res.StatusCode);
}

[TestMethod]
public async Task UriAndBody_Json_Multi2()
{
var mcf = MockHttpClientFactory.Create();
var mc = mcf.CreateClient("XXX", new Uri("https://d365test")).TraceRequestComparisons();
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.AreEqual(HttpStatusCode.Accepted, res.StatusCode);

res = await hc.PostAsync("products/xyz", new StringContent("{\"firstName\":\"Jenny\",\"lastName\":\"Browne\"}", Encoding.UTF8, MediaTypeNames.Application.Json)).ConfigureAwait(false);
Assert.AreEqual(HttpStatusCode.OK, res.StatusCode);
}

[TestMethod]
public async Task UriAndBody_WithJsonResponse()
{
Expand Down
Loading