diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs
index ad25e470..e0a08f1f 100644
--- a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs
+++ b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs
@@ -105,27 +105,38 @@ namespace WireMock.Matchers.Request
private double IsMatch(RequestMessage requestMessage)
{
- if (requestMessage.Body != null)
- {
- if (Matcher is IStringMatcher stringMatcher)
- {
- return stringMatcher.IsMatch(requestMessage.Body);
- }
- }
-
+ // Check if the matcher is a IObjectMatcher
if (Matcher is IObjectMatcher objectMatcher)
{
+ // If the body is a JSON object, try to match.
if (requestMessage.BodyAsJson != null)
{
return objectMatcher.IsMatch(requestMessage.BodyAsJson);
}
+ // If the body is a byte array, try to match.
if (requestMessage.BodyAsBytes != null)
{
return objectMatcher.IsMatch(requestMessage.BodyAsBytes);
}
}
+ // Check if the matcher is a IStringMatcher
+ if (Matcher is IStringMatcher stringMatcher)
+ {
+ // If the body is a JSON object, try to use Body (string) to match.
+ if (requestMessage.BodyAsJson != null && requestMessage.Body != null)
+ {
+ return stringMatcher.IsMatch(requestMessage.Body);
+ }
+
+ // If the string body is defined, try to match.
+ if (requestMessage.Body != null)
+ {
+ return stringMatcher.IsMatch(requestMessage.Body);
+ }
+ }
+
if (Func != null)
{
return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body));
diff --git a/src/WireMock.Net/RequestMessage.cs b/src/WireMock.Net/RequestMessage.cs
index 875c1974..bd5126ae 100644
--- a/src/WireMock.Net/RequestMessage.cs
+++ b/src/WireMock.Net/RequestMessage.cs
@@ -65,7 +65,7 @@ namespace WireMock
public string RawQuery { get; }
///
- /// The body as string.
+ /// The original body as string, this is defined when Body or BodyAsJson are not null.
///
public string Body { get; }
@@ -113,7 +113,7 @@ namespace WireMock
/// The body.
/// The headers.
/// The cookies.
- public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] BodyData body, [CanBeNull] IDictionary headers = null, [CanBeNull] IDictionary cookies = null)
+ public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] BodyData body = null, [CanBeNull] IDictionary headers = null, [CanBeNull] IDictionary cookies = null)
{
Check.NotNull(url, nameof(url));
Check.NotNull(method, nameof(method));
@@ -140,43 +140,6 @@ namespace WireMock
Query = ParseQuery(RawQuery);
}
- ///
- /// Initializes a new instance of the class.
- ///
- /// The original url.
- /// The HTTP method.
- /// The client IP Address.
- /// The bodyAsBytes byte[].
- /// The body string.
- /// The body encoding
- /// The headers.
- /// The cookies.
- public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNull] string body = null, [CanBeNull] Encoding bodyEncoding = null, [CanBeNull] IDictionary headers = null, [CanBeNull] IDictionary cookies = null)
- {
- Check.NotNull(url, nameof(url));
- Check.NotNull(method, nameof(method));
- Check.NotNull(clientIP, nameof(clientIP));
-
- Url = url.ToString();
- Protocol = url.Scheme;
- Host = url.Host;
- Port = url.Port;
- Origin = $"{url.Scheme}://{url.Host}:{url.Port}";
- Path = WebUtility.UrlDecode(url.AbsolutePath);
- PathSegments = Path.Split('/').Skip(1).ToArray();
- Method = method.ToLower();
- ClientIP = clientIP;
-
- BodyAsBytes = bodyAsBytes;
- Body = body;
- BodyEncoding = bodyEncoding;
-
- Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList(header.Value));
- Cookies = cookies;
- RawQuery = WebUtility.UrlDecode(url.Query);
- Query = ParseQuery(RawQuery);
- }
-
private static IDictionary> ParseQuery(string queryString)
{
if (string.IsNullOrEmpty(queryString))
diff --git a/src/WireMock.Net/Util/BodyData.cs b/src/WireMock.Net/Util/BodyData.cs
index 3e9d2d1d..2444b0f1 100644
--- a/src/WireMock.Net/Util/BodyData.cs
+++ b/src/WireMock.Net/Util/BodyData.cs
@@ -13,7 +13,7 @@ namespace WireMock.Util
public Encoding Encoding { get; set; }
///
- /// The body as string.
+ /// The body as string, this is defined when BodyAsString or BodyAsJson are not null.
///
public string BodyAsString { get; set; }
diff --git a/src/WireMock.Net/Util/BodyParser.cs b/src/WireMock.Net/Util/BodyParser.cs
index 617dbff2..c551ddc4 100644
--- a/src/WireMock.Net/Util/BodyParser.cs
+++ b/src/WireMock.Net/Util/BodyParser.cs
@@ -58,6 +58,7 @@ namespace WireMock.Util
else if (contentTypeHeaderValue != null && contentTypeHeaderValue.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
{
var stringData = await ReadStringAsync(stream);
+ data.BodyAsString = stringData.Item1;
data.Encoding = stringData.Item2;
try
diff --git a/test/WireMock.Net.Tests/FluentMockServerTests.cs b/test/WireMock.Net.Tests/FluentMockServerTests.cs
index d5bae319..8c080da8 100644
--- a/test/WireMock.Net.Tests/FluentMockServerTests.cs
+++ b/test/WireMock.Net.Tests/FluentMockServerTests.cs
@@ -1,9 +1,11 @@
using System;
+using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
+using System.Text;
using System.Threading.Tasks;
using NFluent;
using WireMock.Matchers;
@@ -18,6 +20,7 @@ namespace WireMock.Net.Tests
public partial class FluentMockServerTests : IDisposable
{
private FluentMockServer _server;
+ private static string jsonRequestMessage = @"{ ""message"" : ""Hello server"" }";
// For for AppVeyor + OpenCover
private string GetCurrentFolder()
@@ -348,6 +351,39 @@ namespace WireMock.Net.Tests
Check.That(responseAsBytes).ContainsExactly(new byte[] { 48, 49 });
}
+ public static IEnumerable