Add WireMockAspNetCoreLogger to log Kestrel warnings/errors (#1432)

* Add WireMockAspNetCoreLogger

* fix tests

* x

* .
This commit is contained in:
Stef Heyenrath
2026-03-14 10:30:56 +01:00
committed by GitHub
parent d08ce944b6
commit ca788cb9b0
9 changed files with 159 additions and 37 deletions

View File

@@ -431,8 +431,13 @@ public partial class WireMockServerTests(ITestOutputHelper testOutputHelper)
using var server = WireMockServer.Start();
server
.Given(Request.Create().WithPath(path).UsingHead())
.RespondWith(Response.Create().WithHeader(HttpKnownHeaderNames.ContentLength, length));
.WhenRequest(r => r
.WithPath(path)
.UsingHead()
)
.ThenRespondWith(r => r
.WithHeader(HttpKnownHeaderNames.ContentLength, length)
);
// Act
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, path);
@@ -442,6 +447,45 @@ public partial class WireMockServerTests(ITestOutputHelper testOutputHelper)
response.Content.Headers.GetValues(HttpKnownHeaderNames.ContentLength).Should().Contain(length);
}
#if NET8_0_OR_GREATER
[Theory]
[InlineData("DELETE")]
[InlineData("GET")]
[InlineData("OPTIONS")]
[InlineData("PATCH")]
[InlineData("POST")]
[InlineData("PUT")]
[InlineData("TRACE")]
public async Task WireMockServer_Should_LogAndThrowExceptionWhenInvalidContentLength(string method)
{
// Assign
const string length = "42";
var path = $"/InvalidContentLength_{Guid.NewGuid()}";
using var server = WireMockServer.Start(new WireMockServerSettings
{
Logger = new TestOutputHelperWireMockLogger(testOutputHelper)
});
server
.WhenRequest(r => r
.WithPath(path)
.UsingAnyMethod()
)
.ThenRespondWith(r => r
.WithStatusCode(HttpStatusCode.OK)
.WithHeader(HttpKnownHeaderNames.ContentLength, length)
);
// Act
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Parse(method), path);
var response = await server.CreateClient().SendAsync(httpRequestMessage, _ct);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
testOutputHelper.Output.Should().Contain($"Response Content-Length mismatch: too few bytes written (0 of {length}).");
}
#endif
[Theory]
[InlineData("TRACE")]
[InlineData("GET")]