Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,4 +70,4 @@ internal static MatchResult CalculateMatchScore(IBodyData? requestMessage, IMatc

return default;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,79 @@ public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IObjectMatche
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]
[MemberData(nameof(MatchingScoreData))]
public async Task RequestMessageBodyMatcher_GetMatchingScore_Funcs_Matching(object body, RequestMessageBodyMatcher matcher, bool shouldMatch)
Expand Down Expand Up @@ -459,13 +532,13 @@ public static TheoryData<object, RequestMessageBodyMatcher, bool> 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},
Expand All @@ -476,7 +549,7 @@ public static TheoryData<object, RequestMessageBodyMatcher, bool> 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},
Expand Down
Loading