diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher`1.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher`1.cs new file mode 100644 index 00000000..2ee8a4ab --- /dev/null +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher`1.cs @@ -0,0 +1,60 @@ +// Copyright © WireMock.Net + +using System; +using Newtonsoft.Json.Linq; +using Stef.Validation; + +namespace WireMock.Matchers.Request; + +/// +/// The request body matcher. +/// +public class RequestMessageBodyMatcher : IRequestMatcher +{ + /// + /// The body data function for type T + /// + public Func? Func { get; } + + /// + /// The + /// + public MatchOperator MatchOperator { get; } = MatchOperator.Or; + + /// + /// Initializes a new instance of the class. + /// + /// The function. + public RequestMessageBodyMatcher(Func func) + { + Func = Guard.NotNull(func); + } + + /// + public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult) + { + var (score, exception) = CalculateMatchScore(requestMessage).Expand(); + return requestMatchResult.AddScore(GetType(), score, exception); + } + + private MatchResult CalculateMatchScore(IRequestMessage requestMessage) + { + if (Func != null) + { + if (requestMessage.BodyData?.BodyAsJson is JObject jsonObject) + { + try + { + var bodyAsT = jsonObject.ToObject(); + return MatchScores.ToScore(Func(bodyAsT)); + } + catch (Exception ex) + { + return new MatchResult(ex); + } + } + } + + return default; + } +} \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/RequestBuilders/Request.WithBody.cs b/src/WireMock.Net.Minimal/RequestBuilders/Request.WithBody.cs index fd840aae..1eec96da 100644 --- a/src/WireMock.Net.Minimal/RequestBuilders/Request.WithBody.cs +++ b/src/WireMock.Net.Minimal/RequestBuilders/Request.WithBody.cs @@ -34,13 +34,6 @@ public partial class Request return this; } - /// - public IRequestBuilder WithBodyAsJson(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) - { - var matcher = body as IMatcher ?? new JsonMatcher(matchBehaviour, body); - return WithBody([matcher]); - } - /// public IRequestBuilder WithBody(IMatcher matcher) { @@ -98,4 +91,20 @@ public partial class Request _requestMatchers.Add(new RequestMessageBodyMatcher(Guard.NotNull(func))); return this; } + + /// + public IRequestBuilder WithBodyAsJson(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + { + var matcher = body as IMatcher ?? new JsonMatcher(matchBehaviour, body); + return WithBody([matcher]); + } + + /// + public IRequestBuilder WithBodyAsType(Func func) + { + Guard.NotNull(func); + + _requestMatchers.Add(new RequestMessageBodyMatcher(func)); + return this; + } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/RequestBuilders/Request.cs b/src/WireMock.Net.Minimal/RequestBuilders/Request.cs index 4071cf95..8b0d80c1 100644 --- a/src/WireMock.Net.Minimal/RequestBuilders/Request.cs +++ b/src/WireMock.Net.Minimal/RequestBuilders/Request.cs @@ -29,7 +29,7 @@ public partial class Request : RequestMessageCompositeMatcher, IRequestBuilder /// The . public static IRequestBuilder Create() { - return new Request(new List()); + return new Request([]); } /// diff --git a/src/WireMock.Net.Shared/RequestBuilders/IBodyRequestBuilder.cs b/src/WireMock.Net.Shared/RequestBuilders/IBodyRequestBuilder.cs index f22c1b33..1e049b91 100644 --- a/src/WireMock.Net.Shared/RequestBuilders/IBodyRequestBuilder.cs +++ b/src/WireMock.Net.Shared/RequestBuilders/IBodyRequestBuilder.cs @@ -80,6 +80,14 @@ public interface IBodyRequestBuilder : IMultiPartRequestBuilder /// The . IRequestBuilder WithBody(Func func); + /// + /// WithBody: func (type) + /// + /// The type. + /// The function. + /// The . + IRequestBuilder WithBodyAsType(Func func); + /// /// WithBody: func (BodyData object) /// diff --git a/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithBodyTests.cs b/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithBodyTests.cs index 7244147f..09cab484 100644 --- a/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithBodyTests.cs +++ b/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithBodyTests.cs @@ -1,11 +1,12 @@ // Copyright © WireMock.Net using System; -using FluentAssertions; using System.Collections.Generic; using System.Linq; using System.Text; +using FluentAssertions; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using NFluent; using WireMock.Matchers; using WireMock.Matchers.Request; @@ -72,15 +73,17 @@ public class RequestBuilderWithBodyTests } [Fact] - public void Request_WithBody_FuncJson() + public void Request_WithBody_FuncObject() { // Assign - var requestBuilder = Request.Create().UsingAnyMethod().WithBody(b => b != null); + var requestBuilder = Request.Create() + .UsingAnyMethod() + .WithBody(b => b != null); // Act var body = new BodyData { - BodyAsJson = 123, + BodyAsJson = JObject.Parse("""{ "X": 123, "Y": "a" }"""), DetectedBodyType = BodyType.Json }; var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body); @@ -90,6 +93,57 @@ public class RequestBuilderWithBodyTests Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0); } + [Theory] + [InlineData("""{ "X": 123, "Y": "a" }""", 1.0)] + [InlineData("""{ "X": 123, "Y": "b" }""", 0.0)] + public void Request_WithBodyAsType_Func(string json, double expected) + { + // Assign + var requestBuilder = Request.Create() + .UsingAnyMethod() + .WithBodyAsType(ft => ft != null && ft.X == 123 && ft.Y == "a"); + + // Act + var body = new BodyData + { + BodyAsJson = JObject.Parse(json), + DetectedBodyType = BodyType.Json + }; + var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body); + + // Assert + var requestMatchResult = new RequestMatchResult(); + Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(expected); + } + + [Fact] + public void Request_WithBodyAsType_Func_IncorrectType() + { + // Assign + var requestBuilder = Request.Create() + .UsingAnyMethod() + .WithBodyAsType(ft => ft != null); + + // Act + var body = new BodyData + { + BodyAsJson = JObject.Parse("""{ "X": 123, "Y": "a" }"""), + DetectedBodyType = BodyType.Json + }; + var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body); + + // Assert + var requestMatchResult = new RequestMatchResult(); + Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(0.0); + } + + private class FuncType + { + public int X { get; set; } = 42; + + public string Y { get; set; } = string.Empty; + } + [Fact] public void Request_WithBody_FuncFormUrlEncoded() {