diff --git a/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs b/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs index 4d91fc34..e11f630c 100644 --- a/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs +++ b/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs @@ -305,12 +305,6 @@ public class WireMockServerSettings [PublicAPI, JsonIgnore] public IDictionary>? CustomMatcherMappings { get; set; } - /// - /// The used when the JSON response is generated. - /// - [PublicAPI, JsonIgnore] - public JsonSerializerSettings? JsonSerializerSettings { get; set; } - /// /// The Culture to use. /// Currently used for: diff --git a/src/WireMock.Net.Shared/Util/BodyParser.cs b/src/WireMock.Net.Shared/Util/BodyParser.cs index 18a4ae16..518e2e22 100644 --- a/src/WireMock.Net.Shared/Util/BodyParser.cs +++ b/src/WireMock.Net.Shared/Util/BodyParser.cs @@ -5,6 +5,7 @@ using System.Text; using Stef.Validation; using WireMock.Constants; using WireMock.Matchers; +using WireMock.Serialization; using WireMock.Types; namespace WireMock.Util; @@ -25,7 +26,7 @@ internal static class BodyParser CONNECT - No defined body semantics PATCH - Body supported. */ - private static readonly IDictionary BodyAllowedForMethods = new Dictionary + private static readonly Dictionary BodyAllowedForMethods = new() { { HttpRequestMethod.HEAD, false }, { HttpRequestMethod.GET, false }, @@ -173,7 +174,7 @@ internal static class BodyParser { try { - data.BodyAsJson = settings.DefaultJsonConverter.Deserialize(data.BodyAsString); + data.BodyAsJson = settings.DefaultJsonConverter.Deserialize(data.BodyAsString, JsonSerializationConstants.JsonConverterOptionsWithDateParsingNone); data.DetectedBodyType = BodyType.Json; } catch diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialWildcardMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialWildcardMatcherTests.cs index 78f71a19..488ca602 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPartialWildcardMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialWildcardMatcherTests.cs @@ -416,4 +416,20 @@ public class JsonPartialWildcardMatcherTests // Assert Assert.Equal(1.0, match); } -} + + [Fact] + public void JsonPartialWildcardMatcher_IsMatch_WithRegexTrue_DateFormat_ShouldMatch() + { + // Assign + var matcher = new JsonPartialWildcardMatcher( + new { date = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?(?:Z|[+-]\\d{2}:\\d{2})$", id = 1 }, + ignoreCase: false, + regex: true); + + // Act + var match = matcher.IsMatch("{\"date\":\"2026-06-09T22:23:18.53421+00:00\",\"id\":1}").Score; + + // Assert + Assert.Equal(1.0, match); + } +} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/SystemTextJsonPartialWildcardMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/SystemTextJsonPartialWildcardMatcherTests.cs index 057a9ecd..dc0d9087 100644 --- a/test/WireMock.Net.Tests/Matchers/SystemTextJsonPartialWildcardMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/SystemTextJsonPartialWildcardMatcherTests.cs @@ -366,17 +366,33 @@ public class SystemTextJsonPartialWildcardMatcherTests Assert.Equal(1.0, match); } - [Fact] - public void SystemTextJsonPartialWildcardMatcher_IsMatch_JsonElement_ShouldMatch() - { - // Assign - var matcher = new SystemTextJsonPartialWildcardMatcher(new { Id = 1, Name = "Test" }); + [Fact] + public void SystemTextJsonPartialWildcardMatcher_IsMatch_JsonElement_ShouldMatch() + { + // Assign + var matcher = new SystemTextJsonPartialWildcardMatcher(new { Id = 1, Name = "Test" }); - // Act - var jsonElement = JsonDocument.Parse("{ \"Id\" : 1, \"Name\" : \"Test\", \"Extra\" : \"value\" }").RootElement; - var match = matcher.IsMatch(jsonElement).Score; + // Act + var jsonElement = JsonDocument.Parse("{ \"Id\" : 1, \"Name\" : \"Test\", \"Extra\" : \"value\" }").RootElement; + var match = matcher.IsMatch(jsonElement).Score; - // Assert - Assert.Equal(1.0, match); + // Assert + Assert.Equal(1.0, match); + } + + [Fact] + public void SystemTextJsonPartialWildcardMatcher_IsMatch_WithRegexTrue_DateFormat_ShouldMatch() + { + // Assign + var matcher = new SystemTextJsonPartialWildcardMatcher( + new { date = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?(?:Z|[+-]\\d{2}:\\d{2})$", id = 1 }, + ignoreCase: false, + regex: true); + + // Act + var match = matcher.IsMatch("{\"date\":\"2026-06-09T22:23:18.53421+00:00\",\"id\":1}").Score; + + // Assert + Assert.Equal(1.0, match); + } } -} diff --git a/test/WireMock.Net.Tests/WireMockServerTests.WithBody.cs b/test/WireMock.Net.Tests/WireMockServerTests.WithBody.cs index 7bba8581..2a01d7ab 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.WithBody.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.WithBody.cs @@ -53,6 +53,81 @@ public partial class WireMockServerTests response.Should().BeEquivalentTo("{\"first_name\":\"John\",\"last_name\":\"Smith\"}"); } + [Fact] + public async Task WireMockServer_WithBodyAsJson_WithJsonPartialWildcardMatcher() + { + // Arrange + using var server = WireMockServer.Start(new WireMockServerSettings + { + Logger = new TestOutputHelperWireMockLogger(testOutputHelper) + }); + server + .Given( + Request.Create() + .WithPath("/test") + .UsingPost() + .WithBody(new JsonPartialWildcardMatcher( + new + { + date = @"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$", + id = 1 + }, + ignoreCase: true, + regex: true + ) + ) + ) + .RespondWith(Response.Create().WithStatusCode(200).WithBody("matched")); + + // Act + var requestUri = new Uri($"http://localhost:{server.Port}/test"); + + var response = await server.CreateClient().PostAsync(requestUri, new StringContent("{\"date\":\"2026-06-09T22:23:18.53421+00:00\",\"id\":1}"), _ct); + + // Assert + response.StatusCode.Should().Be(HttpStatusCode.OK); + + server.Stop(); + } + + [Fact] + public async Task WireMockServer_WithBodyAsJson_WithSystemTextJsonPartialWildcardMatcher() + { + // Arrange + using var server = WireMockServer.Start(new WireMockServerSettings + { + Logger = new TestOutputHelperWireMockLogger(testOutputHelper), + DefaultJsonSerializer = new SystemTextJsonConverter() + }); + server + .Given( + Request.Create() + .WithPath("/test") + .UsingPost() + .WithBody(new SystemTextJsonPartialWildcardMatcher( + new + { + date = @"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$", + id = 1 + }, + ignoreCase: true, + regex: true + ) + ) + ) + .RespondWith(Response.Create().WithStatusCode(200).WithBody("matched")); + + // Act + var requestUri = new Uri($"http://localhost:{server.Port}/test"); + + var response = await server.CreateClient().PostAsync(requestUri, new StringContent("{\"date\":\"2026-06-09T22:23:18.53421+00:00\",\"id\":1}"), _ct); + + // Assert + response.StatusCode.Should().Be(HttpStatusCode.OK); + + server.Stop(); + } + [Fact] public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch() {