mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-22 16:58:58 +02:00
Write logging in case a Matcher throws an exception (#986)
* ThrowException * ... * . * ... * b * fix test * ... * . * sonar * ft * . * fix tst
This commit is contained in:
@@ -15,8 +15,8 @@ namespace WireMock.Net.Tests.Serialization;
|
||||
public class CustomPathParamMatcher : IStringMatcher
|
||||
{
|
||||
public string Name => nameof(CustomPathParamMatcher);
|
||||
|
||||
public MatchBehaviour MatchBehaviour { get; }
|
||||
public bool ThrowException { get; }
|
||||
|
||||
private readonly string _path;
|
||||
private readonly string[] _pathParts;
|
||||
@@ -30,18 +30,16 @@ public class CustomPathParamMatcher : IStringMatcher
|
||||
MatchBehaviour matchBehaviour,
|
||||
string path,
|
||||
Dictionary<string, string> pathParams,
|
||||
bool throwException = false,
|
||||
MatchOperator matchOperator = MatchOperator.Or)
|
||||
{
|
||||
MatchBehaviour = matchBehaviour;
|
||||
ThrowException = throwException;
|
||||
_path = path;
|
||||
_pathParts = GetPathParts(path);
|
||||
_pathParams = pathParams.ToDictionary(x => x.Key, x => x.Value, StringComparer.OrdinalIgnoreCase);
|
||||
MatchOperator = matchOperator;
|
||||
}
|
||||
|
||||
public double IsMatch(string? input)
|
||||
public MatchResult IsMatch(string? input)
|
||||
{
|
||||
var inputParts = GetPathParts(input);
|
||||
if (inputParts.Length != _pathParts.Length)
|
||||
@@ -79,11 +77,6 @@ public class CustomPathParamMatcher : IStringMatcher
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (ThrowException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,14 +43,14 @@ public class MatcherModelMapperTests
|
||||
|
||||
// Assert 1
|
||||
matcher1.Should().NotBeNull();
|
||||
matcher1.IsMatch("x").Should().Be(1.0d);
|
||||
matcher1.IsMatch("x").Score.Should().Be(1.0d);
|
||||
|
||||
// Act 2
|
||||
var matcher2 = (ICSharpCodeMatcher)sut.Map(model)!;
|
||||
|
||||
// Assert 2
|
||||
matcher2.Should().NotBeNull();
|
||||
matcher2.IsMatch("x").Should().Be(1.0d);
|
||||
matcher2.IsMatch("x").Score.Should().Be(1.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -96,7 +96,6 @@ public class MatcherModelMapperTests
|
||||
|
||||
// Assert
|
||||
matcher.GetPatterns().Should().ContainSingle("x");
|
||||
matcher.ThrowException.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -136,7 +135,6 @@ public class MatcherModelMapperTests
|
||||
matcher.IgnoreCase.Should().BeFalse();
|
||||
matcher.Value.Should().Be(pattern);
|
||||
matcher.Regex.Should().BeFalse();
|
||||
matcher.ThrowException.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -159,43 +157,6 @@ public class MatcherModelMapperTests
|
||||
matcher.IgnoreCase.Should().BeFalse();
|
||||
matcher.Value.Should().Be(pattern);
|
||||
matcher.Regex.Should().BeTrue();
|
||||
matcher.ThrowException.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(nameof(LinqMatcher))]
|
||||
[InlineData(nameof(ExactMatcher))]
|
||||
[InlineData(nameof(ExactObjectMatcher))]
|
||||
[InlineData(nameof(RegexMatcher))]
|
||||
[InlineData(nameof(JsonMatcher))]
|
||||
[InlineData(nameof(JsonPartialMatcher))]
|
||||
[InlineData(nameof(JsonPartialWildcardMatcher))]
|
||||
[InlineData(nameof(JsonPathMatcher))]
|
||||
[InlineData(nameof(JmesPathMatcher))]
|
||||
[InlineData(nameof(XPathMatcher))]
|
||||
[InlineData(nameof(WildcardMatcher))]
|
||||
[InlineData(nameof(ContentTypeMatcher))]
|
||||
[InlineData(nameof(SimMetricsMatcher))]
|
||||
public void MatcherModelMapper_Map_ThrowExceptionWhenMatcherFails_True(string name)
|
||||
{
|
||||
// Assign
|
||||
var settings = new WireMockServerSettings
|
||||
{
|
||||
ThrowExceptionWhenMatcherFails = true
|
||||
};
|
||||
var sut = new MatcherMapper(settings);
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = name,
|
||||
Patterns = new[] { "" }
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = sut.Map(model)!;
|
||||
|
||||
// Assert
|
||||
matcher.Should().NotBeNull();
|
||||
matcher.ThrowException.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -249,7 +210,9 @@ public class MatcherModelMapperTests
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
||||
Check.That(matcher.IsMatch("X")).IsEqualTo(expected);
|
||||
|
||||
var result = matcher.IsMatch("X");
|
||||
result.Score.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -272,7 +235,9 @@ public class MatcherModelMapperTests
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
||||
Check.That(matcher.IsMatch("X")).IsEqualTo(expected);
|
||||
|
||||
var result = matcher.IsMatch("X");
|
||||
result.Score.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -306,7 +271,9 @@ public class MatcherModelMapperTests
|
||||
|
||||
// Assert
|
||||
matcher.GetPatterns().Should().HaveCount(1).And.Contain(new AnyOf<string, StringPattern>(stringPattern));
|
||||
matcher.IsMatch("c").Should().Be(1.0d);
|
||||
|
||||
var result = matcher.IsMatch("c");
|
||||
result.Score.Should().Be(MatchScores.Perfect);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -395,8 +362,7 @@ public class MatcherModelMapperTests
|
||||
return new CustomPathParamMatcher(
|
||||
matcherModel.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch,
|
||||
matcherParams.Path,
|
||||
matcherParams.PathParams,
|
||||
settings.ThrowExceptionWhenMatcherFails == true
|
||||
matcherParams.PathParams
|
||||
);
|
||||
};
|
||||
var sut = new MatcherMapper(settings);
|
||||
|
||||
Reference in New Issue
Block a user