diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs
index d2e8d307..d891bc7d 100644
--- a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs
+++ b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs
@@ -1,185 +1,195 @@
-using JetBrains.Annotations;
-using System;
-using System.Linq;
-using WireMock.Types;
+using JetBrains.Annotations;
+using System;
+using System.Linq;
+using WireMock.Types;
using WireMock.Util;
-using WireMock.Validation;
-
-namespace WireMock.Matchers.Request
-{
- ///
- /// The request body matcher.
- ///
- public class RequestMessageBodyMatcher : IRequestMatcher
- {
- ///
- /// The body function
- ///
- public Func Func { get; }
-
- ///
- /// The body data function for byte[]
- ///
- public Func DataFunc { get; }
-
- ///
- /// The body data function for json
- ///
- public Func JsonFunc { get; }
-
- ///
- /// The body data function for BodyData
- ///
- public Func BodyDataFunc { get; }
-
- ///
- /// The matchers.
- ///
- public IMatcher[] Matchers { get; }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The match behaviour.
- /// The body.
- public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] string body) : this(new[] { new WildcardMatcher(matchBehaviour, body) }.Cast().ToArray())
- {
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The match behaviour.
- /// The body.
- public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] byte[] body) : this(new[] { new ExactObjectMatcher(matchBehaviour, body) }.Cast().ToArray())
- {
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The match behaviour.
- /// The body.
- public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] object body) : this(new[] { new ExactObjectMatcher(matchBehaviour, body) }.Cast().ToArray())
- {
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The function.
- public RequestMessageBodyMatcher([NotNull] Func func)
- {
- Check.NotNull(func, nameof(func));
- Func = func;
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The function.
- public RequestMessageBodyMatcher([NotNull] Func func)
- {
- Check.NotNull(func, nameof(func));
- DataFunc = func;
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The function.
- public RequestMessageBodyMatcher([NotNull] Func func)
- {
- Check.NotNull(func, nameof(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.
- ///
- /// The matchers.
- public RequestMessageBodyMatcher([NotNull] params IMatcher[] matchers)
- {
- Check.NotNull(matchers, nameof(matchers));
- Matchers = matchers;
- }
-
- ///
- public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
- {
- double score = CalculateMatchScore(requestMessage);
- return requestMatchResult.AddScore(GetType(), score);
- }
-
- private double CalculateMatchScore(IRequestMessage requestMessage, IMatcher matcher)
- {
- // Check if the matcher is a IObjectMatcher
- if (matcher is IObjectMatcher objectMatcher)
- {
- // If the body is a JSON object, try to match.
- if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json)
- {
- return objectMatcher.IsMatch(requestMessage.BodyData.BodyAsJson);
- }
-
- // If the body is a byte array, try to match.
- if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Bytes)
- {
- return objectMatcher.IsMatch(requestMessage.BodyData.BodyAsBytes);
- }
- }
-
- // Check if the matcher is a IStringMatcher
- if (matcher is IStringMatcher stringMatcher)
- {
- // If the body is a Json or a String, use the BodyAsString to match on.
- if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json || requestMessage?.BodyData?.DetectedBodyType == BodyType.String)
- {
- return stringMatcher.IsMatch(requestMessage.BodyData.BodyAsString);
- }
- }
-
- return MatchScores.Mismatch;
- }
-
- private double CalculateMatchScore(IRequestMessage requestMessage)
- {
- if (Matchers != null && Matchers.Any())
- {
- return Matchers.Max(matcher => CalculateMatchScore(requestMessage, matcher));
- }
-
- if (Func != null)
- {
- return MatchScores.ToScore(Func(requestMessage?.BodyData?.BodyAsString));
- }
-
- if (JsonFunc != null)
- {
- return MatchScores.ToScore(JsonFunc(requestMessage?.BodyData?.BodyAsJson));
- }
-
- if (DataFunc != null)
- {
- return MatchScores.ToScore(DataFunc(requestMessage?.BodyData?.BodyAsBytes));
- }
-
- if (BodyDataFunc != null)
- {
- return MatchScores.ToScore(BodyDataFunc(requestMessage?.BodyData));
- }
-
- return MatchScores.Mismatch;
- }
- }
+using WireMock.Validation;
+
+namespace WireMock.Matchers.Request
+{
+ ///
+ /// The request body matcher.
+ ///
+ public class RequestMessageBodyMatcher : IRequestMatcher
+ {
+ ///
+ /// The body function
+ ///
+ public Func Func { get; }
+
+ ///
+ /// The body data function for byte[]
+ ///
+ public Func DataFunc { get; }
+
+ ///
+ /// The body data function for json
+ ///
+ public Func JsonFunc { get; }
+
+ ///
+ /// The body data function for BodyData
+ ///
+ public Func BodyDataFunc { get; }
+
+ ///
+ /// The matchers.
+ ///
+ public IMatcher[] Matchers { get; }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The match behaviour.
+ /// The body.
+ public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] string body) : this(new[] { new WildcardMatcher(matchBehaviour, body) }.Cast().ToArray())
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The match behaviour.
+ /// The body.
+ public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] byte[] body) : this(new[] { new ExactObjectMatcher(matchBehaviour, body) }.Cast().ToArray())
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The match behaviour.
+ /// The body.
+ public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] object body) : this(new[] { new ExactObjectMatcher(matchBehaviour, body) }.Cast().ToArray())
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The function.
+ public RequestMessageBodyMatcher([NotNull] Func func)
+ {
+ Check.NotNull(func, nameof(func));
+ Func = func;
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The function.
+ public RequestMessageBodyMatcher([NotNull] Func func)
+ {
+ Check.NotNull(func, nameof(func));
+ DataFunc = func;
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The function.
+ public RequestMessageBodyMatcher([NotNull] Func func)
+ {
+ Check.NotNull(func, nameof(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.
+ ///
+ /// The matchers.
+ public RequestMessageBodyMatcher([NotNull] params IMatcher[] matchers)
+ {
+ Check.NotNull(matchers, nameof(matchers));
+ Matchers = matchers;
+ }
+
+ ///
+ public double GetMatchingScore(IRequestMessage requestMessage, RequestMatchResult requestMatchResult)
+ {
+ double score = CalculateMatchScore(requestMessage);
+ return requestMatchResult.AddScore(GetType(), score);
+ }
+
+ private double CalculateMatchScore(IRequestMessage requestMessage, IMatcher matcher)
+ {
+ if (matcher is ExactObjectMatcher exactObjectMatcher)
+ {
+ // If the body is a byte array, try to match.
+ var detectedBodyType = requestMessage?.BodyData?.DetectedBodyType;
+ if (detectedBodyType == BodyType.Bytes || detectedBodyType == BodyType.String)
+ {
+ return exactObjectMatcher.IsMatch(requestMessage.BodyData.BodyAsBytes);
+ }
+ }
+
+ // Check if the matcher is a IObjectMatcher
+ if (matcher is IObjectMatcher objectMatcher)
+ {
+ // If the body is a JSON object, try to match.
+ if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json)
+ {
+ return objectMatcher.IsMatch(requestMessage.BodyData.BodyAsJson);
+ }
+
+ // If the body is a byte array, try to match.
+ if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Bytes)
+ {
+ return objectMatcher.IsMatch(requestMessage.BodyData.BodyAsBytes);
+ }
+ }
+
+ // Check if the matcher is a IStringMatcher
+ if (matcher is IStringMatcher stringMatcher)
+ {
+ // If the body is a Json or a String, use the BodyAsString to match on.
+ if (requestMessage?.BodyData?.DetectedBodyType == BodyType.Json || requestMessage?.BodyData?.DetectedBodyType == BodyType.String)
+ {
+ return stringMatcher.IsMatch(requestMessage.BodyData.BodyAsString);
+ }
+ }
+
+ return MatchScores.Mismatch;
+ }
+
+ private double CalculateMatchScore(IRequestMessage requestMessage)
+ {
+ if (Matchers != null && Matchers.Any())
+ {
+ return Matchers.Max(matcher => CalculateMatchScore(requestMessage, matcher));
+ }
+
+ if (Func != null)
+ {
+ return MatchScores.ToScore(Func(requestMessage?.BodyData?.BodyAsString));
+ }
+
+ if (JsonFunc != null)
+ {
+ return MatchScores.ToScore(JsonFunc(requestMessage?.BodyData?.BodyAsJson));
+ }
+
+ if (DataFunc != null)
+ {
+ return MatchScores.ToScore(DataFunc(requestMessage?.BodyData?.BodyAsBytes));
+ }
+
+ if (BodyDataFunc != null)
+ {
+ return MatchScores.ToScore(BodyDataFunc(requestMessage?.BodyData));
+ }
+
+ return MatchScores.Mismatch;
+ }
+ }
}
\ No newline at end of file
diff --git a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs
index 6969a237..43a1e298 100644
--- a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs
+++ b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs
@@ -209,13 +209,15 @@ namespace WireMock.Net.Tests.RequestMatchers
Check.That(score).IsEqualTo(1.0d);
}
- [Fact]
- public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IObjectMatcher()
+ [Theory]
+ [InlineData(new byte[] { 1 })]
+ [InlineData(new byte[] { 48 })]
+ public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IObjectMatcher(byte[] bytes)
{
// Assign
var body = new BodyData
{
- BodyAsBytes = new byte[] { 1 },
+ BodyAsBytes = bytes,
DetectedBodyType = BodyType.Bytes
};
var objectMatcherMock = new Mock();
diff --git a/test/WireMock.Net.Tests/RequestWithBodyTests.cs b/test/WireMock.Net.Tests/RequestWithBodyTests.cs
index 1cb5e3f4..d4d04f20 100644
--- a/test/WireMock.Net.Tests/RequestWithBodyTests.cs
+++ b/test/WireMock.Net.Tests/RequestWithBodyTests.cs
@@ -2,6 +2,7 @@
using NFluent;
using System;
using System.Text;
+using FluentAssertions;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.Models;
@@ -307,17 +308,20 @@ namespace WireMock.Net.Tests
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
}
- [Fact]
- public void Request_WithBodyAsBytes_ExactObjectMatcher_true()
+ [Theory]
+ [InlineData(new byte[] { 1 }, BodyType.Bytes)]
+ [InlineData(new byte[] { 48, 49, 50 }, BodyType.Bytes)]
+ [InlineData(new byte[] { 48, 49, 50 }, BodyType.String)]
+ public void Request_WithBodyAsBytes_ExactObjectMatcher_true(byte[] bytes, BodyType detectedBodyType)
{
// Assign
- byte[] body = { 123 };
+ byte[] body = bytes;
var requestBuilder = Request.Create().UsingAnyMethod().WithBody(body);
var bodyData = new BodyData
{
- BodyAsBytes = new byte[] { 123 },
- DetectedBodyType = BodyType.Bytes
+ BodyAsBytes = bytes,
+ DetectedBodyType = detectedBodyType
};
// Act
@@ -325,7 +329,7 @@ namespace WireMock.Net.Tests
// Assert
var requestMatchResult = new RequestMatchResult();
- Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
+ requestBuilder.GetMatchingScore(request, requestMatchResult).Should().Be(1.0);
}
}
}
\ No newline at end of file