mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-28 19:58:09 +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,10 +1,10 @@
|
|||||||
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).
|
||||||
|
|||||||
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
@@ -44,6 +44,9 @@ namespace WireMock.Serialization
|
|||||||
|
|
||||||
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,8 +1,9 @@
|
|||||||
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 FluentAssertions;
|
||||||
using Moq;
|
using Moq;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
@@ -209,6 +210,54 @@ namespace WireMock.Net.Tests.RequestMatchers
|
|||||||
Check.That(score).IsEqualTo(1.0d);
|
Check.That(score).IsEqualTo(1.0d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(null, 0.0)]
|
||||||
|
[InlineData(new byte[0], 0.0)]
|
||||||
|
[InlineData(new byte[] { 48 }, 1.0)]
|
||||||
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsBytes_NotNullOrEmptyObjectMatcher(byte[] bytes, double expected)
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsBytes = bytes,
|
||||||
|
DetectedBodyType = BodyType.Bytes
|
||||||
|
};
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(new NotNullOrEmptyMatcher());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
score.Should().Be(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(null, 0.0)]
|
||||||
|
[InlineData("", 0.0)]
|
||||||
|
[InlineData("x", 1.0)]
|
||||||
|
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_NotNullOrEmptyObjectMatcher(string data, double expected)
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
BodyAsString = data,
|
||||||
|
DetectedBodyType = BodyType.String
|
||||||
|
};
|
||||||
|
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||||
|
|
||||||
|
var matcher = new RequestMessageBodyMatcher(new NotNullOrEmptyMatcher());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = new RequestMatchResult();
|
||||||
|
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
score.Should().Be(expected);
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(new byte[] { 1 })]
|
[InlineData(new byte[] { 1 })]
|
||||||
[InlineData(new byte[] { 48 })]
|
[InlineData(new byte[] { 48 })]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
|
|||||||
Reference in New Issue
Block a user