mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-21 08:21:53 +02:00
Add NotNullOrEmptyMatcher (#625)
This commit is contained in:
6
.editorconfig
Normal file
6
.editorconfig
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[*]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
end_of_line = crlf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>1.4.19-preview-01</VersionPrefix>
|
<VersionPrefix>1.4.19</VersionPrefix>
|
||||||
<PackageReleaseNotes>See CHANGELOG.md</PackageReleaseNotes>
|
<PackageReleaseNotes>See CHANGELOG.md</PackageReleaseNotes>
|
||||||
<PackageIconUrl>https://raw.githubusercontent.com/WireMock-Net/WireMock.Net/master/WireMock.Net-Logo.png</PackageIconUrl>
|
<PackageIconUrl>https://raw.githubusercontent.com/WireMock-Net/WireMock.Net/master/WireMock.Net-Logo.png</PackageIconUrl>
|
||||||
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 16
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 16.0.30114.105
|
VisualStudioVersion = 17.0.31521.260
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8F890C6F-9ACC-438D-928A-AD61CDA862F2}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8F890C6F-9ACC-438D-928A-AD61CDA862F2}"
|
||||||
EndProject
|
EndProject
|
||||||
@@ -21,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{98
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7EFB2C5B-1BB2-4AAF-BC9F-216ED80C594D}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7EFB2C5B-1BB2-4AAF-BC9F-216ED80C594D}"
|
||||||
ProjectSection(SolutionItems) = preProject
|
ProjectSection(SolutionItems) = preProject
|
||||||
|
.editorconfig = .editorconfig
|
||||||
.gitignore = .gitignore
|
.gitignore = .gitignore
|
||||||
azure-pipelines-ci-linux.yml = azure-pipelines-ci-linux.yml
|
azure-pipelines-ci-linux.yml = azure-pipelines-ci-linux.yml
|
||||||
azure-pipelines-ci.yml = azure-pipelines-ci.yml
|
azure-pipelines-ci.yml = azure-pipelines-ci.yml
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
namespace WireMock.Matchers
|
namespace WireMock.Matchers
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// IValueMatcher
|
/// IValueMatcher
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <seealso cref="IObjectMatcher" />
|
/// <seealso cref="IObjectMatcher" />
|
||||||
public interface IValueMatcher: IObjectMatcher
|
public interface IValueMatcher : IObjectMatcher
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the value (can be a string or an object).
|
/// Gets the value (can be a string or an object).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Value</returns>
|
/// <returns>Value</returns>
|
||||||
object Value { get; }
|
object Value { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
52
src/WireMock.Net/Matchers/NotNullOrEmptyMatcher.cs
Normal file
52
src/WireMock.Net/Matchers/NotNullOrEmptyMatcher.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace WireMock.Matchers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// NotNullOrEmptyMatcher
|
||||||
|
/// </summary>
|
||||||
|
/// <seealso cref="IObjectMatcher" />
|
||||||
|
public class NotNullOrEmptyMatcher : IObjectMatcher
|
||||||
|
{
|
||||||
|
/// <inheritdoc cref="IMatcher.Name"/>
|
||||||
|
public string Name => "NotNullOrEmptyMatcher";
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
|
||||||
|
public MatchBehaviour MatchBehaviour { get; }
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMatcher.ThrowException"/>
|
||||||
|
public bool ThrowException { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="NotNullOrEmptyMatcher"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
|
public NotNullOrEmptyMatcher(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
|
{
|
||||||
|
MatchBehaviour = matchBehaviour;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
|
||||||
|
public double IsMatch(object input)
|
||||||
|
{
|
||||||
|
bool match;
|
||||||
|
|
||||||
|
switch (input)
|
||||||
|
{
|
||||||
|
case string @string:
|
||||||
|
match = !string.IsNullOrEmpty(@string);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case byte[] bytes:
|
||||||
|
match = bytes != null && bytes.Any();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
match = input != null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,113 +1,113 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
|
||||||
namespace WireMock.Matchers
|
namespace WireMock.Matchers
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Regular Expression Matcher
|
/// Regular Expression Matcher
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <inheritdoc cref="IStringMatcher"/>
|
/// <inheritdoc cref="IStringMatcher"/>
|
||||||
/// <inheritdoc cref="IIgnoreCaseMatcher"/>
|
/// <inheritdoc cref="IIgnoreCaseMatcher"/>
|
||||||
public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher
|
public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher
|
||||||
{
|
{
|
||||||
private readonly string[] _patterns;
|
private readonly string[] _patterns;
|
||||||
private readonly Regex[] _expressions;
|
private readonly Regex[] _expressions;
|
||||||
|
|
||||||
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
|
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
|
||||||
public MatchBehaviour MatchBehaviour { get; }
|
public MatchBehaviour MatchBehaviour { get; }
|
||||||
|
|
||||||
/// <inheritdoc cref="IMatcher.ThrowException"/>
|
/// <inheritdoc cref="IMatcher.ThrowException"/>
|
||||||
public bool ThrowException { get; }
|
public bool ThrowException { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="pattern">The pattern.</param>
|
/// <param name="pattern">The pattern.</param>
|
||||||
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
|
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
|
||||||
public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
|
public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <param name="pattern">The pattern.</param>
|
/// <param name="pattern">The pattern.</param>
|
||||||
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
|
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
|
||||||
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
|
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="patterns">The patterns.</param>
|
/// <param name="patterns">The patterns.</param>
|
||||||
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
|
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
|
||||||
public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
|
public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <param name="patterns">The patterns.</param>
|
/// <param name="patterns">The patterns.</param>
|
||||||
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
|
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
|
||||||
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
|
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
|
||||||
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string[] patterns, bool ignoreCase = false, bool throwException = false)
|
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string[] patterns, bool ignoreCase = false, bool throwException = false)
|
||||||
{
|
{
|
||||||
Check.NotNull(patterns, nameof(patterns));
|
Check.NotNull(patterns, nameof(patterns));
|
||||||
|
|
||||||
_patterns = patterns;
|
_patterns = patterns;
|
||||||
IgnoreCase = ignoreCase;
|
IgnoreCase = ignoreCase;
|
||||||
MatchBehaviour = matchBehaviour;
|
MatchBehaviour = matchBehaviour;
|
||||||
ThrowException = throwException;
|
ThrowException = throwException;
|
||||||
|
|
||||||
RegexOptions options = RegexOptions.Compiled | RegexOptions.Multiline;
|
RegexOptions options = RegexOptions.Compiled | RegexOptions.Multiline;
|
||||||
|
|
||||||
if (ignoreCase)
|
if (ignoreCase)
|
||||||
{
|
{
|
||||||
options |= RegexOptions.IgnoreCase;
|
options |= RegexOptions.IgnoreCase;
|
||||||
}
|
|
||||||
|
|
||||||
_expressions = patterns.Select(p => new Regex(p, options)).ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
|
|
||||||
public virtual double IsMatch(string input)
|
|
||||||
{
|
|
||||||
double match = MatchScores.Mismatch;
|
|
||||||
if (input != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
match = MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input)));
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
if (ThrowException)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
|
_expressions = patterns.Select(p => new Regex(p, options)).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
|
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
|
||||||
public virtual string[] GetPatterns()
|
public virtual double IsMatch(string input)
|
||||||
{
|
{
|
||||||
return _patterns;
|
double match = MatchScores.Mismatch;
|
||||||
}
|
if (input != null)
|
||||||
|
{
|
||||||
/// <inheritdoc cref="IMatcher.Name"/>
|
try
|
||||||
public virtual string Name => "RegexMatcher";
|
{
|
||||||
|
match = MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input)));
|
||||||
/// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/>
|
}
|
||||||
public bool IgnoreCase { get; }
|
catch (Exception)
|
||||||
}
|
{
|
||||||
|
if (ThrowException)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
|
||||||
|
public virtual string[] GetPatterns()
|
||||||
|
{
|
||||||
|
return _patterns;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IMatcher.Name"/>
|
||||||
|
public virtual string Name => "RegexMatcher";
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/>
|
||||||
|
public bool IgnoreCase { get; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -123,6 +123,22 @@ namespace WireMock.Matchers.Request
|
|||||||
|
|
||||||
private double CalculateMatchScore(IRequestMessage requestMessage, IMatcher matcher)
|
private double CalculateMatchScore(IRequestMessage requestMessage, IMatcher matcher)
|
||||||
{
|
{
|
||||||
|
if (matcher is NotNullOrEmptyMatcher notNullOrEmptyMatcher)
|
||||||
|
{
|
||||||
|
switch (requestMessage?.BodyData?.DetectedBodyType)
|
||||||
|
{
|
||||||
|
case BodyType.Json:
|
||||||
|
case BodyType.String:
|
||||||
|
return notNullOrEmptyMatcher.IsMatch(requestMessage.BodyData.BodyAsString);
|
||||||
|
|
||||||
|
case BodyType.Bytes:
|
||||||
|
return notNullOrEmptyMatcher.IsMatch(requestMessage.BodyData.BodyAsBytes);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return MatchScores.Mismatch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (matcher is ExactObjectMatcher exactObjectMatcher)
|
if (matcher is ExactObjectMatcher exactObjectMatcher)
|
||||||
{
|
{
|
||||||
// If the body is a byte array, try to match.
|
// If the body is a byte array, try to match.
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
@@ -43,7 +43,10 @@ namespace WireMock.Serialization
|
|||||||
bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;
|
bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;
|
||||||
|
|
||||||
switch (matcherName)
|
switch (matcherName)
|
||||||
{
|
{
|
||||||
|
case "NotNullOrEmptyMatcher":
|
||||||
|
return new NotNullOrEmptyMatcher(matchBehaviour);
|
||||||
|
|
||||||
case "CSharpCodeMatcher":
|
case "CSharpCodeMatcher":
|
||||||
if (_settings.AllowCSharpCodeMatcher == true)
|
if (_settings.AllowCSharpCodeMatcher == true)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
using FluentAssertions;
|
||||||
|
using NFluent;
|
||||||
|
using WireMock.Matchers;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests.Matchers
|
||||||
|
{
|
||||||
|
public class NotNullOrEmptyMatcherTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void NotNullOrEmptyMatcher_GetName()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var matcher = new NotNullOrEmptyMatcher();
|
||||||
|
string name = matcher.Name;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Check.That(name).Equals("NotNullOrEmptyMatcher");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(null, 0.0)]
|
||||||
|
[InlineData(new byte[0], 0.0)]
|
||||||
|
[InlineData(new byte[] { 48 }, 1.0)]
|
||||||
|
public void NotNullOrEmptyMatcher_IsMatch_ByteArray(byte[] data, double expected)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var matcher = new NotNullOrEmptyMatcher();
|
||||||
|
double result = matcher.IsMatch(data);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().Be(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(null, 0.0)]
|
||||||
|
[InlineData("", 0.0)]
|
||||||
|
[InlineData("x", 1.0)]
|
||||||
|
public void NotNullOrEmptyMatcher_IsMatch_String(string data, double expected)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var matcher = new NotNullOrEmptyMatcher();
|
||||||
|
double result = matcher.IsMatch(data);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().Be(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NotNullOrEmptyMatcher_IsMatch_Json()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var matcher = new NotNullOrEmptyMatcher();
|
||||||
|
double result = matcher.IsMatch(new { x = "x" });
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().Be(1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,328 +1,377 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Moq;
|
using FluentAssertions;
|
||||||
using NFluent;
|
using Moq;
|
||||||
using WireMock.Matchers;
|
using NFluent;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers;
|
||||||
using WireMock.Models;
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.Types;
|
using WireMock.Models;
|
||||||
using WireMock.Util;
|
using WireMock.Types;
|
||||||
using Xunit;
|
using WireMock.Util;
|
||||||
|
using Xunit;
|
||||||
namespace WireMock.Net.Tests.RequestMatchers
|
|
||||||
{
|
namespace WireMock.Net.Tests.RequestMatchers
|
||||||
public class RequestMessageBodyMatcherTests
|
{
|
||||||
{
|
public class RequestMessageBodyMatcherTests
|
||||||
[Fact]
|
{
|
||||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatcher()
|
[Fact]
|
||||||
{
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatcher()
|
||||||
// Assign
|
{
|
||||||
var body = new BodyData
|
// Assign
|
||||||
{
|
var body = new BodyData
|
||||||
BodyAsString = "b",
|
{
|
||||||
DetectedBodyType = BodyType.String
|
BodyAsString = "b",
|
||||||
};
|
DetectedBodyType = BodyType.String
|
||||||
var stringMatcherMock = new Mock<IStringMatcher>();
|
};
|
||||||
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||||
|
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
||||||
// Act
|
|
||||||
var result = new RequestMatchResult();
|
// Act
|
||||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
// Assert
|
|
||||||
Check.That(score).IsEqualTo(0.5d);
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.5d);
|
||||||
// Verify
|
|
||||||
stringMatcherMock.Verify(m => m.GetPatterns(), Times.Never);
|
// Verify
|
||||||
stringMatcherMock.Verify(m => m.IsMatch("b"), Times.Once);
|
stringMatcherMock.Verify(m => m.GetPatterns(), Times.Never);
|
||||||
}
|
stringMatcherMock.Verify(m => m.IsMatch("b"), Times.Once);
|
||||||
|
}
|
||||||
[Fact]
|
|
||||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatchers()
|
[Fact]
|
||||||
{
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatchers()
|
||||||
// Assign
|
{
|
||||||
var body = new BodyData
|
// Assign
|
||||||
{
|
var body = new BodyData
|
||||||
BodyAsString = "b",
|
{
|
||||||
DetectedBodyType = BodyType.String
|
BodyAsString = "b",
|
||||||
};
|
DetectedBodyType = BodyType.String
|
||||||
var stringMatcherMock1 = new Mock<IStringMatcher>();
|
};
|
||||||
stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.2d);
|
var stringMatcherMock1 = new Mock<IStringMatcher>();
|
||||||
var stringMatcherMock2 = new Mock<IStringMatcher>();
|
stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.2d);
|
||||||
stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.8d);
|
var stringMatcherMock2 = new Mock<IStringMatcher>();
|
||||||
var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object };
|
stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.8d);
|
||||||
|
var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object };
|
||||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
var matcher = new RequestMessageBodyMatcher(matchers.Cast<IMatcher>().ToArray());
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(matchers.Cast<IMatcher>().ToArray());
|
||||||
// Act
|
|
||||||
var result = new RequestMatchResult();
|
// Act
|
||||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
// Assert
|
|
||||||
Check.That(score).IsEqualTo(0.8d);
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.8d);
|
||||||
// Verify
|
|
||||||
stringMatcherMock1.Verify(m => m.GetPatterns(), Times.Never);
|
// Verify
|
||||||
stringMatcherMock1.Verify(m => m.IsMatch("b"), Times.Once);
|
stringMatcherMock1.Verify(m => m.GetPatterns(), Times.Never);
|
||||||
stringMatcherMock2.Verify(m => m.GetPatterns(), Times.Never);
|
stringMatcherMock1.Verify(m => m.IsMatch("b"), Times.Once);
|
||||||
stringMatcherMock2.Verify(m => m.IsMatch("b"), Times.Once);
|
stringMatcherMock2.Verify(m => m.GetPatterns(), Times.Never);
|
||||||
}
|
stringMatcherMock2.Verify(m => m.IsMatch("b"), Times.Once);
|
||||||
|
}
|
||||||
[Fact]
|
|
||||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IStringMatcher()
|
[Fact]
|
||||||
{
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IStringMatcher()
|
||||||
// Assign
|
{
|
||||||
var body = new BodyData
|
// Assign
|
||||||
{
|
var body = new BodyData
|
||||||
BodyAsBytes = new byte[] { 1 },
|
{
|
||||||
DetectedBodyType = BodyType.Bytes
|
BodyAsBytes = new byte[] { 1 },
|
||||||
};
|
DetectedBodyType = BodyType.Bytes
|
||||||
var stringMatcherMock = new Mock<IStringMatcher>();
|
};
|
||||||
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||||
|
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
||||||
// Act
|
|
||||||
var result = new RequestMatchResult();
|
// Act
|
||||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
// Assert
|
|
||||||
Check.That(score).IsEqualTo(0.0d);
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.0d);
|
||||||
// Verify
|
|
||||||
stringMatcherMock.Verify(m => m.GetPatterns(), Times.Never);
|
// Verify
|
||||||
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Never);
|
stringMatcherMock.Verify(m => m.GetPatterns(), Times.Never);
|
||||||
}
|
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
[Fact]
|
|
||||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_IStringMatcher()
|
[Fact]
|
||||||
{
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_IStringMatcher()
|
||||||
// Assign
|
{
|
||||||
var body = new BodyData
|
// Assign
|
||||||
{
|
var body = new BodyData
|
||||||
BodyAsJson = new { value = 42 },
|
{
|
||||||
DetectedBodyType = BodyType.Json
|
BodyAsJson = new { value = 42 },
|
||||||
};
|
DetectedBodyType = BodyType.Json
|
||||||
var stringMatcherMock = new Mock<IStringMatcher>();
|
};
|
||||||
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||||
|
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
||||||
// Act
|
|
||||||
var result = new RequestMatchResult();
|
// Act
|
||||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
// Assert
|
|
||||||
Check.That(score).IsEqualTo(0.5d);
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.5d);
|
||||||
// Verify
|
|
||||||
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Once);
|
// Verify
|
||||||
}
|
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
[Fact]
|
|
||||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_and_BodyAsString_IStringMatcher()
|
[Fact]
|
||||||
{
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_and_BodyAsString_IStringMatcher()
|
||||||
// Assign
|
{
|
||||||
var body = new BodyData
|
// Assign
|
||||||
{
|
var body = new BodyData
|
||||||
BodyAsJson = new { value = 42 },
|
{
|
||||||
BodyAsString = "orig",
|
BodyAsJson = new { value = 42 },
|
||||||
DetectedBodyType = BodyType.Json
|
BodyAsString = "orig",
|
||||||
};
|
DetectedBodyType = BodyType.Json
|
||||||
var stringMatcherMock = new Mock<IStringMatcher>();
|
};
|
||||||
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||||
|
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object);
|
||||||
// Act
|
|
||||||
var result = new RequestMatchResult();
|
// Act
|
||||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
// Assert
|
|
||||||
Check.That(score).IsEqualTo(0.5d);
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.5d);
|
||||||
// Verify
|
|
||||||
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Once);
|
// Verify
|
||||||
}
|
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
[Fact]
|
|
||||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_IObjectMatcher()
|
[Fact]
|
||||||
{
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_IObjectMatcher()
|
||||||
// Assign
|
{
|
||||||
var body = new BodyData
|
// Assign
|
||||||
{
|
var body = new BodyData
|
||||||
BodyAsJson = 42,
|
{
|
||||||
DetectedBodyType = BodyType.Json
|
BodyAsJson = 42,
|
||||||
};
|
DetectedBodyType = BodyType.Json
|
||||||
var objectMatcherMock = new Mock<IObjectMatcher>();
|
};
|
||||||
objectMatcherMock.Setup(m => m.IsMatch(It.IsAny<object>())).Returns(0.5d);
|
var objectMatcherMock = new Mock<IObjectMatcher>();
|
||||||
|
objectMatcherMock.Setup(m => m.IsMatch(It.IsAny<object>())).Returns(0.5d);
|
||||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
var matcher = new RequestMessageBodyMatcher(objectMatcherMock.Object);
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(objectMatcherMock.Object);
|
||||||
// Act
|
|
||||||
var result = new RequestMatchResult();
|
// Act
|
||||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
// Assert
|
|
||||||
Check.That(score).IsEqualTo(0.5d);
|
// Assert
|
||||||
|
Check.That(score).IsEqualTo(0.5d);
|
||||||
// Verify
|
|
||||||
objectMatcherMock.Verify(m => m.IsMatch(42), Times.Once);
|
// Verify
|
||||||
}
|
objectMatcherMock.Verify(m => m.IsMatch(42), Times.Once);
|
||||||
|
}
|
||||||
[Fact]
|
|
||||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_CSharpCodeMatcher()
|
[Fact]
|
||||||
{
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_CSharpCodeMatcher()
|
||||||
// Assign
|
{
|
||||||
var body = new BodyData
|
// Assign
|
||||||
{
|
var body = new BodyData
|
||||||
BodyAsJson = new { value = 42 },
|
{
|
||||||
DetectedBodyType = BodyType.Json
|
BodyAsJson = new { value = 42 },
|
||||||
};
|
DetectedBodyType = BodyType.Json
|
||||||
|
};
|
||||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
var matcher = new RequestMessageBodyMatcher(new CSharpCodeMatcher(MatchBehaviour.AcceptOnMatch, "return it.value == 42;"));
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(new CSharpCodeMatcher(MatchBehaviour.AcceptOnMatch, "return it.value == 42;"));
|
||||||
// Act
|
|
||||||
var result = new RequestMatchResult();
|
// Act
|
||||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
// Assert
|
|
||||||
Check.That(score).IsEqualTo(1.0d);
|
// Assert
|
||||||
}
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
|
}
|
||||||
[Theory]
|
|
||||||
[InlineData(new byte[] { 1 })]
|
[Theory]
|
||||||
[InlineData(new byte[] { 48 })]
|
[InlineData(null, 0.0)]
|
||||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IObjectMatcher(byte[] bytes)
|
[InlineData(new byte[0], 0.0)]
|
||||||
{
|
[InlineData(new byte[] { 48 }, 1.0)]
|
||||||
// Assign
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_NotNullOrEmptyObjectMatcher(byte[] bytes, double expected)
|
||||||
var body = new BodyData
|
{
|
||||||
{
|
// Assign
|
||||||
BodyAsBytes = bytes,
|
var body = new BodyData
|
||||||
DetectedBodyType = BodyType.Bytes
|
{
|
||||||
};
|
BodyAsBytes = bytes,
|
||||||
var objectMatcherMock = new Mock<IObjectMatcher>();
|
DetectedBodyType = BodyType.Bytes
|
||||||
objectMatcherMock.Setup(m => m.IsMatch(It.IsAny<object>())).Returns(0.5d);
|
};
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(new NotNullOrEmptyMatcher());
|
||||||
var matcher = new RequestMessageBodyMatcher(objectMatcherMock.Object);
|
|
||||||
|
// Act
|
||||||
// Act
|
var result = new RequestMatchResult();
|
||||||
var result = new RequestMatchResult();
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
|
||||||
|
// Assert
|
||||||
// Assert
|
score.Should().Be(expected);
|
||||||
Check.That(score).IsEqualTo(0.5d);
|
}
|
||||||
|
|
||||||
// Verify
|
[Theory]
|
||||||
objectMatcherMock.Verify(m => m.IsMatch(It.IsAny<byte[]>()), Times.Once);
|
[InlineData(null, 0.0)]
|
||||||
}
|
[InlineData("", 0.0)]
|
||||||
|
[InlineData("x", 1.0)]
|
||||||
[Theory]
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_NotNullOrEmptyObjectMatcher(string data, double expected)
|
||||||
[MemberData(nameof(MatchingScoreData))]
|
{
|
||||||
public async Task RequestMessageBodyMatcher_GetMatchingScore_Funcs_Matching(object body, RequestMessageBodyMatcher matcher, bool shouldMatch)
|
// Assign
|
||||||
{
|
var body = new BodyData
|
||||||
// assign
|
{
|
||||||
BodyData bodyData;
|
BodyAsString = data,
|
||||||
if (body is byte[] b)
|
DetectedBodyType = BodyType.String
|
||||||
{
|
};
|
||||||
var bodyParserSettings = new BodyParserSettings
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
{
|
|
||||||
Stream = new MemoryStream(b),
|
var matcher = new RequestMessageBodyMatcher(new NotNullOrEmptyMatcher());
|
||||||
ContentType = null,
|
|
||||||
DeserializeJson = true
|
// Act
|
||||||
};
|
var result = new RequestMatchResult();
|
||||||
bodyData = await BodyParser.Parse(bodyParserSettings);
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
}
|
|
||||||
else if (body is string s)
|
// Assert
|
||||||
{
|
score.Should().Be(expected);
|
||||||
var bodyParserSettings = new BodyParserSettings
|
}
|
||||||
{
|
|
||||||
Stream = new MemoryStream(Encoding.UTF8.GetBytes(s)),
|
[Theory]
|
||||||
ContentType = null,
|
[InlineData(new byte[] { 1 })]
|
||||||
DeserializeJson = true
|
[InlineData(new byte[] { 48 })]
|
||||||
};
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_IObjectMatcher(byte[] bytes)
|
||||||
bodyData = await BodyParser.Parse(bodyParserSettings);
|
{
|
||||||
}
|
// Assign
|
||||||
else
|
var body = new BodyData
|
||||||
{
|
{
|
||||||
throw new Exception();
|
BodyAsBytes = bytes,
|
||||||
}
|
DetectedBodyType = BodyType.Bytes
|
||||||
|
};
|
||||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", bodyData);
|
var objectMatcherMock = new Mock<IObjectMatcher>();
|
||||||
|
objectMatcherMock.Setup(m => m.IsMatch(It.IsAny<object>())).Returns(0.5d);
|
||||||
// act
|
|
||||||
var result = new RequestMatchResult();
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
var score = matcher.GetMatchingScore(requestMessage, result);
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(objectMatcherMock.Object);
|
||||||
// assert
|
|
||||||
Check.That(score).IsEqualTo(shouldMatch ? 1d : 0d);
|
// Act
|
||||||
}
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
public static TheoryData<object, RequestMessageBodyMatcher, bool> MatchingScoreData
|
|
||||||
{
|
// Assert
|
||||||
get
|
Check.That(score).IsEqualTo(0.5d);
|
||||||
{
|
|
||||||
var json = "{'a':'b'}";
|
// Verify
|
||||||
var str = "HelloWorld";
|
objectMatcherMock.Verify(m => m.IsMatch(It.IsAny<byte[]>()), Times.Once);
|
||||||
var bytes = new byte[] { 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00 };
|
}
|
||||||
|
|
||||||
return new TheoryData<object, RequestMessageBodyMatcher, bool>
|
[Theory]
|
||||||
{
|
[MemberData(nameof(MatchingScoreData))]
|
||||||
// JSON match +++
|
public async Task RequestMessageBodyMatcher_GetMatchingScore_Funcs_Matching(object body, RequestMessageBodyMatcher matcher, bool shouldMatch)
|
||||||
{json, new RequestMessageBodyMatcher((object o) => ((dynamic) o).a == "b"), true},
|
{
|
||||||
{json, new RequestMessageBodyMatcher((string s) => s == json), true},
|
// assign
|
||||||
{json, new RequestMessageBodyMatcher((byte[] b) => b.SequenceEqual(Encoding.UTF8.GetBytes(json))), true},
|
BodyData bodyData;
|
||||||
|
if (body is byte[] b)
|
||||||
// JSON no match ---
|
{
|
||||||
{json, new RequestMessageBodyMatcher((object o) => false), false},
|
var bodyParserSettings = new BodyParserSettings
|
||||||
{json, new RequestMessageBodyMatcher((string s) => false), false},
|
{
|
||||||
{json, new RequestMessageBodyMatcher((byte[] b) => false), false},
|
Stream = new MemoryStream(b),
|
||||||
{json, new RequestMessageBodyMatcher(), false },
|
ContentType = null,
|
||||||
|
DeserializeJson = true
|
||||||
// string match +++
|
};
|
||||||
{str, new RequestMessageBodyMatcher((object o) => o == null), true},
|
bodyData = await BodyParser.Parse(bodyParserSettings);
|
||||||
{str, new RequestMessageBodyMatcher((string s) => s == str), true},
|
}
|
||||||
{str, new RequestMessageBodyMatcher((byte[] b) => b.SequenceEqual(Encoding.UTF8.GetBytes(str))), true},
|
else if (body is string s)
|
||||||
|
{
|
||||||
// string no match ---
|
var bodyParserSettings = new BodyParserSettings
|
||||||
{str, new RequestMessageBodyMatcher((object o) => false), false},
|
{
|
||||||
{str, new RequestMessageBodyMatcher((string s) => false), false},
|
Stream = new MemoryStream(Encoding.UTF8.GetBytes(s)),
|
||||||
{str, new RequestMessageBodyMatcher((byte[] b) => false), false},
|
ContentType = null,
|
||||||
{str, new RequestMessageBodyMatcher(), false },
|
DeserializeJson = true
|
||||||
|
};
|
||||||
// binary match +++
|
bodyData = await BodyParser.Parse(bodyParserSettings);
|
||||||
{bytes, new RequestMessageBodyMatcher((object o) => o == null), true},
|
}
|
||||||
{bytes, new RequestMessageBodyMatcher((string s) => s == null), true},
|
else
|
||||||
{bytes, new RequestMessageBodyMatcher((byte[] b) => b.SequenceEqual(bytes)), true},
|
{
|
||||||
|
throw new Exception();
|
||||||
// binary no match ---
|
}
|
||||||
{bytes, new RequestMessageBodyMatcher((object o) => false), false},
|
|
||||||
{bytes, new RequestMessageBodyMatcher((string s) => false), false},
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", bodyData);
|
||||||
{bytes, new RequestMessageBodyMatcher((byte[] b) => false), false},
|
|
||||||
{bytes, new RequestMessageBodyMatcher(), false },
|
// act
|
||||||
};
|
var result = new RequestMatchResult();
|
||||||
}
|
var score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
}
|
|
||||||
}
|
// assert
|
||||||
}
|
Check.That(score).IsEqualTo(shouldMatch ? 1d : 0d);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TheoryData<object, RequestMessageBodyMatcher, bool> MatchingScoreData
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var json = "{'a':'b'}";
|
||||||
|
var str = "HelloWorld";
|
||||||
|
var bytes = new byte[] { 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00 };
|
||||||
|
|
||||||
|
return new TheoryData<object, RequestMessageBodyMatcher, bool>
|
||||||
|
{
|
||||||
|
// JSON match +++
|
||||||
|
{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},
|
||||||
|
|
||||||
|
// 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},
|
||||||
|
{str, new RequestMessageBodyMatcher((byte[] b) => b.SequenceEqual(Encoding.UTF8.GetBytes(str))), true},
|
||||||
|
|
||||||
|
// string no match ---
|
||||||
|
{str, new RequestMessageBodyMatcher((object o) => false), false},
|
||||||
|
{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},
|
||||||
|
{bytes, new RequestMessageBodyMatcher((byte[] b) => b.SequenceEqual(bytes)), true},
|
||||||
|
|
||||||
|
// binary no match ---
|
||||||
|
{bytes, new RequestMessageBodyMatcher((object o) => false), false},
|
||||||
|
{bytes, new RequestMessageBodyMatcher((string s) => false), false},
|
||||||
|
{bytes, new RequestMessageBodyMatcher((byte[] b) => false), false},
|
||||||
|
{bytes, new RequestMessageBodyMatcher(), false },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,276 +1,276 @@
|
|||||||
using System;
|
using System;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Serialization;
|
using WireMock.Serialization;
|
||||||
using WireMock.Settings;
|
using WireMock.Settings;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests.Serialization
|
namespace WireMock.Net.Tests.Serialization
|
||||||
{
|
{
|
||||||
public class MatcherModelMapperTests
|
public class MatcherModelMapperTests
|
||||||
{
|
{
|
||||||
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
|
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
|
||||||
|
|
||||||
private readonly MatcherMapper _sut;
|
private readonly MatcherMapper _sut;
|
||||||
|
|
||||||
public MatcherModelMapperTests()
|
public MatcherModelMapperTests()
|
||||||
{
|
{
|
||||||
_sut = new MatcherMapper(_settings);
|
_sut = new MatcherMapper(_settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_CSharpCodeMatcher()
|
public void MatcherModelMapper_Map_CSharpCodeMatcher()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "CSharpCodeMatcher",
|
Name = "CSharpCodeMatcher",
|
||||||
Patterns = new[] { "return it == \"x\";" }
|
Patterns = new[] { "return it == \"x\";" }
|
||||||
};
|
};
|
||||||
var sut = new MatcherMapper(new WireMockServerSettings { AllowCSharpCodeMatcher = true });
|
var sut = new MatcherMapper(new WireMockServerSettings { AllowCSharpCodeMatcher = true });
|
||||||
|
|
||||||
// Act 1
|
// Act 1
|
||||||
var matcher1 = (ICSharpCodeMatcher)sut.Map(model);
|
var matcher1 = (ICSharpCodeMatcher)sut.Map(model);
|
||||||
|
|
||||||
// Assert 1
|
// Assert 1
|
||||||
matcher1.Should().NotBeNull();
|
matcher1.Should().NotBeNull();
|
||||||
matcher1.IsMatch("x").Should().Be(1.0d);
|
matcher1.IsMatch("x").Should().Be(1.0d);
|
||||||
|
|
||||||
// Act 2
|
// Act 2
|
||||||
var matcher2 = (ICSharpCodeMatcher)sut.Map(model);
|
var matcher2 = (ICSharpCodeMatcher)sut.Map(model);
|
||||||
|
|
||||||
// Assert 2
|
// Assert 2
|
||||||
matcher2.Should().NotBeNull();
|
matcher2.Should().NotBeNull();
|
||||||
matcher2.IsMatch("x").Should().Be(1.0d);
|
matcher2.IsMatch("x").Should().Be(1.0d);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_CSharpCodeMatcher_NotAllowed_ThrowsException()
|
public void MatcherModelMapper_Map_CSharpCodeMatcher_NotAllowed_ThrowsException()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "CSharpCodeMatcher",
|
Name = "CSharpCodeMatcher",
|
||||||
Patterns = new[] { "x" }
|
Patterns = new[] { "x" }
|
||||||
};
|
};
|
||||||
var sut = new MatcherMapper(new WireMockServerSettings { AllowCSharpCodeMatcher = false });
|
var sut = new MatcherMapper(new WireMockServerSettings { AllowCSharpCodeMatcher = false });
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
Action action = () => sut.Map(model);
|
Action action = () => sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
action.Should().Throw<NotSupportedException>();
|
action.Should().Throw<NotSupportedException>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_Null()
|
public void MatcherModelMapper_Map_Null()
|
||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
IMatcher matcher = _sut.Map((MatcherModel)null);
|
IMatcher matcher = _sut.Map((MatcherModel)null);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(matcher).IsNull();
|
Check.That(matcher).IsNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_ExactMatcher_Pattern()
|
public void MatcherModelMapper_Map_ExactMatcher_Pattern()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "ExactMatcher",
|
Name = "ExactMatcher",
|
||||||
Patterns = new[] { "x" }
|
Patterns = new[] { "x" }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var matcher = (ExactMatcher)_sut.Map(model);
|
var matcher = (ExactMatcher)_sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
matcher.GetPatterns().Should().ContainSingle("x");
|
matcher.GetPatterns().Should().ContainSingle("x");
|
||||||
matcher.ThrowException.Should().BeFalse();
|
matcher.ThrowException.Should().BeFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_ExactMatcher_Patterns()
|
public void MatcherModelMapper_Map_ExactMatcher_Patterns()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "ExactMatcher",
|
Name = "ExactMatcher",
|
||||||
Patterns = new[] { "x", "y" }
|
Patterns = new[] { "x", "y" }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var matcher = (ExactMatcher)_sut.Map(model);
|
var matcher = (ExactMatcher)_sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(nameof(LinqMatcher))]
|
[InlineData(nameof(LinqMatcher))]
|
||||||
[InlineData(nameof(ExactMatcher))]
|
[InlineData(nameof(ExactMatcher))]
|
||||||
[InlineData(nameof(ExactObjectMatcher))]
|
[InlineData(nameof(ExactObjectMatcher))]
|
||||||
[InlineData(nameof(RegexMatcher))]
|
[InlineData(nameof(RegexMatcher))]
|
||||||
[InlineData(nameof(JsonMatcher))]
|
[InlineData(nameof(JsonMatcher))]
|
||||||
[InlineData(nameof(JsonPathMatcher))]
|
[InlineData(nameof(JsonPathMatcher))]
|
||||||
[InlineData(nameof(JmesPathMatcher))]
|
[InlineData(nameof(JmesPathMatcher))]
|
||||||
[InlineData(nameof(XPathMatcher))]
|
[InlineData(nameof(XPathMatcher))]
|
||||||
[InlineData(nameof(WildcardMatcher))]
|
[InlineData(nameof(WildcardMatcher))]
|
||||||
[InlineData(nameof(ContentTypeMatcher))]
|
[InlineData(nameof(ContentTypeMatcher))]
|
||||||
[InlineData(nameof(SimMetricsMatcher))]
|
[InlineData(nameof(SimMetricsMatcher))]
|
||||||
public void MatcherModelMapper_Map_ThrowExceptionWhenMatcherFails_True(string name)
|
public void MatcherModelMapper_Map_ThrowExceptionWhenMatcherFails_True(string name)
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var settings = new WireMockServerSettings
|
var settings = new WireMockServerSettings
|
||||||
{
|
{
|
||||||
ThrowExceptionWhenMatcherFails = true
|
ThrowExceptionWhenMatcherFails = true
|
||||||
};
|
};
|
||||||
var sut = new MatcherMapper(settings);
|
var sut = new MatcherMapper(settings);
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = name,
|
Name = name,
|
||||||
Patterns = new[] { "" }
|
Patterns = new[] { "" }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var matcher = sut.Map(model);
|
var matcher = sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
matcher.ThrowException.Should().BeTrue();
|
matcher.ThrowException.Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_ExactObjectMatcher_ValidBase64StringPattern()
|
public void MatcherModelMapper_Map_ExactObjectMatcher_ValidBase64StringPattern()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "ExactObjectMatcher",
|
Name = "ExactObjectMatcher",
|
||||||
Patterns = new object[] { "c3RlZg==" }
|
Patterns = new object[] { "c3RlZg==" }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var matcher = (ExactObjectMatcher)_sut.Map(model);
|
var matcher = (ExactObjectMatcher)_sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(matcher.ValueAsBytes).ContainsExactly(new byte[] { 115, 116, 101, 102 });
|
Check.That(matcher.ValueAsBytes).ContainsExactly(new byte[] { 115, 116, 101, 102 });
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_ExactObjectMatcher_InvalidBase64StringPattern()
|
public void MatcherModelMapper_Map_ExactObjectMatcher_InvalidBase64StringPattern()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "ExactObjectMatcher",
|
Name = "ExactObjectMatcher",
|
||||||
Patterns = new object[] { "_" }
|
Patterns = new object[] { "_" }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Check.ThatCode(() => _sut.Map(model)).Throws<ArgumentException>();
|
Check.ThatCode(() => _sut.Map(model)).Throws<ArgumentException>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_RegexMatcher()
|
public void MatcherModelMapper_Map_RegexMatcher()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "RegexMatcher",
|
Name = "RegexMatcher",
|
||||||
Patterns = new[] { "x", "y" },
|
Patterns = new[] { "x", "y" },
|
||||||
IgnoreCase = true
|
IgnoreCase = true
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var matcher = (RegexMatcher)_sut.Map(model);
|
var matcher = (RegexMatcher)_sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
||||||
Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
|
Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_WildcardMatcher()
|
public void MatcherModelMapper_Map_WildcardMatcher()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "WildcardMatcher",
|
Name = "WildcardMatcher",
|
||||||
Patterns = new[] { "x", "y" },
|
Patterns = new[] { "x", "y" },
|
||||||
IgnoreCase = true
|
IgnoreCase = true
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var matcher = (WildcardMatcher)_sut.Map(model);
|
var matcher = (WildcardMatcher)_sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
||||||
Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
|
Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_SimMetricsMatcher()
|
public void MatcherModelMapper_Map_SimMetricsMatcher()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "SimMetricsMatcher",
|
Name = "SimMetricsMatcher",
|
||||||
Pattern = "x"
|
Pattern = "x"
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var matcher = (SimMetricsMatcher)_sut.Map(model);
|
var matcher = (SimMetricsMatcher)_sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(matcher.GetPatterns()).ContainsExactly("x");
|
Check.That(matcher.GetPatterns()).ContainsExactly("x");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_SimMetricsMatcher_BlockDistance()
|
public void MatcherModelMapper_Map_SimMetricsMatcher_BlockDistance()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "SimMetricsMatcher.BlockDistance",
|
Name = "SimMetricsMatcher.BlockDistance",
|
||||||
Pattern = "x"
|
Pattern = "x"
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var matcher = (SimMetricsMatcher)_sut.Map(model);
|
var matcher = (SimMetricsMatcher)_sut.Map(model);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(matcher.GetPatterns()).ContainsExactly("x");
|
Check.That(matcher.GetPatterns()).ContainsExactly("x");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_SimMetricsMatcher_Throws1()
|
public void MatcherModelMapper_Map_SimMetricsMatcher_Throws1()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "error",
|
Name = "error",
|
||||||
Pattern = "x"
|
Pattern = "x"
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
|
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatcherModelMapper_Map_SimMetricsMatcher_Throws2()
|
public void MatcherModelMapper_Map_SimMetricsMatcher_Throws2()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var model = new MatcherModel
|
var model = new MatcherModel
|
||||||
{
|
{
|
||||||
Name = "SimMetricsMatcher.error",
|
Name = "SimMetricsMatcher.error",
|
||||||
Pattern = "x"
|
Pattern = "x"
|
||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
|
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user