Write logging in case a Matcher throws an exception (#986)

* ThrowException

* ...

* .

* ...

* b

* fix test

* ...

* .

* sonar

* ft

* .

* fix tst
This commit is contained in:
Stef Heyenrath
2023-08-21 20:07:46 +02:00
committed by GitHub
parent 09a302baf2
commit 93c87845c2
88 changed files with 1266 additions and 1244 deletions

View File

@@ -1,3 +1,4 @@
using FluentAssertions;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -11,12 +12,13 @@ public class CSharpCodeMatcherTests
{
// Assign
string input = "x";
// Act
var matcher = new CSharpCodeMatcher("return it == \"x\";");
// Act
var score = matcher.IsMatch(input).Score;
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(1.0d);
score.Should().Be(MatchScores.Perfect);
}
[Fact]
@@ -24,12 +26,13 @@ public class CSharpCodeMatcherTests
{
// Assign
string input = "y";
// Act
var matcher = new CSharpCodeMatcher("return it == \"x\";");
// Act
var score = matcher.IsMatch(input).Score;
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
score.Should().Be(MatchScores.Mismatch);
}
[Fact]
@@ -37,12 +40,13 @@ public class CSharpCodeMatcherTests
{
// Assign
string input = "x";
// Act
var matcher = new CSharpCodeMatcher(MatchBehaviour.RejectOnMatch, MatchOperator.Or, "return it == \"x\";");
// Act
var score = matcher.IsMatch(input).Score;
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
score.Should().Be(MatchScores.Mismatch);
}
[Fact]
@@ -55,12 +59,13 @@ public class CSharpCodeMatcherTests
Name = "Test"
};
// Act
var matcher = new CSharpCodeMatcher("return it.Id > 1 && it.Name == \"Test\";");
double match = matcher.IsMatch(input);
// Act
var score = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(1.0, match);
score.Should().Be(MatchScores.Perfect);
}
[Fact]

View File

@@ -1,3 +1,4 @@
using FluentAssertions;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -14,7 +15,12 @@ public class ContentTypeMatcherTests
public void ContentTypeMatcher_IsMatchWithIgnoreCaseFalse_Positive(string contentType)
{
var matcher = new ContentTypeMatcher("application/json");
Check.That(matcher.IsMatch(contentType)).IsEqualTo(1.0d);
// Act
var score = matcher.IsMatch(contentType).Score;
// Assert
score.Should().Be(MatchScores.Perfect);
}
[Theory]
@@ -26,7 +32,12 @@ public class ContentTypeMatcherTests
public void ContentTypeMatcher_IsMatchWithIgnoreCaseTrue_Positive(string contentType)
{
var matcher = new ContentTypeMatcher("application/json", true);
Check.That(matcher.IsMatch(contentType)).IsEqualTo(1.0d);
// Act
var score = matcher.IsMatch(contentType).Score;
// Assert
score.Should().Be(MatchScores.Perfect);
}
[Fact]

View File

@@ -39,7 +39,7 @@ public class ExactMatcherTests
var matcher = new ExactMatcher(true, "x");
// Act
double result = matcher.IsMatch("X");
double result = matcher.IsMatch("X").Score;
// Assert
Check.That(result).IsEqualTo(1.0);
@@ -52,7 +52,7 @@ public class ExactMatcherTests
var matcher = new ExactMatcher("x");
// Act
double result = matcher.IsMatch("x");
double result = matcher.IsMatch("x").Score;
// Assert
Check.That(result).IsEqualTo(1.0);
@@ -65,7 +65,7 @@ public class ExactMatcherTests
var matcher = new ExactMatcher("x");
// Act
double result = matcher.IsMatch("y");
double result = matcher.IsMatch("y").Score;
// Assert
Check.That(result).IsEqualTo(0.0);
@@ -78,7 +78,7 @@ public class ExactMatcherTests
var matcher = new ExactMatcher("x", "y");
// Act
double result = matcher.IsMatch("x");
double result = matcher.IsMatch("x").Score;
// Assert
Check.That(result).IsEqualTo(1.0);
@@ -91,7 +91,7 @@ public class ExactMatcherTests
var matcher = new ExactMatcher("x", "y");
// Act
double result = matcher.IsMatch("x");
double result = matcher.IsMatch("x").Score;
// Assert
Check.That(result).IsEqualTo(1.0);
@@ -104,10 +104,10 @@ public class ExactMatcherTests
public void ExactMatcher_IsMatch_WithMultiplePatterns_Average_ReturnsMatch(MatchOperator matchOperator, double score)
{
// Assign
var matcher = new ExactMatcher(MatchBehaviour.AcceptOnMatch, false, false, matchOperator, "x", "y");
var matcher = new ExactMatcher(MatchBehaviour.AcceptOnMatch, false, matchOperator, "x", "y");
// Act
double result = matcher.IsMatch("x");
double result = matcher.IsMatch("x").Score;
// Assert
Check.That(result).IsEqualTo(score);
@@ -120,7 +120,7 @@ public class ExactMatcherTests
var matcher = new ExactMatcher("cat");
// Act
double result = matcher.IsMatch("caR");
double result = matcher.IsMatch("caR").Score;
// Assert
Check.That(result).IsEqualTo(0.0);
@@ -130,10 +130,10 @@ public class ExactMatcherTests
public void ExactMatcher_IsMatch_SinglePattern_AcceptOnMatch()
{
// Assign
var matcher = new ExactMatcher(MatchBehaviour.AcceptOnMatch, false, false, MatchOperator.Or, "cat");
var matcher = new ExactMatcher(MatchBehaviour.AcceptOnMatch, false, MatchOperator.Or, "cat");
// Act
double result = matcher.IsMatch("cat");
double result = matcher.IsMatch("cat").Score;
// Assert
Check.That(result).IsEqualTo(1.0);
@@ -143,10 +143,10 @@ public class ExactMatcherTests
public void ExactMatcher_IsMatch_SinglePattern_RejectOnMatch()
{
// Assign
var matcher = new ExactMatcher(MatchBehaviour.RejectOnMatch, false, false, MatchOperator.Or, "cat");
var matcher = new ExactMatcher(MatchBehaviour.RejectOnMatch, false, MatchOperator.Or, "cat");
// Act
double result = matcher.IsMatch("cat");
double result = matcher.IsMatch("cat").Score;
// Assert
Check.That(result).IsEqualTo(0.0);

View File

@@ -14,7 +14,7 @@ public class ExactObjectMatcherTests
// Act
var matcher = new ExactObjectMatcher(obj);
string name = matcher.Name;
var name = matcher.Name;
// Assert
Check.That(name).Equals("ExactObjectMatcher");
@@ -28,10 +28,10 @@ public class ExactObjectMatcherTests
// Act
var matcher = new ExactObjectMatcher(new byte[] { 1, 2 });
double result = matcher.IsMatch(checkValue);
var score = matcher.IsMatch(checkValue).Score;
// Assert
Check.That(result).IsEqualTo(1.0);
Check.That(score).IsEqualTo(1.0);
}
[Fact]
@@ -42,10 +42,10 @@ public class ExactObjectMatcherTests
// Act
var matcher = new ExactObjectMatcher(obj);
double result = matcher.IsMatch(new { x = 500, s = "s" });
var score = matcher.IsMatch(new { x = 500, s = "s" }).Score;
// Assert
Check.That(result).IsEqualTo(1.0);
Check.That(score).IsEqualTo(1.0);
}
[Fact]
@@ -56,9 +56,9 @@ public class ExactObjectMatcherTests
// Act
var matcher = new ExactObjectMatcher(MatchBehaviour.RejectOnMatch, obj);
double result = matcher.IsMatch(new { x = 500, s = "s" });
var score = matcher.IsMatch(new { x = 500, s = "s" }).Score;
// Assert
Check.That(result).IsEqualTo(0.0);
Check.That(score).IsEqualTo(0.0);
}
}

View File

@@ -51,7 +51,7 @@ public class GraphQLMatcherTests
var result = matcher.IsMatch(input);
// Assert
result.Should().Be(MatchScores.Perfect);
result.Score.Should().Be(MatchScores.Perfect);
matcher.GetPatterns().Should().Contain(TestSchema);
}
@@ -71,7 +71,7 @@ public class GraphQLMatcherTests
var result = matcher.IsMatch(input);
// Assert
result.Should().Be(MatchScores.Perfect);
result.Score.Should().Be(MatchScores.Perfect);
matcher.GetPatterns().Should().Contain(TestSchema);
}
@@ -93,7 +93,7 @@ public class GraphQLMatcherTests
var result = matcher.IsMatch(input);
// Assert
result.Should().Be(MatchScores.Mismatch);
result.Score.Should().Be(MatchScores.Mismatch);
}
[Fact]
@@ -111,21 +111,22 @@ public class GraphQLMatcherTests
var result = matcher.IsMatch(input);
// Assert
result.Should().Be(MatchScores.Perfect);
result.Score.Should().Be(MatchScores.Perfect);
}
[Fact]
public void GraphQLMatcher_For_ValidSchema_And_IncorrectQueryWithError_WithThrowExceptionTrue_ThrowsException()
public void GraphQLMatcher_For_ValidSchema_And_IncorrectQueryWithError_WithThrowExceptionTrue_ReturnsError()
{
// Arrange
var input = "{\"query\":\"{\\r\\n studentsX {\\r\\n fullName\\r\\n X\\r\\n }\\r\\n}\"}";
// Act
var matcher = new GraphQLMatcher(TestSchema, MatchBehaviour.AcceptOnMatch, true);
Action action = () => matcher.IsMatch(input);
var matcher = new GraphQLMatcher(TestSchema);
var result = matcher.IsMatch(input);
// Assert
action.Should().Throw<Exception>();
result.Score.Should().Be(MatchScores.Mismatch);
result.Exception!.Message.Should().StartWith("Cannot query field 'studentsX' on type 'Query'");
}
[Fact]

View File

@@ -42,7 +42,7 @@ public class JmesPathMatcherTests
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch(bytes);
double match = matcher.IsMatch(bytes).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -56,7 +56,7 @@ public class JmesPathMatcherTests
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch(s);
double match = matcher.IsMatch(s).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -70,7 +70,7 @@ public class JmesPathMatcherTests
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch(o);
double match = matcher.IsMatch(o).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -83,7 +83,7 @@ public class JmesPathMatcherTests
var matcher = new JmesPathMatcher("xxx");
// Act
double match = matcher.IsMatch("");
double match = matcher.IsMatch("").Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -96,7 +96,7 @@ public class JmesPathMatcherTests
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch("x");
double match = matcher.IsMatch("x").Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -109,7 +109,7 @@ public class JmesPathMatcherTests
var matcher = new JmesPathMatcher("things.name == 'RequiredThing'");
// Act
double match = matcher.IsMatch(new { things = new { name = "RequiredThing" } });
double match = matcher.IsMatch(new { things = new { name = "RequiredThing" } }).Score;
// Assert
Check.That(match).IsEqualTo(1);
@@ -132,7 +132,7 @@ public class JmesPathMatcherTests
{ "Id", new JValue(1) },
{ "things", sub }
};
double match = matcher.IsMatch(jobject);
double match = matcher.IsMatch(jobject).Score;
// Assert
Check.That(match).IsEqualTo(1);
@@ -145,7 +145,7 @@ public class JmesPathMatcherTests
var matcher = new JmesPathMatcher("things.x == 'RequiredThing'");
// Act
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }"));
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }")).Score;
// Assert
Check.That(match).IsEqualTo(1);
@@ -155,10 +155,10 @@ public class JmesPathMatcherTests
public void JmesPathMatcher_IsMatch_RejectOnMatch()
{
// Assign
var matcher = new JmesPathMatcher(MatchBehaviour.RejectOnMatch, false, MatchOperator.Or, "things.x == 'RequiredThing'");
var matcher = new JmesPathMatcher(MatchBehaviour.RejectOnMatch, MatchOperator.Or, "things.x == 'RequiredThing'");
// Act
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }"));
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }")).Score;
// Assert
Check.That(match).IsEqualTo(0.0);

View File

@@ -82,29 +82,17 @@ public class JsonMatcherTests
}
[Fact]
public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch()
public void JsonMatcher_IsMatch_WithInvalidValue_Should_ReturnMismatch_And_Exception_ShouldBeSet()
{
// Assign
var matcher = new JsonMatcher("");
// Act
double match = matcher.IsMatch(new MemoryStream());
var result = matcher.IsMatch(new MemoryStream());
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch()
{
// Assign
var matcher = new JsonMatcher("", false, true);
// Act
Action action = () => matcher.IsMatch(new MemoryStream());
// Assert
action.Should().Throw<JsonException>();
result.Score.Should().Be(MatchScores.Mismatch);
result.Exception.Should().BeAssignableTo<JsonException>();
}
[Fact]
@@ -115,7 +103,7 @@ public class JsonMatcherTests
var matcher = new JsonMatcher("");
// Act
double match = matcher.IsMatch(bytes);
double match = matcher.IsMatch(bytes).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -129,7 +117,7 @@ public class JsonMatcherTests
var matcher = new JsonMatcher("");
// Act
double match = matcher.IsMatch(s);
double match = matcher.IsMatch(s).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -143,7 +131,7 @@ public class JsonMatcherTests
var matcher = new JsonMatcher("");
// Act
double match = matcher.IsMatch(o);
double match = matcher.IsMatch(o).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -161,7 +149,7 @@ public class JsonMatcherTests
"x",
"y"
};
double match = matcher.IsMatch(jArray);
double match = matcher.IsMatch(jArray).Score;
// Assert
Assert.Equal(1.0, match);
@@ -179,7 +167,7 @@ public class JsonMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -197,7 +185,7 @@ public class JsonMatcherTests
{ "Id", new JValue(1) },
{ "NaMe", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -211,7 +199,7 @@ public class JsonMatcherTests
// Act
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -225,7 +213,7 @@ public class JsonMatcherTests
// Act
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -243,7 +231,7 @@ public class JsonMatcherTests
"x",
"y"
};
double match = matcher.IsMatch(jArray);
double match = matcher.IsMatch(jArray).Score;
// Assert
Assert.Equal(1.0, match);
@@ -261,7 +249,7 @@ public class JsonMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -279,7 +267,7 @@ public class JsonMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -297,7 +285,7 @@ public class JsonMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(0.0, match);
@@ -314,7 +302,7 @@ public class JsonMatcherTests
{
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -331,7 +319,7 @@ public class JsonMatcherTests
{
{ "NormalEnum", new JValue(0) }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
match.Should().Be(1.0);
@@ -348,7 +336,7 @@ public class JsonMatcherTests
{
{ "EnumWithJsonConverter", new JValue("Type1") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
match.Should().Be(1.0);

View File

@@ -60,29 +60,17 @@ public class JsonPartialMatcherTests
}
[Fact]
public void JsonPartialMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch()
public void JsonPartialMatcher_IsMatch_WithInvalidValue_Should_ReturnMismatch_And_Exception_ShouldBeSet()
{
// Assign
var matcher = new JsonPartialMatcher("");
// Act
double match = matcher.IsMatch(new MemoryStream());
var result = matcher.IsMatch(new MemoryStream());
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPartialMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch()
{
// Assign
var matcher = new JsonPartialMatcher("", false, true);
// Act
Action action = () => matcher.IsMatch(new MemoryStream());
// Assert
action.Should().Throw<JsonException>();
result.Score.Should().Be(MatchScores.Mismatch);
result.Exception.Should().BeAssignableTo<JsonException>();
}
[Fact]
@@ -93,7 +81,7 @@ public class JsonPartialMatcherTests
var matcher = new JsonPartialMatcher("");
// Act
double match = matcher.IsMatch(bytes);
double match = matcher.IsMatch(bytes).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -107,7 +95,7 @@ public class JsonPartialMatcherTests
var matcher = new JsonPartialMatcher("");
// Act
double match = matcher.IsMatch(s);
double match = matcher.IsMatch(s).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -121,7 +109,7 @@ public class JsonPartialMatcherTests
var matcher = new JsonPartialMatcher("");
// Act
double match = matcher.IsMatch(o);
double match = matcher.IsMatch(o).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -139,7 +127,7 @@ public class JsonPartialMatcherTests
"x",
"y"
};
double match = matcher.IsMatch(jArray);
double match = matcher.IsMatch(jArray).Score;
// Assert
Assert.Equal(1.0, match);
@@ -157,7 +145,7 @@ public class JsonPartialMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -167,7 +155,7 @@ public class JsonPartialMatcherTests
public void JsonPartialMatcher_IsMatch_WithRegexTrue()
{
// Assign
var matcher = new JsonPartialMatcher(new { Id = "^\\d+$", Name = "Test" }, false, false, true);
var matcher = new JsonPartialMatcher(new { Id = "^\\d+$", Name = "Test" }, false, true);
// Act
var jObject = new JObject
@@ -175,7 +163,7 @@ public class JsonPartialMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -193,7 +181,7 @@ public class JsonPartialMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(0.0, match);
@@ -203,10 +191,11 @@ public class JsonPartialMatcherTests
public void JsonPartialMatcher_IsMatch_GuidAsString_UsingRegex()
{
var guid = new Guid("1111238e-b775-44a9-a263-95e570135c94");
var matcher = new JsonPartialMatcher(new {
var matcher = new JsonPartialMatcher(new
{
Id = 1,
Name = "^1111[a-fA-F0-9]{4}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"
}, false, false, true);
}, false, true);
// Act
var jObject = new JObject
@@ -214,7 +203,7 @@ public class JsonPartialMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue(guid) }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -232,7 +221,7 @@ public class JsonPartialMatcherTests
{ "Id", new JValue(1) },
{ "NaMe", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -246,7 +235,7 @@ public class JsonPartialMatcherTests
// Act
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -260,7 +249,7 @@ public class JsonPartialMatcherTests
// Act
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -278,7 +267,7 @@ public class JsonPartialMatcherTests
"x",
"y"
};
double match = matcher.IsMatch(jArray);
double match = matcher.IsMatch(jArray).Score;
// Assert
Assert.Equal(1.0, match);
@@ -296,7 +285,7 @@ public class JsonPartialMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -305,20 +294,20 @@ public class JsonPartialMatcherTests
[Fact]
public void JsonPartialMatcher_IsMatch_GuidAsString()
{
// Assign
var guid = Guid.NewGuid();
var matcher = new JsonPartialMatcher(new { Id = 1, Name = guid });
// Assign
var guid = Guid.NewGuid();
var matcher = new JsonPartialMatcher(new { Id = 1, Name = guid });
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue(guid.ToString()) }
};
double match = matcher.IsMatch(jObject);
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue(guid.ToString()) }
};
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
@@ -333,7 +322,7 @@ public class JsonPartialMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -351,7 +340,7 @@ public class JsonPartialMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(0.0, match);
@@ -368,7 +357,7 @@ public class JsonPartialMatcherTests
{
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -391,7 +380,7 @@ public class JsonPartialMatcherTests
var matcher = new JsonPartialMatcher(value);
// Act
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(1.0, match);
@@ -415,7 +404,7 @@ public class JsonPartialMatcherTests
var matcher = new JsonPartialMatcher(value);
// Act
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(0.0, match);
@@ -434,7 +423,7 @@ public class JsonPartialMatcherTests
var matcher = new JsonPartialMatcher(value);
// Act
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(1.0, match);
@@ -453,7 +442,7 @@ public class JsonPartialMatcherTests
var matcher = new JsonPartialMatcher(value);
// Act
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(0.0, match);

View File

@@ -60,29 +60,17 @@ public class JsonPartialWildcardMatcherTests
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch()
public void JsonPartialWildcardMatcher_IsMatch_WithInvalidValue_Should_ReturnMismatch_And_Exception_ShouldBeSet()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = matcher.IsMatch(new MemoryStream());
var result = matcher.IsMatch(new MemoryStream());
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("", false, true);
// Act
Action action = () => matcher.IsMatch(new MemoryStream());
// Assert
action.Should().Throw<JsonException>();
result.Score.Should().Be(MatchScores.Mismatch);
result.Exception.Should().BeAssignableTo<JsonException>();
}
[Fact]
@@ -93,7 +81,7 @@ public class JsonPartialWildcardMatcherTests
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = matcher.IsMatch(bytes);
double match = matcher.IsMatch(bytes).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -107,7 +95,7 @@ public class JsonPartialWildcardMatcherTests
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = matcher.IsMatch(s);
double match = matcher.IsMatch(s).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -121,7 +109,7 @@ public class JsonPartialWildcardMatcherTests
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = matcher.IsMatch(o);
double match = matcher.IsMatch(o).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -139,7 +127,7 @@ public class JsonPartialWildcardMatcherTests
"x",
"y"
};
double match = matcher.IsMatch(jArray);
double match = matcher.IsMatch(jArray).Score;
// Assert
Assert.Equal(1.0, match);
@@ -157,7 +145,7 @@ public class JsonPartialWildcardMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -175,7 +163,7 @@ public class JsonPartialWildcardMatcherTests
{ "Id", new JValue(1) },
{ "NaMe", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -189,7 +177,7 @@ public class JsonPartialWildcardMatcherTests
// Act
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -203,7 +191,7 @@ public class JsonPartialWildcardMatcherTests
// Act
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -221,7 +209,7 @@ public class JsonPartialWildcardMatcherTests
"x",
"y"
};
double match = matcher.IsMatch(jArray);
double match = matcher.IsMatch(jArray).Score;
// Assert
Assert.Equal(1.0, match);
@@ -239,7 +227,7 @@ public class JsonPartialWildcardMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -257,7 +245,7 @@ public class JsonPartialWildcardMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -275,7 +263,7 @@ public class JsonPartialWildcardMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(0.0, match);
@@ -292,7 +280,7 @@ public class JsonPartialWildcardMatcherTests
{
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
};
double match = matcher.IsMatch(jObject);
double match = matcher.IsMatch(jObject).Score;
// Assert
Assert.Equal(1.0, match);
@@ -315,7 +303,7 @@ public class JsonPartialWildcardMatcherTests
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(1.0, match);
@@ -330,7 +318,7 @@ public class JsonPartialWildcardMatcherTests
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
match.Should().Be(1.0);
@@ -354,7 +342,7 @@ public class JsonPartialWildcardMatcherTests
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(0.0, match);
@@ -373,7 +361,7 @@ public class JsonPartialWildcardMatcherTests
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(1.0, match);
@@ -392,7 +380,7 @@ public class JsonPartialWildcardMatcherTests
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(0.0, match);

View File

@@ -42,7 +42,7 @@ public class JsonPathMatcherTests
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch(bytes);
double match = matcher.IsMatch(bytes).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -56,7 +56,7 @@ public class JsonPathMatcherTests
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch(s);
double match = matcher.IsMatch(s).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -70,7 +70,7 @@ public class JsonPathMatcherTests
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch(o);
double match = matcher.IsMatch(o).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -83,7 +83,7 @@ public class JsonPathMatcherTests
var matcher = new JsonPathMatcher("xxx");
// Act
double match = matcher.IsMatch("");
double match = matcher.IsMatch("").Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -96,7 +96,7 @@ public class JsonPathMatcherTests
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch("x");
double match = matcher.IsMatch("x").Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -109,7 +109,7 @@ public class JsonPathMatcherTests
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
// Act
double match = matcher.IsMatch(new { Id = 1, Name = "Test" });
double match = matcher.IsMatch(new { Id = 1, Name = "Test" }).Score;
// Assert
Check.That(match).IsEqualTo(1);
@@ -122,7 +122,7 @@ public class JsonPathMatcherTests
var matcher = new JsonPathMatcher("$.things[?(@.name == 'x')]");
// Act
double match = matcher.IsMatch(new { things = new { name = "x" } });
double match = matcher.IsMatch(new { things = new { name = "x" } }).Score;
// Assert
Check.That(match).IsEqualTo(1);
@@ -136,7 +136,7 @@ public class JsonPathMatcherTests
var matcher = new JsonPathMatcher("$.things[?(@.name == 'x')]");
// Act
double match = matcher.IsMatch(json);
double match = matcher.IsMatch(json).Score;
// Assert
Check.That(match).IsEqualTo(1);
@@ -150,7 +150,7 @@ public class JsonPathMatcherTests
var matcher = new JsonPathMatcher("$.things[?(@.name == 'x')]");
// Act
double match = matcher.IsMatch(json);
double match = matcher.IsMatch(json).Score;
// Assert
Check.That(match).IsEqualTo(0);
@@ -169,7 +169,7 @@ public class JsonPathMatcherTests
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
double match = matcher.IsMatch(jobject).Score;
// Assert
Check.That(match).IsEqualTo(1);
@@ -182,7 +182,7 @@ public class JsonPathMatcherTests
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
// Act
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}"));
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}")).Score;
// Assert
Check.That(match).IsEqualTo(1);
@@ -192,10 +192,10 @@ public class JsonPathMatcherTests
public void JsonPathMatcher_IsMatch_RejectOnMatch()
{
// Arrange
var matcher = new JsonPathMatcher(MatchBehaviour.RejectOnMatch, false, MatchOperator.Or, "$..[?(@.Id == 1)]");
var matcher = new JsonPathMatcher(MatchBehaviour.RejectOnMatch, MatchOperator.Or, "$..[?(@.Id == 1)]");
// Act
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}"));
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}")).Score;
// Assert
Check.That(match).IsEqualTo(0.0);
@@ -206,7 +206,7 @@ public class JsonPathMatcherTests
{
// Arrange
var matcher = new JsonPathMatcher("$.arr[0].line1");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
@@ -215,18 +215,18 @@ public class JsonPathMatcherTests
""arr"": [{
""line1"": ""line1"",
}]
}"));
}")).Score;
// Assert
Check.That(match).IsEqualTo(1.0);
Check.That(match).IsEqualTo(1.0);
}
[Fact]
public void JsonPathMatcher_IsMatch_ObjectMatch()
{
// Arrange
var matcher = new JsonPathMatcher("$.test");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
@@ -237,10 +237,10 @@ public class JsonPathMatcherTests
""line1"": ""line1"",
}
]
}"));
}")).Score;
// Assert
Check.That(match).IsEqualTo(1.0);
Check.That(match).IsEqualTo(1.0);
}
[Fact]
@@ -248,7 +248,7 @@ public class JsonPathMatcherTests
{
// Arrange
var matcher = new JsonPathMatcher("$.test3");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
@@ -259,10 +259,10 @@ public class JsonPathMatcherTests
""line1"": ""line1"",
}
]
}"));
}")).Score;
// Assert
Check.That(match).IsEqualTo(0.0);
Check.That(match).IsEqualTo(0.0);
}
[Fact]
@@ -270,35 +270,35 @@ public class JsonPathMatcherTests
{
// Arrange
var matcher = new JsonPathMatcher("$arr[0].line1");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
""test"": ""test"",
""test2"": ""test2"",
""arr"": []
}"));
}")).Score;
// Assert
Check.That(match).IsEqualTo(0.0);
Check.That(match).IsEqualTo(0.0);
}
[Fact]
public void JsonPathMatcher_IsMatch_DoesntMatchNoObjectsInArray()
{
// Arrange
var matcher = new JsonPathMatcher("$arr[2].line1");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
""test"": ""test"",
""test2"": ""test2"",
""arr"": []
}"));
}")).Score;
// Assert
Check.That(match).IsEqualTo(0.0);
Check.That(match).IsEqualTo(0.0);
}
[Fact]
@@ -306,7 +306,7 @@ public class JsonPathMatcherTests
{
// Arrange
var matcher = new JsonPathMatcher("$.arr[0].sub[0].subline1");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
@@ -319,9 +319,9 @@ public class JsonPathMatcherTests
""subline1"":""subline1""
}]
}]
}"));
}")).Score;
// Assert
Check.That(match).IsEqualTo(1.0);
Check.That(match).IsEqualTo(1.0);
}
}
}

View File

@@ -1,3 +1,4 @@
using FluentAssertions;
using Newtonsoft.Json.Linq;
using NFluent;
using WireMock.Matchers;
@@ -17,7 +18,8 @@ public class LinqMatcherTests
var matcher = new LinqMatcher("DateTime.Parse(it) > \"2018-08-01 13:50:00\"");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(1.0d);
var score = matcher.IsMatch(input).Score;
score.Should().Be(MatchScores.Perfect);
}
[Fact]
@@ -30,7 +32,8 @@ public class LinqMatcherTests
var matcher = new LinqMatcher("DateTime.Parse(it) > \"2019-01-01 00:00:00\"");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
var score = matcher.IsMatch(input).Score;
score.Should().Be(MatchScores.Mismatch);
}
[Fact]
@@ -43,7 +46,8 @@ public class LinqMatcherTests
var matcher = new LinqMatcher(MatchBehaviour.RejectOnMatch, "DateTime.Parse(it) > \"2018-08-01 13:50:00\"");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
var score = matcher.IsMatch(input).Score;
score.Should().Be(MatchScores.Mismatch);
}
[Fact]
@@ -58,7 +62,7 @@ public class LinqMatcherTests
// Act
var matcher = new LinqMatcher("Id > 1 AND Name == \"Test\"");
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(1.0, match);
@@ -77,7 +81,7 @@ public class LinqMatcherTests
// Act
var matcher = new LinqMatcher("IntegerId > 1 AND LongId > 1 && Name == \"Test\"");
double match = matcher.IsMatch(input);
double match = matcher.IsMatch(input).Score;
// Assert
Assert.Equal(1.0, match);

View File

@@ -55,7 +55,7 @@ AAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC
// Assert
matcher.Name.Should().Be("MimePartMatcher");
result.Should().Be(MatchScores.Perfect);
result.Score.Should().Be(MatchScores.Perfect);
}
[Fact]
@@ -73,7 +73,7 @@ AAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC
var result = matcher.IsMatch(part);
// Assert
result.Should().Be(MatchScores.Perfect);
result.Score.Should().Be(MatchScores.Perfect);
}
[Fact]
@@ -93,7 +93,7 @@ AAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC
var result = matcher.IsMatch(part);
// Assert
result.Should().Be(MatchScores.Perfect);
result.Score.Should().Be(MatchScores.Perfect);
}
}
#endif

View File

@@ -12,7 +12,7 @@ public class NotNullOrEmptyMatcherTests
{
// Act
var matcher = new NotNullOrEmptyMatcher();
string name = matcher.Name;
var name = matcher.Name;
// Assert
Check.That(name).Equals("NotNullOrEmptyMatcher");
@@ -26,7 +26,7 @@ public class NotNullOrEmptyMatcherTests
{
// Act
var matcher = new NotNullOrEmptyMatcher();
double result = matcher.IsMatch(data);
var result = matcher.IsMatch(data).Score;
// Assert
result.Should().Be(expected);
@@ -40,7 +40,7 @@ public class NotNullOrEmptyMatcherTests
{
// Act
var matcher = new NotNullOrEmptyMatcher();
double result = matcher.IsMatch(@string);
var result = matcher.IsMatch(@string).Score;
// Assert
result.Should().Be(expected);
@@ -54,7 +54,7 @@ public class NotNullOrEmptyMatcherTests
{
// Act
var matcher = new NotNullOrEmptyMatcher();
double result = matcher.IsMatch((object)@string);
var result = matcher.IsMatch((object)@string).Score;
// Assert
result.Should().Be(expected);
@@ -65,7 +65,7 @@ public class NotNullOrEmptyMatcherTests
{
// Act
var matcher = new NotNullOrEmptyMatcher();
double result = matcher.IsMatch(new { x = "x" });
var result = matcher.IsMatch(new { x = "x" }).Score;
// Assert
result.Should().Be(1.0);

View File

@@ -53,7 +53,7 @@ public class RegexMatcherTests
var matcher = new RegexMatcher("H.*o");
// Act
double result = matcher.IsMatch("Hello world!");
double result = matcher.IsMatch("Hello world!").Score;
// Assert
Check.That(result).IsEqualTo(1.0d);
@@ -66,7 +66,7 @@ public class RegexMatcherTests
var matcher = new RegexMatcher("H.*o");
// Act
double result = matcher.IsMatch(null);
double result = matcher.IsMatch(null).Score;
// Assert
Check.That(result).IsEqualTo(0.0d);
@@ -79,7 +79,7 @@ public class RegexMatcherTests
var matcher = new RegexMatcher(@"\GUIDB", true);
// Act
double result = matcher.IsMatch(Guid.NewGuid().ToString("B"));
double result = matcher.IsMatch(Guid.NewGuid().ToString("B")).Score;
// Assert
result.Should().Be(1.0);
@@ -89,10 +89,10 @@ public class RegexMatcherTests
public void RegexMatcher_IsMatch_Regex_Guid()
{
// Assign
var matcher = new RegexMatcher(@"\GUIDB", true, false, false);
var matcher = new RegexMatcher(@"\GUIDB", true, false);
// Act
double result = matcher.IsMatch(Guid.NewGuid().ToString("B"));
double result = matcher.IsMatch(Guid.NewGuid().ToString("B")).Score;
// Assert
result.Should().Be(0);
@@ -105,7 +105,7 @@ public class RegexMatcherTests
var matcher = new RegexMatcher("H.*o", true);
// Act
double result = matcher.IsMatch("hello");
double result = matcher.IsMatch("hello").Score;
// Assert
Check.That(result).IsEqualTo(1.0d);
@@ -118,7 +118,7 @@ public class RegexMatcherTests
var matcher = new RegexMatcher(MatchBehaviour.RejectOnMatch, "h.*o");
// Act
double result = matcher.IsMatch("hello");
double result = matcher.IsMatch("hello").Score;
// Assert
Check.That(result).IsEqualTo(0.0);

View File

@@ -13,12 +13,12 @@ public class RequestMatchResultTests
{
// Arrange
var result1 = new RequestMatchResult();
result1.AddScore(typeof(WildcardMatcher), 1);
result1.AddScore(typeof(WildcardMatcher), 0.9);
result1.AddScore(typeof(WildcardMatcher), 1, null);
result1.AddScore(typeof(WildcardMatcher), 0.9, null);
var result2 = new RequestMatchResult();
result2.AddScore(typeof(LinqMatcher), 1);
result2.AddScore(typeof(LinqMatcher), 1);
result2.AddScore(typeof(LinqMatcher), 1, null);
result2.AddScore(typeof(LinqMatcher), 1, null);
var results = new[] { result1, result2 };
@@ -34,13 +34,13 @@ public class RequestMatchResultTests
{
// Arrange
var result1 = new RequestMatchResult();
result1.AddScore(typeof(WildcardMatcher), 1);
result1.AddScore(typeof(WildcardMatcher), 1);
result1.AddScore(typeof(WildcardMatcher), 1, null);
result1.AddScore(typeof(WildcardMatcher), 1, null);
var result2 = new RequestMatchResult();
result2.AddScore(typeof(LinqMatcher), 1);
result2.AddScore(typeof(LinqMatcher), 1);
result2.AddScore(typeof(LinqMatcher), 1);
result2.AddScore(typeof(LinqMatcher), 1, null);
result2.AddScore(typeof(LinqMatcher), 1, null);
result2.AddScore(typeof(LinqMatcher), 1, null);
var results = new[] { result1, result2 };

View File

@@ -39,7 +39,7 @@ public class SimMetricsMatcherTests
var matcher = new SimMetricsMatcher("The cat walks in the street.");
// Act
double result = matcher.IsMatch("The car drives in the street.");
double result = matcher.IsMatch("The car drives in the street.").Score;
// Assert
Check.That(result).IsStrictlyLessThan(1.0).And.IsStrictlyGreaterThan(0.5);
@@ -52,7 +52,7 @@ public class SimMetricsMatcherTests
var matcher = new SimMetricsMatcher("The cat walks in the street.");
// Act
double result = matcher.IsMatch("Hello");
double result = matcher.IsMatch("Hello").Score;
// Assert
Check.That(result).IsStrictlyLessThan(0.1).And.IsStrictlyGreaterThan(0.05);
@@ -65,7 +65,7 @@ public class SimMetricsMatcherTests
var matcher = new SimMetricsMatcher("test");
// Act
double result = matcher.IsMatch("test");
double result = matcher.IsMatch("test").Score;
// Assert
Check.That(result).IsEqualTo(1.0);
@@ -78,7 +78,7 @@ public class SimMetricsMatcherTests
var matcher = new SimMetricsMatcher(MatchBehaviour.RejectOnMatch, "test");
// Act
double result = matcher.IsMatch("test");
double result = matcher.IsMatch("test").Score;
// Assert
Check.That(result).IsEqualTo(0.0);

View File

@@ -19,11 +19,13 @@ public class WildcardMatcherTest
PatternAsFile = "pf"
};
// Act
var matcher = new WildcardMatcher(pattern);
// Act
var score = matcher.IsMatch("a").Score;
// Assert
matcher.IsMatch("a").Should().Be(1.0d);
score.Should().Be(1.0d);
}
[Fact]
@@ -39,11 +41,13 @@ public class WildcardMatcherTest
Pattern = "b"
};
// Act
var matcher = new WildcardMatcher(new [] { pattern1, pattern2 });
// Act
var score = matcher.IsMatch("a").Score;
// Assert
matcher.IsMatch("a").Should().Be(1.0d);
score.Should().Be(1.0d);
}
[Fact]
@@ -69,7 +73,12 @@ public class WildcardMatcherTest
foreach (var test in tests)
{
var matcher = new WildcardMatcher(test.p);
matcher.IsMatch(test.i).Should().Be(1.0d, $"Pattern '{test.p}' with value '{test.i}' should be 1.0");
// Act
var score = matcher.IsMatch(test.i).Score;
// Assert
score.Should().Be(MatchScores.Perfect, $"Pattern '{test.p}' with value '{test.i}' should be 1.0");
}
}
@@ -94,7 +103,12 @@ public class WildcardMatcherTest
foreach (var test in tests)
{
var matcher = new WildcardMatcher(test.p);
Check.That(matcher.IsMatch(test.i)).IsEqualTo(0.0);
// Act
var score = matcher.IsMatch(test.i).Score;
// Assert
score.Should().Be(MatchScores.Mismatch);
}
}
@@ -105,7 +119,7 @@ public class WildcardMatcherTest
var matcher = new WildcardMatcher("x");
// Act
string name = matcher.Name;
var name = matcher.Name;
// Assert
Check.That(name).Equals("WildcardMatcher");
@@ -131,7 +145,7 @@ public class WildcardMatcherTest
var matcher = new WildcardMatcher(MatchBehaviour.RejectOnMatch, "m");
// Act
double result = matcher.IsMatch("m");
var result = matcher.IsMatch("m").Score;
Check.That(result).IsEqualTo(0.0);
}

View File

@@ -43,7 +43,7 @@ public class XPathMatcherTests
var matcher = new XPathMatcher("/todo-list[count(todo-item) = 1]");
// Act
double result = matcher.IsMatch(xml);
double result = matcher.IsMatch(xml).Score;
// Assert
Check.That(result).IsEqualTo(1.0);
@@ -57,10 +57,10 @@ public class XPathMatcherTests
<todo-list>
<todo-item id='a1'>abc</todo-item>
</todo-list>";
var matcher = new XPathMatcher(MatchBehaviour.RejectOnMatch, false, MatchOperator.Or, "/todo-list[count(todo-item) = 1]");
var matcher = new XPathMatcher(MatchBehaviour.RejectOnMatch, MatchOperator.Or, "/todo-list[count(todo-item) = 1]");
// Act
double result = matcher.IsMatch(xml);
double result = matcher.IsMatch(xml).Score;
// Assert
Check.That(result).IsEqualTo(0.0);

View File

@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using Xunit;
using Moq;
using System.Threading.Tasks;
using System.Threading;
using FluentAssertions;
using WireMock.Handlers;
using WireMock.Owin.Mappers;
using WireMock.ResponseBuilders;
@@ -235,6 +237,24 @@ namespace WireMock.Net.Tests.Owin.Mappers
#endif
}
[Fact]
public void OwinResponseMapper_MapAsync_BodyAsFile_ThrowsException()
{
// Arrange
var responseMessage = new ResponseMessage
{
Headers = new Dictionary<string, WireMockList<string>>(),
BodyData = new BodyData { DetectedBodyType = BodyType.File, BodyAsFile = string.Empty }
};
_fileSystemHandlerMock.Setup(f => f.ReadResponseBodyAsFile(It.IsAny<string>())).Throws<FileNotFoundException>();
// Act
Func<Task> action = () => _sut.MapAsync(responseMessage, _responseMock.Object);
// Assert
action.Should().ThrowAsync<FileNotFoundException>();
}
[Fact]
public async Task OwinResponseMapper_MapAsync_WithFault_EMPTY_RESPONSE()
{
@@ -251,7 +271,7 @@ namespace WireMock.Net.Tests.Owin.Mappers
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
// Assert
_stream.Verify(s => s.WriteAsync(new byte[0], 0, 0, It.IsAny<CancellationToken>()), Times.Once);
_stream.Verify(s => s.WriteAsync(EmptyArray<byte>.Value, 0, 0, It.IsAny<CancellationToken>()), Times.Once);
}
[Theory]

View File

@@ -220,7 +220,7 @@ public class MappingMatcherTests
var requestMatchResult = new RequestMatchResult();
foreach (var score in match.scores)
{
requestMatchResult.AddScore(typeof(object), score);
requestMatchResult.AddScore(typeof(object), score, null);
}
mappingMock.SetupGet(m => m.Probability).Returns(match.probability);

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -77,7 +77,6 @@ server
.WithBody(new JsonPartialMatcher(
value: "{ a = 1, b = 2 }",
ignoreCase: false,
throwException: false,
regex: false
))
)

View File

@@ -138,25 +138,25 @@ public partial class WireMockServerTests
#if NETCOREAPP3_1 || NET5_0 || NET6_0 || NET7_0
[Fact]
public async Task WireMockServer_WithCorsPolicyOptions_Should_Work_Correct()
{
// Arrange
var settings = new WireMockServerSettings
public async Task WireMockServer_WithCorsPolicyOptions_Should_Work_Correct()
{
CorsPolicyOptions = CorsPolicyOptions.AllowAll
};
var server = WireMockServer.Start(settings);
// Arrange
var settings = new WireMockServerSettings
{
CorsPolicyOptions = CorsPolicyOptions.AllowAll
};
var server = WireMockServer.Start(settings);
server.Given(Request.Create().WithPath("/*")).RespondWith(Response.Create().WithBody("x"));
server.Given(Request.Create().WithPath("/*")).RespondWith(Response.Create().WithBody("x"));
// Act
var response = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
// Act
var response = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
// Asser.
response.Should().Be("x");
// Asser.
response.Should().Be("x");
server.Stop();
}
server.Stop();
}
#endif
[Fact]
@@ -277,34 +277,34 @@ public async Task WireMockServer_WithCorsPolicyOptions_Should_Work_Correct()
}
#if !NET452 && !NET461
[Theory]
[InlineData("TRACE")]
[InlineData("GET")]
public async Task WireMockServer_Should_exclude_body_for_methods_where_body_is_definitely_disallowed(string method)
{
// Assign
string content = "hello";
var server = WireMockServer.Start();
[Theory]
[InlineData("TRACE")]
[InlineData("GET")]
public async Task WireMockServer_Should_exclude_body_for_methods_where_body_is_definitely_disallowed(string method)
{
// Assign
string content = "hello";
var server = WireMockServer.Start();
server
.Given(Request.Create().WithBody((byte[] bodyBytes) => bodyBytes != null))
.AtPriority(0)
.RespondWith(Response.Create().WithStatusCode(400));
server
.Given(Request.Create())
.AtPriority(1)
.RespondWith(Response.Create().WithStatusCode(200));
server
.Given(Request.Create().WithBody((byte[] bodyBytes) => bodyBytes != null))
.AtPriority(0)
.RespondWith(Response.Create().WithStatusCode(400));
server
.Given(Request.Create())
.AtPriority(1)
.RespondWith(Response.Create().WithStatusCode(200));
// Act
var request = new HttpRequestMessage(new HttpMethod(method), "http://localhost:" + server.Ports[0] + "/");
request.Content = new StringContent(content);
var response = await new HttpClient().SendAsync(request).ConfigureAwait(false);
// Act
var request = new HttpRequestMessage(new HttpMethod(method), "http://localhost:" + server.Ports[0] + "/");
request.Content = new StringContent(content);
var response = await new HttpClient().SendAsync(request).ConfigureAwait(false);
// Assert
Check.That(response.StatusCode).Equals(HttpStatusCode.OK);
// Assert
Check.That(response.StatusCode).Equals(HttpStatusCode.OK);
server.Stop();
}
server.Stop();
}
#endif
[Theory]
@@ -462,11 +462,10 @@ public async Task WireMockServer_Should_exclude_body_for_methods_where_body_is_d
settings.CustomMatcherMappings = new Dictionary<string, Func<MatcherModel, IMatcher>>();
settings.CustomMatcherMappings[nameof(CustomPathParamMatcher)] = matcherModel =>
{
var matcherParams = JsonConvert.DeserializeObject<CustomPathParamMatcherModel>((string)matcherModel.Pattern);
var matcherParams = JsonConvert.DeserializeObject<CustomPathParamMatcherModel>((string)matcherModel.Pattern!)!;
return new CustomPathParamMatcher(
matcherModel.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch,
matcherParams.Path, matcherParams.PathParams,
settings.ThrowExceptionWhenMatcherFails == true
matcherParams.Path, matcherParams.PathParams
);
};
@@ -510,11 +509,10 @@ public async Task WireMockServer_Should_exclude_body_for_methods_where_body_is_d
settings.CustomMatcherMappings = new Dictionary<string, Func<MatcherModel, IMatcher>>();
settings.CustomMatcherMappings[nameof(CustomPathParamMatcher)] = matcherModel =>
{
var matcherParams = JsonConvert.DeserializeObject<CustomPathParamMatcherModel>((string)matcherModel.Pattern);
var matcherParams = JsonConvert.DeserializeObject<CustomPathParamMatcherModel>((string)matcherModel.Pattern!)!;
return new CustomPathParamMatcher(
matcherModel.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch,
matcherParams.Path, matcherParams.PathParams,
settings.ThrowExceptionWhenMatcherFails == true
matcherParams.Path, matcherParams.PathParams
);
};