JsonPartialMatcher - support Regex (#771)

* JsonPartialMatcher - support Regex

* .

* .

* more tests

* .

* .
This commit is contained in:
Stef Heyenrath
2022-07-24 15:54:53 +02:00
committed by GitHub
parent 150b448d07
commit bdd421e128
20 changed files with 1773 additions and 1625 deletions

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System;
using System.IO;
using FluentAssertions;
using Newtonsoft.Json;
@@ -42,6 +41,7 @@ namespace WireMock.Net.Tests.Matchers
public void JsonPartialMatcher_WithInvalidStringValue_Should_ThrowException()
{
// Act
// ReSharper disable once ObjectCreationAsStatement
Action action = () => new JsonPartialMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\"");
// Assert
@@ -52,6 +52,7 @@ namespace WireMock.Net.Tests.Matchers
public void JsonPartialMatcher_WithInvalidObjectValue_Should_ThrowException()
{
// Act
// ReSharper disable once ObjectCreationAsStatement
Action action = () => new JsonPartialMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream());
// Assert
@@ -102,7 +103,7 @@ namespace WireMock.Net.Tests.Matchers
public void JsonPartialMatcher_IsMatch_NullString()
{
// Assign
string s = null;
string? s = null;
var matcher = new JsonPartialMatcher("");
// Act
@@ -116,7 +117,7 @@ namespace WireMock.Net.Tests.Matchers
public void JsonPartialMatcher_IsMatch_NullObject()
{
// Assign
object o = null;
object? o = null;
var matcher = new JsonPartialMatcher("");
// Act
@@ -151,17 +152,53 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JsonPartialMatcher(new { Id = 1, Name = "Test" });
// Act
var jobject = new JObject
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialMatcher_IsMatch_WithRegexTrue()
{
// Assign
var matcher = new JsonPartialMatcher(new { Id = "^\\d+$", Name = "Test" }, false, false, true);
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialMatcher_IsMatch_WithRegexFalse()
{
// Assign
var matcher = new JsonPartialMatcher(new { Id = "^\\d+$", Name = "Test" });
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(0.0, match);
}
[Fact]
public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObject()
{
@@ -169,12 +206,12 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JsonPartialMatcher(new { id = 1, Name = "test" }, true);
// Act
var jobject = new JObject
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "NaMe", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
@@ -187,8 +224,8 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JsonPartialMatcher(new { Id = 1, Name = "Test" });
// Act
var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jobject);
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
@@ -201,8 +238,8 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JsonPartialMatcher(new { Id = 1, Name = "TESt" }, true);
// Act
var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jobject);
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
@@ -233,12 +270,12 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JsonPartialMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }");
// Act
var jobject = new JObject
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
@@ -251,12 +288,12 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JsonPartialMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true);
// Act
var jobject = new JObject
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
@@ -269,12 +306,12 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JsonPartialMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }");
// Act
var jobject = new JObject
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(0.0, match);
@@ -287,11 +324,11 @@ namespace WireMock.Net.Tests.Matchers
var matcher = new JsonPartialMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }");
// Act
var jobject = new JObject
var jObject = new JObject
{
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
};
double match = matcher.IsMatch(jobject);
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Newtonsoft.Json;
@@ -8,393 +7,394 @@ using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
namespace WireMock.Net.Tests.Matchers;
public class JsonPartialWildcardMatcherTests
{
public class JsonPartialWildcardMatcherTests
[Fact]
public void JsonPartialWildcardMatcher_GetName()
{
[Fact]
public void JsonPartialWildcardMatcher_GetName()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("{}");
// Assign
var matcher = new JsonPartialWildcardMatcher("{}");
// Act
string name = matcher.Name;
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("JsonPartialWildcardMatcher");
}
[Fact]
public void JsonPartialWildcardMatcher_GetValue()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("{}");
// Act
object value = matcher.Value;
// Assert
Check.That(value).Equals("{}");
}
[Fact]
public void JsonPartialWildcardMatcher_WithInvalidStringValue_Should_ThrowException()
{
// Act
Action action = () => new JsonPartialWildcardMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\"");
// Assert
action.Should().Throw<JsonException>();
}
[Fact]
public void JsonPartialWildcardMatcher_WithInvalidObjectValue_Should_ThrowException()
{
// Act
Action action = () => new JsonPartialWildcardMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream());
// Assert
action.Should().Throw<JsonException>();
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = 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>();
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_ByteArray()
{
// Assign
var bytes = new byte[0];
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = matcher.IsMatch(bytes);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_NullString()
{
// Assign
string s = null;
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = matcher.IsMatch(s);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_NullObject()
{
// Assign
object o = null;
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = matcher.IsMatch(o);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JArray()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new[] { "x", "y" });
// Act
var jArray = new JArray
{
"x",
"y"
};
double match = matcher.IsMatch(jArray);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JObject()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new { Id = 1, Name = "Test" });
// Act
var jobject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrue_JObject()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new { id = 1, Name = "test" }, true);
// Act
var jobject = new JObject
{
{ "Id", new JValue(1) },
{ "NaMe", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JObjectParsed()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new { Id = 1, Name = "Test" });
// Act
var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jobject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new { Id = 1, Name = "TESt" }, true);
// Act
var jobject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jobject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JArrayAsString()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("[ \"x\", \"y\" ]");
// Act
var jArray = new JArray
{
"x",
"y"
};
double match = matcher.IsMatch(jArray);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JObjectAsString()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }");
// Act
var jobject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true);
// Act
var jobject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JObjectAsString_RejectOnMatch()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }");
// Act
var jobject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
// Assert
Assert.Equal(0.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JObjectWithDateTimeOffsetAsString()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }");
// Act
var jobject = new JObject
{
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
};
double match = matcher.IsMatch(jobject);
// Assert
Assert.Equal(1.0, match);
}
[Theory]
[InlineData("{\"test\":\"abc\"}", "{\"test\":\"abc\",\"other\":\"xyz\"}")]
[InlineData("\"test\"", "\"test\"")]
[InlineData("123", "123")]
[InlineData("[\"test\"]", "[\"test\"]")]
[InlineData("[\"test\"]", "[\"test\", \"other\"]")]
[InlineData("[123]", "[123]")]
[InlineData("[123]", "[123, 456]")]
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value\",\"other\":123}")]
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value\"}")]
[InlineData("{\"test\":{\"nested\":\"value\"}}", "{\"test\":{\"nested\":\"value\"}}")]
public void JsonPartialWildcardMatcher_IsMatch_StringInput_IsValidMatch(string value, string input)
{
// Assign
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
// Assert
Assert.Equal(1.0, match);
}
[Theory]
[InlineData("{\"test\":\"*\"}", "{\"test\":\"xxx\",\"other\":\"xyz\"}")]
[InlineData("\"t*t\"", "\"test\"")]
public void JsonPartialWildcardMatcher_IsMatch_StringInputWithWildcard_IsValidMatch(string value, string input)
{
// Assign
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
// Assert
match.Should().Be(1.0);
}
[Theory]
[InlineData("\"test\"", null)]
[InlineData("\"test1\"", "\"test2\"")]
[InlineData("123", "1234")]
[InlineData("[\"test\"]", "[\"test1\"]")]
[InlineData("[\"test\"]", "[\"test1\", \"test2\"]")]
[InlineData("[123]", "[1234]")]
[InlineData("{}", "\"test\"")]
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value2\"}")]
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}")]
[InlineData("{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}")]
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]")]
public void JsonPartialWildcardMatcher_IsMatch_StringInputWithInvalidMatch(string value, string input)
{
// Assign
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
// Assert
Assert.Equal(0.0, match);
}
[Theory]
[InlineData("{ \"test.nested\":123 }", "{\"test\":{\"nested\":123}}")]
[InlineData("{ \"test.nested\":[123, 456] }", "{\"test\":{\"nested\":[123, 456]}}")]
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value\"}}")]
[InlineData("{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value\"}")]
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value\"}}]")]
[InlineData("[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value\"}]")]
public void JsonPartialWildcardMatcher_IsMatch_ValueAsJPathValidMatch(string value, string input)
{
// Assign
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
// Assert
Assert.Equal(1.0, match);
}
[Theory]
[InlineData("{ \"test.nested\":123 }", "{\"test\":{\"nested\":456}}")]
[InlineData("{ \"test.nested\":[123, 456] }", "{\"test\":{\"nested\":[1, 2]}}")]
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}")]
[InlineData("{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value1\"}")]
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]")]
[InlineData("[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value1\"}]")]
public void JsonPartialWildcardMatcher_IsMatch_ValueAsJPathInvalidMatch(string value, string input)
{
// Assign
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
// Assert
Assert.Equal(0.0, match);
}
// Assert
Check.That(name).Equals("JsonPartialWildcardMatcher");
}
}
[Fact]
public void JsonPartialWildcardMatcher_GetValue()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("{}");
// Act
object value = matcher.Value;
// Assert
Check.That(value).Equals("{}");
}
[Fact]
public void JsonPartialWildcardMatcher_WithInvalidStringValue_Should_ThrowException()
{
// Act
// ReSharper disable once ObjectCreationAsStatement
Action action = () => new JsonPartialWildcardMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\"");
// Assert
action.Should().Throw<JsonException>();
}
[Fact]
public void JsonPartialWildcardMatcher_WithInvalidObjectValue_Should_ThrowException()
{
// Act
// ReSharper disable once ObjectCreationAsStatement
Action action = () => new JsonPartialWildcardMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream());
// Assert
action.Should().Throw<JsonException>();
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = 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>();
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_ByteArray()
{
// Assign
var bytes = new byte[0];
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = matcher.IsMatch(bytes);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_NullString()
{
// Assign
string? s = null;
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = matcher.IsMatch(s);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_NullObject()
{
// Assign
object? o = null;
var matcher = new JsonPartialWildcardMatcher("");
// Act
double match = matcher.IsMatch(o);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JArray()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new[] { "x", "y" });
// Act
var jArray = new JArray
{
"x",
"y"
};
double match = matcher.IsMatch(jArray);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JObject()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new { Id = 1, Name = "Test" });
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrue_JObject()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new { id = 1, Name = "test" }, true);
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "NaMe", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JObjectParsed()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new { Id = 1, Name = "Test" });
// Act
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrue_JObjectParsed()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new { Id = 1, Name = "TESt" }, true);
// Act
var jObject = JObject.Parse("{ \"Id\" : 1, \"Name\" : \"Test\" }");
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JArrayAsString()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("[ \"x\", \"y\" ]");
// Act
var jArray = new JArray
{
"x",
"y"
};
double match = matcher.IsMatch(jArray);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JObjectAsString()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("{ \"Id\" : 1, \"Name\" : \"Test\" }");
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("{ \"Id\" : 1, \"Name\" : \"test\" }", true);
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JObjectAsString_RejectOnMatch()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(MatchBehaviour.RejectOnMatch, "{ \"Id\" : 1, \"Name\" : \"Test\" }");
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(0.0, match);
}
[Fact]
public void JsonPartialWildcardMatcher_IsMatch_JObjectWithDateTimeOffsetAsString()
{
// Assign
var matcher = new JsonPartialWildcardMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }");
// Act
var jObject = new JObject
{
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
};
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Theory]
[InlineData("{\"test\":\"abc\"}", "{\"test\":\"abc\",\"other\":\"xyz\"}")]
[InlineData("\"test\"", "\"test\"")]
[InlineData("123", "123")]
[InlineData("[\"test\"]", "[\"test\"]")]
[InlineData("[\"test\"]", "[\"test\", \"other\"]")]
[InlineData("[123]", "[123]")]
[InlineData("[123]", "[123, 456]")]
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value\",\"other\":123}")]
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value\"}")]
[InlineData("{\"test\":{\"nested\":\"value\"}}", "{\"test\":{\"nested\":\"value\"}}")]
public void JsonPartialWildcardMatcher_IsMatch_StringInput_IsValidMatch(string value, string input)
{
// Assign
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
// Assert
Assert.Equal(1.0, match);
}
[Theory]
[InlineData("{\"test\":\"*\"}", "{\"test\":\"xxx\",\"other\":\"xyz\"}")]
[InlineData("\"t*t\"", "\"test\"")]
public void JsonPartialWildcardMatcher_IsMatch_StringInputWithWildcard_IsValidMatch(string value, string input)
{
// Assign
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
// Assert
match.Should().Be(1.0);
}
[Theory]
[InlineData("\"test\"", null)]
[InlineData("\"test1\"", "\"test2\"")]
[InlineData("123", "1234")]
[InlineData("[\"test\"]", "[\"test1\"]")]
[InlineData("[\"test\"]", "[\"test1\", \"test2\"]")]
[InlineData("[123]", "[1234]")]
[InlineData("{}", "\"test\"")]
[InlineData("{ \"test\":\"value\" }", "{\"test\":\"value2\"}")]
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}")]
[InlineData("{\"test\":{\"test1\":\"value\"}}", "{\"test\":{\"test1\":\"value1\"}}")]
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]")]
public void JsonPartialWildcardMatcher_IsMatch_StringInputWithInvalidMatch(string value, string input)
{
// Assign
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
// Assert
Assert.Equal(0.0, match);
}
[Theory]
[InlineData("{ \"test.nested\":123 }", "{\"test\":{\"nested\":123}}")]
[InlineData("{ \"test.nested\":[123, 456] }", "{\"test\":{\"nested\":[123, 456]}}")]
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value\"}}")]
[InlineData("{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value\"}")]
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value\"}}]")]
[InlineData("[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value\"}]")]
public void JsonPartialWildcardMatcher_IsMatch_ValueAsJPathValidMatch(string value, string input)
{
// Assign
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
// Assert
Assert.Equal(1.0, match);
}
[Theory]
[InlineData("{ \"test.nested\":123 }", "{\"test\":{\"nested\":456}}")]
[InlineData("{ \"test.nested\":[123, 456] }", "{\"test\":{\"nested\":[1, 2]}}")]
[InlineData("{ \"test.nested\":\"value\" }", "{\"test\":{\"nested\":\"value1\"}}")]
[InlineData("{ \"['name.with.dot']\":\"value\" }", "{\"name.with.dot\":\"value1\"}")]
[InlineData("[{ \"test.nested\":\"value\" }]", "[{\"test\":{\"nested\":\"value1\"}}]")]
[InlineData("[{ \"['name.with.dot']\":\"value\" }]", "[{\"name.with.dot\":\"value1\"}]")]
public void JsonPartialWildcardMatcher_IsMatch_ValueAsJPathInvalidMatch(string value, string input)
{
// Assign
var matcher = new JsonPartialWildcardMatcher(value);
// Act
double match = matcher.IsMatch(input);
// Assert
Assert.Equal(0.0, match);
}
}

View File

@@ -5,165 +5,164 @@ using WireMock.Matchers.Request;
using WireMock.Models;
using Xunit;
namespace WireMock.Net.Tests.RequestMatchers
namespace WireMock.Net.Tests.RequestMatchers;
public class RequestMessageHeaderMatcherTests
{
public class RequestMessageHeaderMatcherTests
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_AcceptOnMatch_HeaderDoesNotExists()
{
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_AcceptOnMatch_HeaderDoesNotExists()
{
// Assign
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1");
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "h", "x", true);
// Assign
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1");
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "h", "x", true);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(0.0d);
}
// Assert
Check.That(score).IsEqualTo(0.0d);
}
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_RejectOnMatch_HeaderDoesNotExists()
{
// Assign
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1");
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.RejectOnMatch, "h", "x", true);
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_RejectOnMatch_HeaderDoesNotExists()
{
// Assign
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1");
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.RejectOnMatch, "h", "x", true);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(1.0d);
}
// Assert
Check.That(score).IsEqualTo(1.0d);
}
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_AcceptOnMatch_HeaderDoesNotMatchPattern()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "no-match", "123", true);
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_AcceptOnMatch_HeaderDoesNotMatchPattern()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "no-match", "123", true);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(0.0d);
}
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_RejectOnMatch_HeaderDoesNotMatchPattern()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.RejectOnMatch, "no-match", "123", true);
// Assert
Check.That(score).IsEqualTo(0.0d);
}
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_RejectOnMatch_HeaderDoesNotMatchPattern()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.RejectOnMatch, "no-match", "123", true);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(1.0d);
}
// Assert
Check.That(score).IsEqualTo(1.0d);
}
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_AcceptOnMatch()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "h", "x", true);
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_AcceptOnMatch()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "h", "x", true);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(1.0d);
}
// Assert
Check.That(score).IsEqualTo(1.0d);
}
[Fact(Skip = "does not work anymore since 'and'/'or'/'average'")]
public void RequestMessageHeaderMatcher_GetMatchingScore_RejectOnMatch()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.RejectOnMatch, "h", "x", true);
[Fact(Skip = "does not work anymore since 'and'/'or'/'average'")]
public void RequestMessageHeaderMatcher_GetMatchingScore_RejectOnMatch()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.RejectOnMatch, "h", "x", true);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(0.0d);
}
// Assert
Check.That(score).IsEqualTo(0.0d);
}
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_IStringMatcher_Match()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, "h", false, new ExactMatcher("x"));
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_IStringMatcher_Match()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, "h", false, new ExactMatcher("x"));
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(1.0d);
}
// Assert
Check.That(score).IsEqualTo(1.0d);
}
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_Func_Match()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(x => x.ContainsKey("h"));
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_Func_Match()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(x => x.ContainsKey("h"));
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(1.0d);
}
// Assert
Check.That(score).IsEqualTo(1.0d);
}
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_CaseIgnoreForHeaderValue()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "teST" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "h", "test", true);
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_CaseIgnoreForHeaderValue()
{
// Assign
var headers = new Dictionary<string, string[]> { { "h", new[] { "teST" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "h", "test", true);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(1.0d);
}
// Assert
Check.That(score).IsEqualTo(1.0d);
}
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_CaseIgnoreForHeaderName()
{
// Assign
var headers = new Dictionary<string, string[]> { { "teST", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "TEST", "x", true);
[Fact]
public void RequestMessageHeaderMatcher_GetMatchingScore_CaseIgnoreForHeaderName()
{
// Assign
var headers = new Dictionary<string, string[]> { { "teST", new[] { "x" } } };
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", null, headers);
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "TEST", "x", true);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Act
var result = new RequestMatchResult();
double score = matcher.GetMatchingScore(requestMessage, result);
// Assert
Check.That(score).IsEqualTo(1.0d);
}
// Assert
Check.That(score).IsEqualTo(1.0d);
}
}

View File

@@ -1,107 +1,106 @@
using NFluent;
using NFluent;
using WireMock.Models;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests
namespace WireMock.Net.Tests;
public class RequestMessageTests
{
public class RequestMessageTests
private const string ClientIp = "::1";
[Fact]
public void RequestMessage_Method_Should_BeSame()
{
private const string ClientIp = "::1";
// given
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "posT", ClientIp);
[Fact]
public void RequestMessage_Method_Should_BeSame()
{
// given
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "posT", ClientIp);
// then
Check.That(request.Method).IsEqualTo("posT");
}
// then
Check.That(request.Method).IsEqualTo("posT");
}
[Fact]
public void RequestMessage_ParseQuery_NoKeys()
{
// given
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp);
[Fact]
public void RequestMessage_ParseQuery_NoKeys()
{
// given
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp);
// then
Check.That(request.GetParameter("not_there")).IsNull();
}
// then
Check.That(request.GetParameter("not_there")).IsNull();
}
[Fact]
public void RequestMessage_ParseQuery_SingleKey_SingleValue()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost?foo=bar"), "POST", ClientIp);
[Fact]
public void RequestMessage_ParseQuery_SingleKey_SingleValue()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost?foo=bar"), "POST", ClientIp);
// Assert
Check.That(request.GetParameter("foo")).ContainsExactly("bar");
}
// Assert
Check.That(request.GetParameter("foo")).ContainsExactly("bar");
}
[Fact]
public void RequestMessage_ParseQuery_SingleKey_SingleValue_WithIgnoreCase()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost?foo=bar"), "POST", ClientIp);
[Fact]
public void RequestMessage_ParseQuery_SingleKey_SingleValue_WithIgnoreCase()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost?foo=bar"), "POST", ClientIp);
// Assert
Check.That(request.GetParameter("FoO", true)).ContainsExactly("bar");
}
// Assert
Check.That(request.GetParameter("FoO", true)).ContainsExactly("bar");
}
[Fact]
public void RequestMessage_ParseQuery_MultipleKeys_MultipleValues()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost?key=1&key=2"), "POST", ClientIp);
[Fact]
public void RequestMessage_ParseQuery_MultipleKeys_MultipleValues()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost?key=1&key=2"), "POST", ClientIp);
// Assert
Check.That(request.GetParameter("key")).Contains("1");
Check.That(request.GetParameter("key")).Contains("2");
}
// Assert
Check.That(request.GetParameter("key")).Contains("1");
Check.That(request.GetParameter("key")).Contains("2");
}
[Fact]
public void RequestMessage_ParseQuery_SingleKey_MultipleValuesCommaSeparated()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost?key=1,2,3"), "POST", ClientIp);
[Fact]
public void RequestMessage_ParseQuery_SingleKey_MultipleValuesCommaSeparated()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost?key=1,2,3"), "POST", ClientIp);
// Assert
Check.That(request.GetParameter("key")).Contains("1");
Check.That(request.GetParameter("key")).Contains("2");
Check.That(request.GetParameter("key")).Contains("3");
}
// Assert
Check.That(request.GetParameter("key")).Contains("1");
Check.That(request.GetParameter("key")).Contains("2");
Check.That(request.GetParameter("key")).Contains("3");
}
[Fact]
public void RequestMessage_ParseQuery_SingleKey_MultipleValues()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost?key=1,2&foo=bar&key=3"), "POST", ClientIp);
[Fact]
public void RequestMessage_ParseQuery_SingleKey_MultipleValues()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost?key=1,2&foo=bar&key=3"), "POST", ClientIp);
// Assert
Check.That(request.GetParameter("key")).Contains("1");
Check.That(request.GetParameter("key")).Contains("2");
Check.That(request.GetParameter("key")).Contains("3");
}
// Assert
Check.That(request.GetParameter("key")).Contains("1");
Check.That(request.GetParameter("key")).Contains("2");
Check.That(request.GetParameter("key")).Contains("3");
}
[Fact]
public void RequestMessage_Constructor1_PathSegments()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost/a/b/c"), "POST", ClientIp);
[Fact]
public void RequestMessage_Constructor1_PathSegments()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost/a/b/c"), "POST", ClientIp);
// Assert
Check.That(request.PathSegments).ContainsExactly("a", "b", "c");
}
// Assert
Check.That(request.PathSegments).ContainsExactly("a", "b", "c");
}
[Fact]
public void RequestMessage_Constructor2_PathSegments()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost/a/b/c"), "POST", ClientIp, new BodyData());
[Fact]
public void RequestMessage_Constructor2_PathSegments()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost/a/b/c"), "POST", ClientIp, new BodyData());
// Assert
Check.That(request.PathSegments).ContainsExactly("a", "b", "c");
}
// Assert
Check.That(request.PathSegments).ContainsExactly("a", "b", "c");
}
}

View File

@@ -10,399 +10,402 @@ using WireMock.Serialization;
using WireMock.Settings;
using Xunit;
namespace WireMock.Net.Tests.Serialization
namespace WireMock.Net.Tests.Serialization;
public class MatcherMapperTests
{
public class MatcherMapperTests
private readonly WireMockServerSettings _settings = new();
private readonly MatcherMapper _sut;
public MatcherMapperTests()
{
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
private readonly MatcherMapper _sut;
public MatcherMapperTests()
{
_sut = new MatcherMapper(_settings);
}
[Fact]
public void MatcherMapper_Map_IMatcher_Null()
{
// Act
var model = _sut.Map((IMatcher)null);
// Assert
model.Should().BeNull();
}
[Fact]
public void MatcherMapper_Map_IMatchers_Null()
{
// Act
var model = _sut.Map((IMatcher[])null);
// Assert
model.Should().BeNull();
}
[Fact]
public void MatcherMapper_Map_IMatchers()
{
// Assign
var matcherMock1 = new Mock<IStringMatcher>();
var matcherMock2 = new Mock<IStringMatcher>();
// Act
var models = _sut.Map(new[] { matcherMock1.Object, matcherMock2.Object });
// Assert
models.Should().HaveCount(2);
}
[Fact]
public void MatcherMapper_Map_IStringMatcher()
{
// Assign
var matcherMock = new Mock<IStringMatcher>();
matcherMock.Setup(m => m.Name).Returns("test");
matcherMock.Setup(m => m.GetPatterns()).Returns(new AnyOf<string, StringPattern>[] { "p1", "p2" });
// Act
var model = _sut.Map(matcherMock.Object);
// Assert
model.IgnoreCase.Should().BeNull();
model.Name.Should().Be("test");
model.Pattern.Should().BeNull();
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]
public void MatcherMapper_Map_IIgnoreCaseMatcher()
{
// Assign
var matcherMock = new Mock<IIgnoreCaseMatcher>();
matcherMock.Setup(m => m.IgnoreCase).Returns(true);
// Act
var model = _sut.Map(matcherMock.Object);
// Assert
model.IgnoreCase.Should().BeTrue();
}
[Fact]
public void MatcherMapper_Map_MatcherModel_Null()
{
// Act
var result = _sut.Map((MatcherModel)null);
// Assert
result.Should().BeNull();
}
[Fact]
public void MatcherMapper_Map_MatcherModel_Exception()
{
// Assign
var model = new MatcherModel { Name = "test" };
// Act and Assert
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
}
[Fact]
public void MatcherMapper_Map_MatcherModel_LinqMatcher_Pattern()
{
// Assign
var model = new MatcherModel
{
Name = "LinqMatcher",
Pattern = "p"
};
// Act
var matcher = (LinqMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.GetPatterns().Should().Contain("p");
}
[Fact]
public void MatcherMapper_Map_MatcherModel_LinqMatcher_Patterns()
{
// Assign
var model = new MatcherModel
{
Name = "LinqMatcher",
Patterns = new[] { "p1", "p2" }
};
// Act
var matcher = (LinqMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.GetPatterns().Should().Contain("p1", "p2");
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Pattern_As_String()
{
// Assign
var pattern = "{ \"AccountIds\": [ 1, 2, 3 ] }";
var model = new MatcherModel
{
Name = "JsonMatcher",
Pattern = pattern
};
// Act
var matcher = (JsonMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(pattern);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_1_Value_As_String()
{
// Assign
var pattern = "{ \"post1\": \"value1\", \"post2\": \"value2\" }";
var patterns = new[] { pattern };
var model = new MatcherModel
{
Name = "JsonMatcher",
Patterns = patterns
};
// Act
var matcher = (JsonMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_2_Values_As_String()
{
// Assign
var pattern1 = "{ \"AccountIds\": [ 1, 2, 3 ] }";
var pattern2 = "{ \"post1\": \"value1\", \"post2\": \"value2\" }";
var patterns = new[] { pattern1, pattern2 };
var model = new MatcherModel
{
Name = "JsonMatcher",
Patterns = patterns
};
// Act
var matcher = (JsonMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Pattern_As_Object()
{
// Assign
var pattern = new { AccountIds = new[] { 1, 2, 3 } };
var model = new MatcherModel
{
Name = "JsonMatcher",
Pattern = pattern
};
// Act
var matcher = (JsonMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(pattern);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_1_Value_As_Object()
{
// Assign
object pattern = new { post1 = "value1", post2 = "value2" };
var patterns = new[] { pattern };
var model = new MatcherModel
{
Name = "JsonMatcher",
Patterns = patterns
};
// Act
var matcher = (JsonMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_2_Values_As_Object()
{
// Assign
object pattern1 = new { AccountIds = new[] { 1, 2, 3 } };
object pattern2 = new { post1 = "value1", post2 = "value2" };
var patterns = new[] { pattern1, pattern2 };
var model = new MatcherModel
{
Name = "JsonMatcher",
Patterns = patterns
};
// Act
var matcher = (JsonMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Pattern_As_String()
{
// Assign
var pattern = "{ \"AccountIds\": [ 1, 2, 3 ] }";
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);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Patterns_As_String()
{
// Assign
var pattern1 = "{ \"AccountIds\": [ 1, 2, 3 ] }";
var pattern2 = "{ \"X\": \"x\" }";
var patterns = new[] { pattern1, pattern2 };
var model = new MatcherModel
{
Name = "JsonPartialMatcher",
Pattern = patterns
};
// Act
var matcher = (JsonPartialMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Pattern_As_Object()
{
// Assign
var pattern = new { AccountIds = new[] { 1, 2, 3 } };
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);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Patterns_As_Object()
{
// Assign
object pattern1 = new { AccountIds = new[] { 1, 2, 3 } };
object pattern2 = new { X = "x" };
var patterns = new[] { pattern1, pattern2 };
var model = new MatcherModel
{
Name = "JsonPartialMatcher",
Patterns = patterns
};
// Act
var matcher = (JsonMatcher)_sut.Map(model);
// Assert
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);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialWilcardMatcher_Patterns_As_Object()
{
// Assign
object pattern = new { X = "*" };
var model = new MatcherModel
{
Name = "JsonPartialWildcardMatcher",
Pattern = pattern
};
// Act
var matcher = (JsonPartialWildcardMatcher)_sut.Map(model);
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(pattern);
}
_sut = new MatcherMapper(_settings);
}
}
[Fact]
public void MatcherMapper_Map_IMatcher_Null()
{
// Act
var model = _sut.Map((IMatcher?)null);
// Assert
model.Should().BeNull();
}
[Fact]
public void MatcherMapper_Map_IMatchers_Null()
{
// Act
var model = _sut.Map((IMatcher[]?)null);
// Assert
model.Should().BeNull();
}
[Fact]
public void MatcherMapper_Map_IMatchers()
{
// Assign
var matcherMock1 = new Mock<IStringMatcher>();
var matcherMock2 = new Mock<IStringMatcher>();
// Act
var models = _sut.Map(new[] { matcherMock1.Object, matcherMock2.Object });
// Assert
models.Should().HaveCount(2);
}
[Fact]
public void MatcherMapper_Map_IStringMatcher()
{
// Assign
var matcherMock = new Mock<IStringMatcher>();
matcherMock.Setup(m => m.Name).Returns("test");
matcherMock.Setup(m => m.GetPatterns()).Returns(new AnyOf<string, StringPattern>[] { "p1", "p2" });
// Act
var model = _sut.Map(matcherMock.Object)!;
// Assert
model.IgnoreCase.Should().BeNull();
model.Name.Should().Be("test");
model.Pattern.Should().BeNull();
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]
public void MatcherMapper_Map_IIgnoreCaseMatcher()
{
// Assign
var matcherMock = new Mock<IIgnoreCaseMatcher>();
matcherMock.Setup(m => m.IgnoreCase).Returns(true);
// Act
var model = _sut.Map(matcherMock.Object)!;
// Assert
model.IgnoreCase.Should().BeTrue();
}
[Fact]
public void MatcherMapper_Map_MatcherModel_Null()
{
// Act
var result = _sut.Map((MatcherModel?)null);
// Assert
result.Should().BeNull();
}
[Fact]
public void MatcherMapper_Map_MatcherModel_Exception()
{
// Assign
var model = new MatcherModel { Name = "test" };
// Act and Assert
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
}
[Fact]
public void MatcherMapper_Map_MatcherModel_LinqMatcher_Pattern()
{
// Assign
var model = new MatcherModel
{
Name = "LinqMatcher",
Pattern = "p"
};
// Act
var matcher = (LinqMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.GetPatterns().Should().Contain("p");
}
[Fact]
public void MatcherMapper_Map_MatcherModel_LinqMatcher_Patterns()
{
// Assign
var model = new MatcherModel
{
Name = "LinqMatcher",
Patterns = new[] { "p1", "p2" }
};
// Act
var matcher = (LinqMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.GetPatterns().Should().Contain("p1", "p2");
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Pattern_As_String()
{
// Assign
var pattern = "{ \"AccountIds\": [ 1, 2, 3 ] }";
var model = new MatcherModel
{
Name = "JsonMatcher",
Pattern = pattern
};
// Act
var matcher = (JsonMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(pattern);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_1_Value_As_String()
{
// Assign
var pattern = "{ \"post1\": \"value1\", \"post2\": \"value2\" }";
var patterns = new[] { pattern };
var model = new MatcherModel
{
Name = "JsonMatcher",
Patterns = patterns
};
// Act
var matcher = (JsonMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_2_Values_As_String()
{
// Assign
var pattern1 = "{ \"AccountIds\": [ 1, 2, 3 ] }";
var pattern2 = "{ \"post1\": \"value1\", \"post2\": \"value2\" }";
var patterns = new[] { pattern1, pattern2 };
var model = new MatcherModel
{
Name = "JsonMatcher",
Patterns = patterns
};
// Act
var matcher = (JsonMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Pattern_As_Object()
{
// Assign
var pattern = new { AccountIds = new[] { 1, 2, 3 } };
var model = new MatcherModel
{
Name = "JsonMatcher",
Pattern = pattern
};
// Act
var matcher = (JsonMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(pattern);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_1_Value_As_Object()
{
// Assign
object pattern = new { post1 = "value1", post2 = "value2" };
var patterns = new[] { pattern };
var model = new MatcherModel
{
Name = "JsonMatcher",
Patterns = patterns
};
// Act
var matcher = (JsonMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_2_Values_As_Object()
{
// Assign
object pattern1 = new { AccountIds = new[] { 1, 2, 3 } };
object pattern2 = new { post1 = "value1", post2 = "value2" };
var patterns = new[] { pattern1, pattern2 };
var model = new MatcherModel
{
Name = "JsonMatcher",
Patterns = patterns
};
// Act
var matcher = (JsonMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Pattern_As_String()
{
// Assign
var pattern = "{ \"AccountIds\": [ 1, 2, 3 ] }";
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);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Patterns_As_String()
{
// Assign
var pattern1 = "{ \"AccountIds\": [ 1, 2, 3 ] }";
var pattern2 = "{ \"X\": \"x\" }";
var patterns = new[] { pattern1, pattern2 };
var model = new MatcherModel
{
Name = "JsonPartialMatcher",
Pattern = patterns
};
// Act
var matcher = (JsonPartialMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(patterns);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Pattern_As_Object()
{
// Assign
var pattern = new { AccountIds = new[] { 1, 2, 3 } };
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);
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Patterns_As_Object()
{
// Assign
object pattern1 = new { AccountIds = new[] { 1, 2, 3 } };
object pattern2 = new { X = "x" };
var patterns = new[] { pattern1, pattern2 };
var model = new MatcherModel
{
Name = "JsonPartialMatcher",
Patterns = patterns
};
// Act
var matcher = (JsonMatcher)_sut.Map(model)!;
// Assert
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,
Regex = true
};
// Act
var matcher = (JsonPartialMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(pattern);
matcher.Regex.Should().BeTrue();
}
[Fact]
public void MatcherMapper_Map_MatcherModel_JsonPartialWildcardMatcher_Patterns_As_Object()
{
// Assign
object pattern = new { X = "*" };
var model = new MatcherModel
{
Name = "JsonPartialWildcardMatcher",
Pattern = pattern,
Regex = false
};
// Act
var matcher = (JsonPartialWildcardMatcher)_sut.Map(model)!;
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.Value.Should().BeEquivalentTo(pattern);
matcher.Regex.Should().BeFalse();
}
}

View File

@@ -14,378 +14,427 @@ using WireMock.Serialization;
using WireMock.Settings;
using Xunit;
namespace WireMock.Net.Tests.Serialization
namespace WireMock.Net.Tests.Serialization;
public class MatcherModelMapperTests
{
public class MatcherModelMapperTests
private readonly WireMockServerSettings _settings = new();
private readonly MatcherMapper _sut;
public MatcherModelMapperTests()
{
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
_sut = new MatcherMapper(_settings);
}
private readonly MatcherMapper _sut;
public MatcherModelMapperTests()
[Fact]
public void MatcherModelMapper_Map_CSharpCodeMatcher()
{
// Assign
var model = new MatcherModel
{
_sut = new MatcherMapper(_settings);
}
Name = "CSharpCodeMatcher",
Patterns = new[] { "return it == \"x\";" }
};
var sut = new MatcherMapper(new WireMockServerSettings { AllowCSharpCodeMatcher = true });
[Fact]
public void MatcherModelMapper_Map_CSharpCodeMatcher()
// Act 1
var matcher1 = (ICSharpCodeMatcher)sut.Map(model)!;
// Assert 1
matcher1.Should().NotBeNull();
matcher1.IsMatch("x").Should().Be(1.0d);
// Act 2
var matcher2 = (ICSharpCodeMatcher)sut.Map(model)!;
// Assert 2
matcher2.Should().NotBeNull();
matcher2.IsMatch("x").Should().Be(1.0d);
}
[Fact]
public void MatcherModelMapper_Map_CSharpCodeMatcher_NotAllowed_ThrowsException()
{
// Assign
var model = new MatcherModel
{
// Assign
var model = new MatcherModel
{
Name = "CSharpCodeMatcher",
Patterns = new[] { "return it == \"x\";" }
};
var sut = new MatcherMapper(new WireMockServerSettings { AllowCSharpCodeMatcher = true });
Name = "CSharpCodeMatcher",
Patterns = new[] { "x" }
};
var sut = new MatcherMapper(new WireMockServerSettings { AllowCSharpCodeMatcher = false });
// Act 1
var matcher1 = (ICSharpCodeMatcher)sut.Map(model);
// Act
Action action = () => sut.Map(model);
// Assert 1
matcher1.Should().NotBeNull();
matcher1.IsMatch("x").Should().Be(1.0d);
// Assert
action.Should().Throw<NotSupportedException>();
}
// Act 2
var matcher2 = (ICSharpCodeMatcher)sut.Map(model);
[Fact]
public void MatcherModelMapper_Map_Null()
{
// Act
IMatcher matcher = _sut.Map((MatcherModel?)null)!;
// Assert 2
matcher2.Should().NotBeNull();
matcher2.IsMatch("x").Should().Be(1.0d);
}
// Assert
Check.That(matcher).IsNull();
}
[Fact]
public void MatcherModelMapper_Map_CSharpCodeMatcher_NotAllowed_ThrowsException()
[Fact]
public void MatcherModelMapper_Map_ExactMatcher_Pattern()
{
// Assign
var model = new MatcherModel
{
// Assign
var model = new MatcherModel
{
Name = "CSharpCodeMatcher",
Patterns = new[] { "x" }
};
var sut = new MatcherMapper(new WireMockServerSettings { AllowCSharpCodeMatcher = false });
Name = "ExactMatcher",
Patterns = new[] { "x" }
};
// Act
Action action = () => sut.Map(model);
// Act
var matcher = (ExactMatcher)_sut.Map(model)!;
// Assert
action.Should().Throw<NotSupportedException>();
}
// Assert
matcher.GetPatterns().Should().ContainSingle("x");
matcher.ThrowException.Should().BeFalse();
}
[Fact]
public void MatcherModelMapper_Map_Null()
[Fact]
public void MatcherModelMapper_Map_ExactMatcher_Patterns()
{
// Assign
var model = new MatcherModel
{
// Act
IMatcher matcher = _sut.Map((MatcherModel)null);
Name = "ExactMatcher",
Patterns = new[] { "x", "y" }
};
// Assert
Check.That(matcher).IsNull();
}
// Act
var matcher = (ExactMatcher)_sut.Map(model)!;
[Fact]
public void MatcherModelMapper_Map_ExactMatcher_Pattern()
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
}
[Fact]
public void MatcherModelMapper_Map_JsonPartialMatcher_RegexFalse()
{
// Assign
var pattern = "{ \"x\": 1 }";
var model = new MatcherModel
{
// Assign
var model = new MatcherModel
{
Name = "ExactMatcher",
Patterns = new[] { "x" }
};
Name = "JsonPartialMatcher",
Regex = false,
Pattern = pattern
};
// Act
var matcher = (ExactMatcher)_sut.Map(model);
// Act
var matcher = (JsonPartialMatcher)_sut.Map(model)!;
// Assert
matcher.GetPatterns().Should().ContainSingle("x");
matcher.ThrowException.Should().BeFalse();
}
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
matcher.IgnoreCase.Should().BeFalse();
matcher.Value.Should().Be(pattern);
matcher.Regex.Should().BeFalse();
matcher.ThrowException.Should().BeFalse();
}
[Fact]
public void MatcherModelMapper_Map_ExactMatcher_Patterns()
[Fact]
public void MatcherModelMapper_Map_JsonPartialMatcher_RegexTrue()
{
// Assign
var pattern = "{ \"x\": 1 }";
var model = new MatcherModel
{
// Assign
var model = new MatcherModel
{
Name = "ExactMatcher",
Patterns = new[] { "x", "y" }
};
Name = "JsonPartialMatcher",
Regex = true,
Pattern = pattern
};
// Act
var matcher = (ExactMatcher)_sut.Map(model);
// Act
var matcher = (JsonPartialMatcher)_sut.Map(model)!;
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
}
// Assert
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
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(JsonPathMatcher))]
[InlineData(nameof(JmesPathMatcher))]
[InlineData(nameof(XPathMatcher))]
[InlineData(nameof(WildcardMatcher))]
[InlineData(nameof(ContentTypeMatcher))]
[InlineData(nameof(SimMetricsMatcher))]
public void MatcherModelMapper_Map_ThrowExceptionWhenMatcherFails_True(string name)
[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
{
// 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.ThrowException.Should().BeTrue();
}
[Fact]
public void MatcherModelMapper_Map_ExactObjectMatcher_ValidBase64StringPattern()
ThrowExceptionWhenMatcherFails = true
};
var sut = new MatcherMapper(settings);
var model = new MatcherModel
{
// Assign
var model = new MatcherModel
{
Name = "ExactObjectMatcher",
Patterns = new object[] { "c3RlZg==" }
};
Name = name,
Patterns = new[] { "" }
};
// Act
var matcher = (ExactObjectMatcher)_sut.Map(model);
// Act
var matcher = sut.Map(model)!;
// Assert
Check.That(matcher.ValueAsBytes).ContainsExactly(new byte[] { 115, 116, 101, 102 });
}
// Assert
matcher.Should().NotBeNull();
matcher.ThrowException.Should().BeTrue();
}
[Fact]
public void MatcherModelMapper_Map_ExactObjectMatcher_InvalidBase64StringPattern()
[Fact]
public void MatcherModelMapper_Map_ExactObjectMatcher_ValidBase64StringPattern()
{
// Assign
var model = new MatcherModel
{
// Assign
var model = new MatcherModel
{
Name = "ExactObjectMatcher",
Patterns = new object[] { "_" }
};
Name = "ExactObjectMatcher",
Patterns = new object[] { "c3RlZg==" }
};
// Act & Assert
Check.ThatCode(() => _sut.Map(model)).Throws<ArgumentException>();
}
// Act
var matcher = (ExactObjectMatcher)_sut.Map(model)!;
[Theory]
[InlineData(MatchOperator.Or, 1.0d)]
[InlineData(MatchOperator.And, 0.0d)]
[InlineData(MatchOperator.Average, 0.5d)]
public void MatcherModelMapper_Map_RegexMatcher(MatchOperator matchOperator, double expected)
// Assert
Check.That(matcher.ValueAsBytes).ContainsExactly(new byte[] { 115, 116, 101, 102 });
}
[Fact]
public void MatcherModelMapper_Map_ExactObjectMatcher_InvalidBase64StringPattern()
{
// Assign
var model = new MatcherModel
{
// Assign
var model = new MatcherModel
{
Name = "RegexMatcher",
Patterns = new[] { "x", "y" },
IgnoreCase = true,
MatchOperator = matchOperator.ToString()
};
Name = "ExactObjectMatcher",
Patterns = new object[] { "_" }
};
// Act
var matcher = (RegexMatcher)_sut.Map(model)!;
// Act & Assert
Check.ThatCode(() => _sut.Map(model)).Throws<ArgumentException>();
}
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
Check.That(matcher.IsMatch("X")).IsEqualTo(expected);
}
[Theory]
[InlineData(MatchOperator.Or, 1.0d)]
[InlineData(MatchOperator.And, 0.0d)]
[InlineData(MatchOperator.Average, 0.5d)]
public void MatcherModelMapper_Map_WildcardMatcher_IgnoreCase(MatchOperator matchOperator, double expected)
[Theory]
[InlineData(MatchOperator.Or, 1.0d)]
[InlineData(MatchOperator.And, 0.0d)]
[InlineData(MatchOperator.Average, 0.5d)]
public void MatcherModelMapper_Map_RegexMatcher(MatchOperator matchOperator, double expected)
{
// Assign
var model = new MatcherModel
{
// Assign
var model = new MatcherModel
{
Name = "WildcardMatcher",
Patterns = new[] { "x", "y" },
IgnoreCase = true,
MatchOperator = matchOperator.ToString()
};
Name = "RegexMatcher",
Patterns = new[] { "x", "y" },
IgnoreCase = true,
MatchOperator = matchOperator.ToString()
};
// Act
var matcher = (WildcardMatcher)_sut.Map(model)!;
// Act
var matcher = (RegexMatcher)_sut.Map(model)!;
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
Check.That(matcher.IsMatch("X")).IsEqualTo(expected);
}
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
Check.That(matcher.IsMatch("X")).IsEqualTo(expected);
}
[Fact]
public void MatcherModelMapper_Map_WildcardMatcher_With_PatternAsFile()
[Theory]
[InlineData(MatchOperator.Or, 1.0d)]
[InlineData(MatchOperator.And, 0.0d)]
[InlineData(MatchOperator.Average, 0.5d)]
public void MatcherModelMapper_Map_WildcardMatcher_IgnoreCase(MatchOperator matchOperator, double expected)
{
// Assign
var model = new MatcherModel
{
// 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);
Name = "WildcardMatcher",
Patterns = new[] { "x", "y" },
IgnoreCase = true,
MatchOperator = matchOperator.ToString()
};
var model = new MatcherModel
{
Name = "WildcardMatcher",
PatternAsFile = file
};
// Act
var matcher = (WildcardMatcher)_sut.Map(model)!;
var settings = new WireMockServerSettings
{
FileSystemHandler = fileSystemHandleMock.Object
};
var sut = new MatcherMapper(settings);
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
Check.That(matcher.IsMatch("X")).IsEqualTo(expected);
}
// 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()
[Fact]
public void MatcherModelMapper_Map_WildcardMatcher_With_PatternAsFile()
{
// Arrange
var file = "c:\\test.txt";
var fileContent = "c";
var stringPattern = new StringPattern
{
// Assign
var model = new MatcherModel
{
Name = "SimMetricsMatcher",
Pattern = "x"
};
Pattern = fileContent,
PatternAsFile = file
};
var fileSystemHandleMock = new Mock<IFileSystemHandler>();
fileSystemHandleMock.Setup(f => f.ReadFileAsString(file)).Returns(fileContent);
// Act
var matcher = (SimMetricsMatcher)_sut.Map(model);
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x");
}
[Fact]
public void MatcherModelMapper_Map_SimMetricsMatcher_BlockDistance()
var model = new MatcherModel
{
// Assign
var model = new MatcherModel
{
Name = "SimMetricsMatcher.BlockDistance",
Pattern = "x"
};
Name = "WildcardMatcher",
PatternAsFile = file
};
// Act
var matcher = (SimMetricsMatcher)_sut.Map(model);
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x");
}
[Fact]
public void MatcherModelMapper_Map_SimMetricsMatcher_Throws1()
var settings = new WireMockServerSettings
{
// Assign
var model = new MatcherModel
{
Name = "error",
Pattern = "x"
};
FileSystemHandler = fileSystemHandleMock.Object
};
var sut = new MatcherMapper(settings);
// Act
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
}
// Act
var matcher = (WildcardMatcher)sut.Map(model)!;
[Fact]
public void MatcherModelMapper_Map_SimMetricsMatcher_Throws2()
// 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()
{
// Assign
var model = new MatcherModel
{
// Assign
var model = new MatcherModel
{
Name = "SimMetricsMatcher.error",
Pattern = "x"
};
Name = "SimMetricsMatcher",
Pattern = "x"
};
// Act
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
}
// Act
var matcher = (SimMetricsMatcher)_sut.Map(model)!;
[Fact]
public void MatcherModelMapper_Map_MatcherModelToCustomMatcher()
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x");
}
[Fact]
public void MatcherModelMapper_Map_SimMetricsMatcher_BlockDistance()
{
// Assign
var model = new MatcherModel
{
// Arrange
var patternModel = new CustomPathParamMatcherModel("/customer/{customerId}/document/{documentId}",
new Dictionary<string, string>(2)
{
{ "customerId", @"^[0-9]+$" },
{ "documentId", @"^[0-9a-zA-Z\-\_]+\.[a-zA-Z]+$" }
});
var model = new MatcherModel
{
Name = nameof(CustomPathParamMatcher),
Pattern = JsonConvert.SerializeObject(patternModel)
};
Name = "SimMetricsMatcher.BlockDistance",
Pattern = "x"
};
var settings = new WireMockServerSettings();
settings.CustomMatcherMappings = settings.CustomMatcherMappings ?? new Dictionary<string, Func<MatcherModel, IMatcher>>();
settings.CustomMatcherMappings[nameof(CustomPathParamMatcher)] = matcherModel =>
{
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
);
};
var sut = new MatcherMapper(settings);
// Act
var matcher = (SimMetricsMatcher)_sut.Map(model)!;
// Act
var matcher = sut.Map(model) as CustomPathParamMatcher;
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x");
}
// Assert
matcher.Should().NotBeNull();
}
[Fact]
public void MatcherModelMapper_Map_CustomMatcherToMatcherModel()
[Fact]
public void MatcherModelMapper_Map_SimMetricsMatcher_Throws1()
{
// Assign
var model = new MatcherModel
{
// Arrange
var matcher = new CustomPathParamMatcher("/customer/{customerId}/document/{documentId}",
new Dictionary<string, string>(2)
{
{ "customerId", @"^[0-9]+$" },
{ "documentId", @"^[0-9a-zA-Z\-\_]+\.[a-zA-Z]+$" }
});
Name = "error",
Pattern = "x"
};
// Act
var model = _sut.Map(matcher);
// Act
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
}
// Assert
using (new AssertionScope())
[Fact]
public void MatcherModelMapper_Map_SimMetricsMatcher_Throws2()
{
// Assign
var model = new MatcherModel
{
Name = "SimMetricsMatcher.error",
Pattern = "x"
};
// Act
Check.ThatCode(() => _sut.Map(model)).Throws<NotSupportedException>();
}
[Fact]
public void MatcherModelMapper_Map_MatcherModelToCustomMatcher()
{
// Arrange
var patternModel = new CustomPathParamMatcherModel("/customer/{customerId}/document/{documentId}",
new Dictionary<string, string>(2)
{
model.Should().NotBeNull();
model.Name.Should().Be(nameof(CustomPathParamMatcher));
{ "customerId", @"^[0-9]+$" },
{ "documentId", @"^[0-9a-zA-Z\-\_]+\.[a-zA-Z]+$" }
});
var model = new MatcherModel
{
Name = nameof(CustomPathParamMatcher),
Pattern = JsonConvert.SerializeObject(patternModel)
};
var matcherParams = JsonConvert.DeserializeObject<CustomPathParamMatcherModel>((string)model.Pattern);
matcherParams.Path.Should().Be("/customer/{customerId}/document/{documentId}");
matcherParams.PathParams.Should().BeEquivalentTo(new Dictionary<string, string>(2)
{
{ "customerId", @"^[0-9]+$" },
{ "documentId", @"^[0-9a-zA-Z\-\_]+\.[a-zA-Z]+$" }
});
}
var settings = new WireMockServerSettings();
settings.CustomMatcherMappings = settings.CustomMatcherMappings ?? new Dictionary<string, Func<MatcherModel, IMatcher>>();
settings.CustomMatcherMappings[nameof(CustomPathParamMatcher)] = matcherModel =>
{
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
);
};
var sut = new MatcherMapper(settings);
// Act
var matcher = sut.Map(model) as CustomPathParamMatcher;
// Assert
matcher.Should().NotBeNull();
}
[Fact]
public void MatcherModelMapper_Map_CustomMatcherToMatcherModel()
{
// Arrange
var matcher = new CustomPathParamMatcher("/customer/{customerId}/document/{documentId}",
new Dictionary<string, string>(2)
{
{ "customerId", @"^[0-9]+$" },
{ "documentId", @"^[0-9a-zA-Z\-\_]+\.[a-zA-Z]+$" }
});
// Act
var model = _sut.Map(matcher)!;
// Assert
using (new AssertionScope())
{
model.Should().NotBeNull();
model.Name.Should().Be(nameof(CustomPathParamMatcher));
var matcherParams = JsonConvert.DeserializeObject<CustomPathParamMatcherModel>((string)model.Pattern!)!;
matcherParams.Path.Should().Be("/customer/{customerId}/document/{documentId}");
matcherParams.PathParams.Should().BeEquivalentTo(new Dictionary<string, string>(2)
{
{ "customerId", @"^[0-9]+$" },
{ "documentId", @"^[0-9a-zA-Z\-\_]+\.[a-zA-Z]+$" }
});
}
}
}