diff --git a/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs b/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs index a39469763..ed512adaf 100644 --- a/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs +++ b/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs @@ -1,70 +1,79 @@ -using System; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Threading; -using log4net; -using log4net.Config; -using log4net.Repository; -using WireMock.RequestBuilders; -using WireMock.ResponseBuilders; -using WireMock.Server; -using WireMock.Settings; - -namespace WireMock.Net.StandAlone.NETCoreApp -{ - static class Program - { - private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); - // private static readonly ILog Log = LogManager.GetLogger(typeof(Program)); - - private static int sleepTime = 30000; - private static WireMockServer _server; - - static void Main(string[] args) - { - XmlConfigurator.Configure(LogRepository, new FileInfo("log4net.config")); - +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading; +using log4net; +using log4net.Config; +using log4net.Repository; +using WireMock.RequestBuilders; +using WireMock.ResponseBuilders; +using WireMock.Server; +using WireMock.Settings; +using WireMock.Util; + +namespace WireMock.Net.StandAlone.NETCoreApp +{ + static class Program + { + private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); + // private static readonly ILog Log = LogManager.GetLogger(typeof(Program)); + + private static int sleepTime = 30000; + private static WireMockServer _server; + + static void Main(string[] args) + { + XmlConfigurator.Configure(LogRepository, new FileInfo("log4net.config")); + if (!WireMockServerSettingsParser.TryParseArguments(args, out var settings, new WireMockLog4NetLogger())) { return; } - settings.Logger.Debug("WireMock.Net server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'"))); - - _server = WireMockServer.Start(settings); - - _server - .Given(Request.Create() - .UsingAnyMethod()) - .RespondWith(Response.Create() - .WithTransformer() - .WithBody("{{Random Type=\"Integer\" Min=100 Max=999999}} {{DateTime.Now}} {{DateTime.Now \"yyyy-MMM\"}} {{String.Format (DateTime.Now) \"MMM-dd\"}}")); - - Console.WriteLine($"{DateTime.UtcNow} Press Ctrl+C to shut down"); - - Console.CancelKeyPress += (s, e) => - { - Stop("CancelKeyPress"); - }; - - System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx => - { - Stop("AssemblyLoadContext.Default.Unloading"); - }; - - while (true) - { - Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server running : {_server.IsStarted}"); - Thread.Sleep(sleepTime); - } - } - - private static void Stop(string why) - { - Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopping because '{why}'"); - _server.Stop(); - Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopped"); - } - } + settings.Logger.Debug("WireMock.Net server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'"))); + + _server = WireMockServer.Start(settings); + + _server.Given(Request.Create().WithPath("/api/sap") + .UsingPost() + .WithBody((IBodyData xmlData) => { + //xmlData is always null + return true; + })) + .RespondWith(Response.Create().WithStatusCode(System.Net.HttpStatusCode.OK)); + + _server + .Given(Request.Create() + .UsingAnyMethod()) + .RespondWith(Response.Create() + .WithTransformer() + .WithBody("{{Random Type=\"Integer\" Min=100 Max=999999}} {{DateTime.Now}} {{DateTime.Now \"yyyy-MMM\"}} {{String.Format (DateTime.Now) \"MMM-dd\"}}")); + + Console.WriteLine($"{DateTime.UtcNow} Press Ctrl+C to shut down"); + + Console.CancelKeyPress += (s, e) => + { + Stop("CancelKeyPress"); + }; + + System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx => + { + Stop("AssemblyLoadContext.Default.Unloading"); + }; + + while (true) + { + Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server running : {_server.IsStarted}"); + Thread.Sleep(sleepTime); + } + } + + private static void Stop(string why) + { + Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopping because '{why}'"); + _server.Stop(); + Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopped"); + } + } } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs index 93fe0adbe..d2e8d307e 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using WireMock.Types; +using WireMock.Util; using WireMock.Validation; namespace WireMock.Matchers.Request @@ -26,6 +27,11 @@ public class RequestMessageBodyMatcher : IRequestMatcher /// public Func JsonFunc { get; } + /// + /// The body data function for BodyData + /// + public Func BodyDataFunc { get; } + /// /// The matchers. /// @@ -88,6 +94,16 @@ public RequestMessageBodyMatcher([NotNull] Func func) JsonFunc = func; } + /// + /// Initializes a new instance of the class. + /// + /// The function. + public RequestMessageBodyMatcher([NotNull] Func func) + { + Check.NotNull(func, nameof(func)); + BodyDataFunc = func; + } + /// /// Initializes a new instance of the class. /// @@ -158,6 +174,11 @@ private double CalculateMatchScore(IRequestMessage requestMessage) return MatchScores.ToScore(DataFunc(requestMessage?.BodyData?.BodyAsBytes)); } + if (BodyDataFunc != null) + { + return MatchScores.ToScore(BodyDataFunc(requestMessage?.BodyData)); + } + return MatchScores.Mismatch; } } diff --git a/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs index 464d62f7c..f4e768a15 100644 --- a/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs @@ -2,7 +2,8 @@ using System; using WireMock.Matchers; using WireMock.Matchers.Request; - +using WireMock.Util; + namespace WireMock.RequestBuilders { /// @@ -63,10 +64,17 @@ public interface IBodyRequestBuilder : IRequestMatcher IRequestBuilder WithBody([NotNull] Func func); /// - /// WithBody: func (object) + /// WithBody: func (json object) /// /// The function. /// The . IRequestBuilder WithBody([NotNull] Func func); + + /// + /// WithBody: func (BodyData object) + /// + /// The function. + /// The . + IRequestBuilder WithBody([NotNull] Func func); } } \ No newline at end of file diff --git a/src/WireMock.Net/RequestBuilders/Request.WithBody.cs b/src/WireMock.Net/RequestBuilders/Request.WithBody.cs index 2828a26eb..a1f60b5ff 100644 --- a/src/WireMock.Net/RequestBuilders/Request.WithBody.cs +++ b/src/WireMock.Net/RequestBuilders/Request.WithBody.cs @@ -3,6 +3,7 @@ using System; using WireMock.Matchers; using WireMock.Matchers.Request; +using WireMock.Util; using WireMock.Validation; namespace WireMock.RequestBuilders @@ -71,5 +72,14 @@ public IRequestBuilder WithBody(Func func) _requestMatchers.Add(new RequestMessageBodyMatcher(func)); return this; } + + /// + public IRequestBuilder WithBody(Func func) + { + Check.NotNull(func, nameof(func)); + + _requestMatchers.Add(new RequestMessageBodyMatcher(func)); + return this; + } } } diff --git a/test/WireMock.Net.Tests/RequestWithBodyTests.cs b/test/WireMock.Net.Tests/RequestWithBodyTests.cs index 82d4420f3..1cb5e3f46 100644 --- a/test/WireMock.Net.Tests/RequestWithBodyTests.cs +++ b/test/WireMock.Net.Tests/RequestWithBodyTests.cs @@ -54,6 +54,25 @@ public void Request_WithBody_FuncJson() Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0); } + [Fact] + public void Request_WithBody_FuncBodyData() + { + // Assign + var requestBuilder = Request.Create().UsingAnyMethod().WithBody((IBodyData b) => b != null); + + // Act + var body = new BodyData + { + BodyAsJson = 123, + 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(1.0); + } + [Fact] public void Request_WithBody_FuncByteArray() {