mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-18 07:00:04 +02:00
Add some more RequestMessageBodyMatcherTests (#112)
This commit is contained in:
@@ -147,12 +147,10 @@ namespace WireMock.Serialization
|
|||||||
return newDictionary;
|
return newDictionary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//private static string[] Map<T>([CanBeNull] IEnumerable<Func<T, bool>> funcs)
|
||||||
|
//{
|
||||||
private static string[] Map<T>([CanBeNull] IEnumerable<Func<T, bool>> funcs)
|
// return funcs?.Select(Map).Where(x => x != null).ToArray();
|
||||||
{
|
//}
|
||||||
return funcs?.Select(Map).Where(x => x != null).ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string Map<T>([CanBeNull] Func<T, bool> func)
|
private static string Map<T>([CanBeNull] Func<T, bool> func)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
using System;
|
||||||
|
using Moq;
|
||||||
|
using NFluent;
|
||||||
|
using WireMock.Matchers;
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
|
using WireMock.Util;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests.RequestMatchers
|
||||||
|
{
|
||||||
|
public class RequestMessageBodyMatcherTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatcher()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsString = "b"
|
||||||
|
};
|
||||||
|
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||||
|
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||||
|
|
||||||
|
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.5d);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
stringMatcherMock.Verify(m => m.GetPatterns(), Times.Never);
|
||||||
|
stringMatcherMock.Verify(m => m.IsMatch("b"), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IStringMatcher()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsBytes = new byte[] { 1 }
|
||||||
|
};
|
||||||
|
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||||
|
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||||
|
|
||||||
|
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.0d);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
stringMatcherMock.Verify(m => m.GetPatterns(), Times.Never);
|
||||||
|
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_IStringMatcher()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsJson = new { value = 42 }
|
||||||
|
};
|
||||||
|
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||||
|
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||||
|
|
||||||
|
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.0d);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
stringMatcherMock.Verify(m => m.IsMatch("b"), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_IObjectMatcher()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsJson = 42
|
||||||
|
};
|
||||||
|
var objectMatcherMock = new Mock<IObjectMatcher>();
|
||||||
|
objectMatcherMock.Setup(m => m.IsMatch(It.IsAny<object>())).Returns(0.5d);
|
||||||
|
|
||||||
|
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(objectMatcherMock.Object);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.5d);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
objectMatcherMock.Verify(m => m.IsMatch(42), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IObjectMatcher()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsBytes = new byte[] { 1 }
|
||||||
|
};
|
||||||
|
var objectMatcherMock = new Mock<IObjectMatcher>();
|
||||||
|
objectMatcherMock.Setup(m => m.IsMatch(It.IsAny<object>())).Returns(0.5d);
|
||||||
|
|
||||||
|
var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(objectMatcherMock.Object);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.5d);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
objectMatcherMock.Verify(m => m.IsMatch(It.IsAny<byte[]>()), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user