Fixes an issue with matching JSON bodies as bytes (#1339)

* Fixes an issue with matching JSON bodies as bytes

* Adding tests for exact object matching

* Simplify the check for byte data
This commit is contained in:
Sam Fields
2025-08-02 14:11:13 -04:00
committed by GitHub
parent e400e92452
commit 6ccfe68686
2 changed files with 79 additions and 10 deletions

View File

@@ -34,14 +34,10 @@ internal static class BodyDataMatchScoreCalculator
} }
} }
if (matcher is ExactObjectMatcher exactObjectMatcher) if (matcher is ExactObjectMatcher { Value: byte[] } exactObjectMatcher)
{ {
// If the body is a byte array, try to match. // If the body is a byte array, try to match.
var detectedBodyType = requestMessage.DetectedBodyType; return exactObjectMatcher.IsMatch(requestMessage.BodyAsBytes);
if (detectedBodyType is BodyType.Bytes or BodyType.String or BodyType.FormUrlEncoded)
{
return exactObjectMatcher.IsMatch(requestMessage.BodyAsBytes);
}
} }
// Check if the matcher is a IObjectMatcher // Check if the matcher is a IObjectMatcher

View File

@@ -404,6 +404,79 @@ public class RequestMessageBodyMatcherTests
objectMatcherMock.Verify(m => m.IsMatch(It.IsAny<byte[]>()), Times.Once); objectMatcherMock.Verify(m => m.IsMatch(It.IsAny<byte[]>()), 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] [Theory]
[MemberData(nameof(MatchingScoreData))] [MemberData(nameof(MatchingScoreData))]
public async Task RequestMessageBodyMatcher_GetMatchingScore_Funcs_Matching(object body, RequestMessageBodyMatcher matcher, bool shouldMatch) public async Task RequestMessageBodyMatcher_GetMatchingScore_Funcs_Matching(object body, RequestMessageBodyMatcher matcher, bool shouldMatch)