Implement PatternAsFile for StringMatcher (#651)

This commit is contained in:
Stef Heyenrath
2021-10-15 08:54:12 +02:00
committed by GitHub
parent a2a581c84b
commit 57cc616aa3
43 changed files with 817 additions and 491 deletions

View File

@@ -1,6 +1,6 @@
using NFluent;
using System;
using System.IO;
using NFluent;
using WireMock.Handlers;
using Xunit;
@@ -65,6 +65,13 @@ namespace WireMock.Net.Tests.Handlers
Check.ThatCode(() => _sut.ReadFile(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_ReadFileAsString_ThrowsArgumentNullException()
{
// Act
Check.ThatCode(() => _sut.ReadFileAsString(null)).Throws<ArgumentNullException>();
}
[Fact]
public void LocalFileSystemHandler_WriteFile_ThrowsArgumentNullException()
{

View File

@@ -1,4 +1,4 @@
using NFluent;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -83,7 +83,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new CSharpCodeMatcher("x");
// Act
string[] patterns = matcher.GetPatterns();
var patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("x");

View File

@@ -1,4 +1,5 @@
using NFluent;
using AnyOfTypes;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -49,7 +50,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new ContentTypeMatcher("x");
// Act
string[] patterns = matcher.GetPatterns();
var patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("x");

View File

@@ -1,4 +1,4 @@
using NFluent;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -26,7 +26,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new ExactMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
var patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");

View File

@@ -1,4 +1,4 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -27,7 +27,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JmesPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
var patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");

View File

@@ -1,4 +1,4 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -27,7 +27,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JsonPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
var patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");

View File

@@ -1,4 +1,4 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -103,7 +103,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new LinqMatcher("x");
// Act
string[] patterns = matcher.GetPatterns();
var patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("x");

View File

@@ -1,4 +1,4 @@
using NFluent;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -26,7 +26,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new RegexMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
var patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");

View File

@@ -1,4 +1,4 @@
using NFluent;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -26,7 +26,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new SimMetricsMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
var patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");

View File

@@ -1,35 +1,55 @@
using NFluent;
using AnyOfTypes;
using FluentAssertions;
using NFluent;
using WireMock.Matchers;
using WireMock.Models;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class WildcardMatcherTest
{
[Fact]
public void WildcardMatcher_IsMatch_With_StringMatcher_And_StringPattern()
{
// Arrange
var pattern = new StringPattern
{
Pattern = "*",
PatternAsFile = "pf"
};
// Act
var matcher = new WildcardMatcher(pattern);
// Assert
matcher.IsMatch("a").Should().Be(1.0d);
}
[Fact]
public void WildcardMatcher_IsMatch_Positive()
{
var tests = new[]
{
new {p = "*", i = ""},
new {p = "?", i = " "},
new {p = "*", i = "a"},
new {p = "*", i = "ab"},
new {p = "?", i = "a"},
new {p = "*?", i = "abc"},
new {p = "?*", i = "abc"},
new {p = "abc", i = "abc"},
new {p = "abc*", i = "abc"},
new {p = "abc*", i = "abcd"},
new {p = "*abc*", i = "abc"},
new {p = "*a*bc*", i = "abc"},
new {p = "*a*b?", i = "aXXXbc"}
new { p = "*", i = "" },
new { p = "?", i = " "},
new { p = "*", i = "a "},
new { p = "*", i = "ab" },
new { p = "?", i = "a" },
new { p = "*?", i = "abc" },
new { p = "?*", i = "abc" },
new { p = "abc", i = "abc" },
new { p = "abc*", i = "abc" },
new { p = "abc*", i = "abcd" },
new { p = "*abc*", i = "abc" },
new { p = "*a*bc*", i = "abc" },
new { p = "*a*b?", i = "aXXXbc" }
};
foreach (var test in tests)
{
var matcher = new WildcardMatcher(test.p);
Check.That(matcher.IsMatch(test.i)).IsEqualTo(1.0d);
matcher.IsMatch(test.i).Should().Be(1.0d, $"Pattern '{test.p}' with value '{test.i}' should be 1.0");
}
}
@@ -38,17 +58,17 @@ namespace WireMock.Net.Tests.Matchers
{
var tests = new[]
{
new {p = "*a", i = ""},
new {p = "a*", i = ""},
new {p = "?", i = ""},
new {p = "*b*", i = "a"},
new {p = "b*a", i = "ab"},
new {p = "??", i = "a"},
new {p = "*?", i = ""},
new {p = "??*", i = "a"},
new {p = "*abc", i = "abX"},
new {p = "*abc*", i = "Xbc"},
new {p = "*a*bc*", i = "ac"}
new { p = "*a", i = "" },
new { p = "a*", i = "" },
new { p = "?", i = "" },
new { p = "*b*", i = "a" },
new { p = "b*a", i = "ab" },
new { p = "??", i = "a" },
new { p = "*?", i = "" },
new { p = "??*", i = "a" },
new { p = "*abc", i = "abX" },
new { p = "*abc*", i = "Xbc" },
new { p = "*a*bc*", i = "ac" }
};
foreach (var test in tests)
@@ -78,10 +98,10 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new WildcardMatcher("x");
// Act
string[] patterns = matcher.GetPatterns();
var patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("x");
Check.That(patterns).ContainsExactly(new AnyOf<string, StringPattern>("x"));
}
[Fact]

View File

@@ -1,4 +1,4 @@
using NFluent;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -26,7 +26,7 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new XPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
var patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");

View File

@@ -1,6 +1,8 @@
using System;
using System;
using AnyOfTypes;
using FluentAssertions;
using WireMock.Matchers;
using WireMock.Models;
using WireMock.Plugin;
using Xunit;
@@ -16,7 +18,8 @@ namespace WireMock.Net.Tests.Plugin
public void Load_Valid()
{
// Act
var result = PluginLoader.Load<ICSharpCodeMatcher>(MatchBehaviour.AcceptOnMatch, "x");
AnyOf<string, StringPattern> pattern = "x";
var result = PluginLoader.Load<ICSharpCodeMatcher>(MatchBehaviour.AcceptOnMatch, pattern);
// Assert
result.Should().NotBeNull();

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -88,7 +88,7 @@ namespace WireMock.Net.Tests
}
[Fact]
public void Request_WithPathRegexMatcher()
public void Request_WithPathRegexMatcher_HasMatch()
{
// given
var spec = Request.Create().WithPath(new RegexMatcher("^/foo"));
@@ -102,7 +102,7 @@ namespace WireMock.Net.Tests
}
[Fact]
public void Request_WithPathRegex_false()
public void Request_WithPathRegexMatcher_HasNoMatch()
{
// given
var spec = Request.Create().WithPath("/foo");
@@ -115,6 +115,25 @@ namespace WireMock.Net.Tests
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
}
[Fact]
public void Request_WithPathRegexMatcher_WithPatternAsFile_HasMatch()
{
// Arrange
var pattern = new StringPattern
{
Pattern = "^/foo",
PatternAsFile = "c:\\x.txt"
};
var spec = Request.Create().WithPath(new RegexMatcher(pattern));
// when
var request = new RequestMessage(new UrlDetails("http://localhost/foo/bar"), "blabla", ClientIp);
// then
var requestMatchResult = new RequestMatchResult();
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
}
[Fact]
public void Should_specify_requests_matching_given_path_and_method_delete()
{

View File

@@ -1,9 +1,11 @@
using System;
using System;
using AnyOfTypes;
using FluentAssertions;
using Moq;
using NFluent;
using WireMock.Admin.Mappings;
using WireMock.Matchers;
using WireMock.Models;
using WireMock.Serialization;
using WireMock.Settings;
using Xunit;
@@ -60,7 +62,7 @@ namespace WireMock.Net.Tests.Serialization
// Assign
var matcherMock = new Mock<IStringMatcher>();
matcherMock.Setup(m => m.Name).Returns("test");
matcherMock.Setup(m => m.GetPatterns()).Returns(new[] { "p1", "p2" });
matcherMock.Setup(m => m.GetPatterns()).Returns(new AnyOf<string, StringPattern>[] { "p1", "p2" });
// Act
var model = _sut.Map(matcherMock.Object);
@@ -69,7 +71,30 @@ namespace WireMock.Net.Tests.Serialization
model.IgnoreCase.Should().BeNull();
model.Name.Should().Be("test");
model.Pattern.Should().BeNull();
model.Patterns.Should().Contain("p1", "p2");
model.Patterns.Should().HaveCount(2)
.And.Contain("p1")
.And.Contain("p2");
}
[Fact]
public void MatcherMapper_Map_IStringMatcher_With_PatternAsFile()
{
// Arrange
var pattern = new StringPattern { Pattern = "p", PatternAsFile = "pf" };
var matcherMock = new Mock<IStringMatcher>();
matcherMock.Setup(m => m.Name).Returns("test");
matcherMock.Setup(m => m.GetPatterns()).Returns(new AnyOf<string, StringPattern>[] { pattern });
// Act
var model = _sut.Map(matcherMock.Object);
// Assert
model.IgnoreCase.Should().BeNull();
model.Name.Should().Be("test");
model.Pattern.Should().Be("p");
model.Patterns.Should().BeNull();
model.PatternAsFile.Should().Be("pf");
}
[Fact]
@@ -301,5 +326,24 @@ namespace WireMock.Net.Tests.Serialization
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_StringPattern_With_PatternAsFile()
{
// Assign
var pattern = new StringPattern { Pattern = "{ \"AccountIds\": [ 1, 2, 3 ] }", PatternAsFile = "pf" };
var model = new MatcherModel
{
Name = "JsonPartialMatcher",
Pattern = pattern
};
// Act
var matcher = (JsonPartialMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(pattern);
}
}
}

View File

@@ -1,8 +1,12 @@
using System;
using AnyOfTypes;
using FluentAssertions;
using Moq;
using NFluent;
using WireMock.Admin.Mappings;
using WireMock.Handlers;
using WireMock.Matchers;
using WireMock.Models;
using WireMock.Serialization;
using WireMock.Settings;
using Xunit;
@@ -193,7 +197,7 @@ namespace WireMock.Net.Tests.Serialization
}
[Fact]
public void MatcherModelMapper_Map_WildcardMatcher()
public void MatcherModelMapper_Map_WildcardMatcher_IgnoreCase()
{
// Assign
var model = new MatcherModel
@@ -211,6 +215,40 @@ namespace WireMock.Net.Tests.Serialization
Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
}
[Fact]
public void MatcherModelMapper_Map_WildcardMatcher_With_PatternAsFile()
{
// Arrange
var file = "c:\\test.txt";
var fileContent = "c";
var stringPattern = new StringPattern
{
Pattern = fileContent,
PatternAsFile = file
};
var fileSystemHandleMock = new Mock<IFileSystemHandler>();
fileSystemHandleMock.Setup(f => f.ReadFileAsString(file)).Returns(fileContent);
var model = new MatcherModel
{
Name = "WildcardMatcher",
PatternAsFile = file
};
var settings = new WireMockServerSettings
{
FileSystemHandler = fileSystemHandleMock.Object
};
var sut = new MatcherMapper(settings);
// Act
var matcher = (WildcardMatcher)sut.Map(model);
// Assert
matcher.GetPatterns().Should().HaveCount(1).And.Contain(new AnyOf<string, StringPattern>(stringPattern));
matcher.IsMatch("c").Should().Be(1.0d);
}
[Fact]
public void MatcherModelMapper_Map_SimMetricsMatcher()
{

View File

@@ -60,6 +60,7 @@
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.12" />
<!--<PackageReference Include="StrongNamer" Version="0.0.8" />-->
<PackageReference Include="AnyOf" Version="0.2.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net452' or '$(TargetFramework)' == 'net461'">