diff --git a/src/WireMock.Net.Shared/Matchers/Helpers/BodyDataMatchScoreCalculator.cs b/src/WireMock.Net.Shared/Matchers/Helpers/BodyDataMatchScoreCalculator.cs index 5b662f9e4..3e179835a 100644 --- a/src/WireMock.Net.Shared/Matchers/Helpers/BodyDataMatchScoreCalculator.cs +++ b/src/WireMock.Net.Shared/Matchers/Helpers/BodyDataMatchScoreCalculator.cs @@ -34,14 +34,10 @@ internal static MatchResult CalculateMatchScore(IBodyData? requestMessage, IMatc } } - if (matcher is ExactObjectMatcher exactObjectMatcher) + if (matcher is ExactObjectMatcher { Value: byte[] } exactObjectMatcher) { // If the body is a byte array, try to match. - var detectedBodyType = requestMessage.DetectedBodyType; - if (detectedBodyType is BodyType.Bytes or BodyType.String or BodyType.FormUrlEncoded) - { - return exactObjectMatcher.IsMatch(requestMessage.BodyAsBytes); - } + return exactObjectMatcher.IsMatch(requestMessage.BodyAsBytes); } // Check if the matcher is a IObjectMatcher @@ -74,4 +70,4 @@ internal static MatchResult CalculateMatchScore(IBodyData? requestMessage, IMatc return default; } -} \ 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 603bd0f87..ab53577a2 100644 --- a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs +++ b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs @@ -404,6 +404,79 @@ public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IObjectMatche objectMatcherMock.Verify(m => m.IsMatch(It.IsAny()), Times.Once); } + [Theory] + [InlineData(new byte[] { 1 })] + [InlineData(new byte[] { 48 })] + public void RequestMessageBodyMatcher_GetMatchingScore_BodyTypeBytes_BodyAsBytes_ExactObjectMapper(byte[] bytes) + { + // Assign + var body = new BodyData + { + BodyAsBytes = bytes, + DetectedBodyType = BodyType.Bytes + }; + var exactObjectMapper = new ExactObjectMatcher(bytes); + + var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); + + var matcher = new RequestMessageBodyMatcher(exactObjectMapper); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } + + [Fact] + public void RequestMessageBodyMatcher_GetMatchingScore_BodyTypeString_BodyAsBytes_ExactObjectMapper() + { + // Assign + var bytes = Encoding.UTF8.GetBytes("hello"); + var body = new BodyData + { + BodyAsBytes = bytes, + DetectedBodyType = BodyType.String + }; + var exactObjectMapper = new ExactObjectMatcher(bytes); + + var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); + + var matcher = new RequestMessageBodyMatcher(exactObjectMapper); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } + + [Fact] + public void RequestMessageBodyMatcher_GetMatchingScore_BodyTypeJson_BodyAsBytes_ExactObjectMapper() + { + // Assign + var bytes = Encoding.UTF8.GetBytes("""{"value":42}"""); + var body = new BodyData + { + BodyAsBytes = bytes, + DetectedBodyType = BodyType.Json + }; + var exactObjectMapper = new ExactObjectMatcher(bytes); + + var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); + + var matcher = new RequestMessageBodyMatcher(exactObjectMapper); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } + [Theory] [MemberData(nameof(MatchingScoreData))] public async Task RequestMessageBodyMatcher_GetMatchingScore_Funcs_Matching(object body, RequestMessageBodyMatcher matcher, bool shouldMatch) @@ -459,13 +532,13 @@ public static TheoryData MatchingScoreD {json, new RequestMessageBodyMatcher((object? o) => ((dynamic) o!).a == "b"), true}, {json, new RequestMessageBodyMatcher((string? s) => s == json), true}, {json, new RequestMessageBodyMatcher((byte[]? b) => b?.SequenceEqual(Encoding.UTF8.GetBytes(json)) == true), true}, - + // JSON no match --- {json, new RequestMessageBodyMatcher((object? o) => false), false}, {json, new RequestMessageBodyMatcher((string? s) => false), false}, {json, new RequestMessageBodyMatcher((byte[]? b) => false), false}, {json, new RequestMessageBodyMatcher(), false }, - + // string match +++ {str, new RequestMessageBodyMatcher((object? o) => o == null), true}, {str, new RequestMessageBodyMatcher((string? s) => s == str), true}, @@ -476,7 +549,7 @@ public static TheoryData MatchingScoreD {str, new RequestMessageBodyMatcher((string? s) => false), false}, {str, new RequestMessageBodyMatcher((byte[]? b) => false), false}, {str, new RequestMessageBodyMatcher(), false }, - + // binary match +++ {bytes, new RequestMessageBodyMatcher((object? o) => o == null), true}, {bytes, new RequestMessageBodyMatcher((string? s) => s == null), true},