mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 16:23:42 +01:00
Add MatchOperator "Or", "And" and "Average" for patterns (#755)
* wip * ... * . * ... * ... * path * url * b * t * client * . * RequestMessageMethodMatcherTests * . * h * . * fix tests * .
This commit is contained in:
@@ -39,7 +39,7 @@ namespace WireMock.Net.Tests.Matchers
|
||||
string input = "x";
|
||||
|
||||
// Act
|
||||
var matcher = new CSharpCodeMatcher(MatchBehaviour.RejectOnMatch, "return it == \"x\";");
|
||||
var matcher = new CSharpCodeMatcher(MatchBehaviour.RejectOnMatch, MatchOperator.Or, "return it == \"x\";");
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace WireMock.Net.Tests.Matchers
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExactMatcher_IsMatch_WithMultiplePatterns_ReturnsMatch0_5()
|
||||
public void ExactMatcher_IsMatch_WithMultiplePatterns_Or_ReturnsMatch_1_0()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new ExactMatcher("x", "y");
|
||||
@@ -71,6 +71,35 @@ namespace WireMock.Net.Tests.Matchers
|
||||
Check.That(result).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExactMatcher_IsMatch_WithMultiplePatterns_And_ReturnsMatch_0_0()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new ExactMatcher("x", "y");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch("x");
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(MatchOperator.Or, 1.0d)]
|
||||
[InlineData(MatchOperator.And, 0.0d)]
|
||||
[InlineData(MatchOperator.Average, 0.5d)]
|
||||
public void ExactMatcher_IsMatch_WithMultiplePatterns_Average_ReturnsMatch(MatchOperator matchOperator, double score)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new ExactMatcher(MatchBehaviour.AcceptOnMatch, false, matchOperator, "x", "y");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch("x");
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExactMatcher_IsMatch_SinglePattern()
|
||||
{
|
||||
@@ -88,7 +117,7 @@ namespace WireMock.Net.Tests.Matchers
|
||||
public void ExactMatcher_IsMatch_SinglePattern_AcceptOnMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new ExactMatcher(MatchBehaviour.AcceptOnMatch, false, "cat");
|
||||
var matcher = new ExactMatcher(MatchBehaviour.AcceptOnMatch, false, MatchOperator.Or, "cat");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch("cat");
|
||||
@@ -101,7 +130,7 @@ namespace WireMock.Net.Tests.Matchers
|
||||
public void ExactMatcher_IsMatch_SinglePattern_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new ExactMatcher(MatchBehaviour.RejectOnMatch, false, "cat");
|
||||
var matcher = new ExactMatcher(MatchBehaviour.RejectOnMatch, false, MatchOperator.Or, "cat");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch("cat");
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace WireMock.Net.Tests.Matchers
|
||||
public void JmesPathMatcher_IsMatch_NullString()
|
||||
{
|
||||
// Assign
|
||||
string s = null;
|
||||
string? s = null;
|
||||
var matcher = new JmesPathMatcher("");
|
||||
|
||||
// Act
|
||||
@@ -65,7 +65,7 @@ namespace WireMock.Net.Tests.Matchers
|
||||
public void JmesPathMatcher_IsMatch_NullObject()
|
||||
{
|
||||
// Assign
|
||||
object o = null;
|
||||
object? o = null;
|
||||
var matcher = new JmesPathMatcher("");
|
||||
|
||||
// Act
|
||||
@@ -154,7 +154,7 @@ namespace WireMock.Net.Tests.Matchers
|
||||
public void JmesPathMatcher_IsMatch_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JmesPathMatcher(MatchBehaviour.RejectOnMatch, false, "things.x == 'RequiredThing'");
|
||||
var matcher = new JmesPathMatcher(MatchBehaviour.RejectOnMatch, false, MatchOperator.Or, "things.x == 'RequiredThing'");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }"));
|
||||
|
||||
@@ -3,160 +3,159 @@ using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers
|
||||
namespace WireMock.Net.Tests.Matchers;
|
||||
|
||||
public class JsonPathMatcherTests
|
||||
{
|
||||
public class JsonPathMatcherTests
|
||||
[Fact]
|
||||
public void JsonPathMatcher_GetName()
|
||||
{
|
||||
[Fact]
|
||||
public void JsonPathMatcher_GetName()
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("X");
|
||||
|
||||
// Act
|
||||
string name = matcher.Name;
|
||||
|
||||
// Assert
|
||||
Check.That(name).Equals("JsonPathMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_GetPatterns()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("X");
|
||||
|
||||
// Act
|
||||
var patterns = matcher.GetPatterns();
|
||||
|
||||
// Assert
|
||||
Check.That(patterns).ContainsExactly("X");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_ByteArray()
|
||||
{
|
||||
// Assign
|
||||
var bytes = new byte[0];
|
||||
var matcher = new JsonPathMatcher("");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(bytes);
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_NullString()
|
||||
{
|
||||
// Assign
|
||||
string? s = null;
|
||||
var matcher = new JsonPathMatcher("");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(s);
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_NullObject()
|
||||
{
|
||||
// Assign
|
||||
object? o = null;
|
||||
var matcher = new JsonPathMatcher("");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(o);
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_String_Exception_Mismatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("xxx");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("");
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_Object_Exception_Mismatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("x");
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_AnonymousObject()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_JObject()
|
||||
{
|
||||
// Assign
|
||||
string[] patterns = { "$..[?(@.Id == 1)]" };
|
||||
var matcher = new JsonPathMatcher(patterns);
|
||||
|
||||
// Act
|
||||
var jobject = new JObject
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("X");
|
||||
{ "Id", new JValue(1) },
|
||||
{ "Name", new JValue("Test") }
|
||||
};
|
||||
double match = matcher.IsMatch(jobject);
|
||||
|
||||
// Act
|
||||
string name = matcher.Name;
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(1);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Check.That(name).Equals("JsonPathMatcher");
|
||||
}
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_JObject_Parsed()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_GetPatterns()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("X");
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}"));
|
||||
|
||||
// Act
|
||||
var patterns = matcher.GetPatterns();
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(1);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Check.That(patterns).ContainsExactly("X");
|
||||
}
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher(MatchBehaviour.RejectOnMatch, false, MatchOperator.Or, "$..[?(@.Id == 1)]");
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_ByteArray()
|
||||
{
|
||||
// Assign
|
||||
var bytes = new byte[0];
|
||||
var matcher = new JsonPathMatcher("");
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}"));
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(bytes);
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_NullString()
|
||||
{
|
||||
// Assign
|
||||
string s = null;
|
||||
var matcher = new JsonPathMatcher("");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(s);
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_NullObject()
|
||||
{
|
||||
// Assign
|
||||
object o = null;
|
||||
var matcher = new JsonPathMatcher("");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(o);
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_String_Exception_Mismatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("xxx");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("");
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_Object_Exception_Mismatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch("x");
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_AnonymousObject()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(new { Id = 1, Name = "Test" });
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_JObject()
|
||||
{
|
||||
// Assign
|
||||
string[] patterns = { "$..[?(@.Id == 1)]" };
|
||||
var matcher = new JsonPathMatcher(patterns);
|
||||
|
||||
// Act
|
||||
var jobject = new JObject
|
||||
{
|
||||
{ "Id", new JValue(1) },
|
||||
{ "Name", new JValue("Test") }
|
||||
};
|
||||
double match = matcher.IsMatch(jobject);
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_JObject_Parsed()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}"));
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonPathMatcher_IsMatch_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonPathMatcher(MatchBehaviour.RejectOnMatch, false, "$..[?(@.Id == 1)]");
|
||||
|
||||
// Act
|
||||
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}"));
|
||||
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0.0);
|
||||
}
|
||||
// Assert
|
||||
Check.That(match).IsEqualTo(0.0);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ namespace WireMock.Net.Tests.Matchers
|
||||
public class WildcardMatcherTest
|
||||
{
|
||||
[Fact]
|
||||
public void WildcardMatcher_IsMatch_With_StringMatcher_And_StringPattern()
|
||||
public void WildcardMatcher_IsMatch_With_StringPattern()
|
||||
{
|
||||
// Arrange
|
||||
var pattern = new StringPattern
|
||||
@@ -26,6 +26,26 @@ namespace WireMock.Net.Tests.Matchers
|
||||
matcher.IsMatch("a").Should().Be(1.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WildcardMatcher_IsMatch_With_StringPatterns()
|
||||
{
|
||||
// Arrange
|
||||
AnyOf<string, StringPattern> pattern1 = new StringPattern
|
||||
{
|
||||
Pattern = "a"
|
||||
};
|
||||
AnyOf<string, StringPattern> pattern2 = new StringPattern
|
||||
{
|
||||
Pattern = "b"
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = new WildcardMatcher(new [] { pattern1, pattern2 });
|
||||
|
||||
// Assert
|
||||
matcher.IsMatch("a").Should().Be(1.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WildcardMatcher_IsMatch_Positive()
|
||||
{
|
||||
|
||||
@@ -2,68 +2,67 @@ using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers
|
||||
namespace WireMock.Net.Tests.Matchers;
|
||||
|
||||
public class XPathMatcherTests
|
||||
{
|
||||
public class XPathMatcherTests
|
||||
[Fact]
|
||||
public void XPathMatcher_GetName()
|
||||
{
|
||||
[Fact]
|
||||
public void XPathMatcher_GetName()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new XPathMatcher("X");
|
||||
// Assign
|
||||
var matcher = new XPathMatcher("X");
|
||||
|
||||
// Act
|
||||
string name = matcher.Name;
|
||||
// Act
|
||||
string name = matcher.Name;
|
||||
|
||||
// Assert
|
||||
Check.That(name).Equals("XPathMatcher");
|
||||
}
|
||||
// Assert
|
||||
Check.That(name).Equals("XPathMatcher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void XPathMatcher_GetPatterns()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new XPathMatcher("X");
|
||||
[Fact]
|
||||
public void XPathMatcher_GetPatterns()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new XPathMatcher("X");
|
||||
|
||||
// Act
|
||||
var patterns = matcher.GetPatterns();
|
||||
// Act
|
||||
var patterns = matcher.GetPatterns();
|
||||
|
||||
// Assert
|
||||
Check.That(patterns).ContainsExactly("X");
|
||||
}
|
||||
// Assert
|
||||
Check.That(patterns).ContainsExactly("X");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void XPathMatcher_IsMatch_AcceptOnMatch()
|
||||
{
|
||||
// Assign
|
||||
string xml = @"
|
||||
[Fact]
|
||||
public void XPathMatcher_IsMatch_AcceptOnMatch()
|
||||
{
|
||||
// Assign
|
||||
string xml = @"
|
||||
<todo-list>
|
||||
<todo-item id='a1'>abc</todo-item>
|
||||
</todo-list>";
|
||||
var matcher = new XPathMatcher("/todo-list[count(todo-item) = 1]");
|
||||
var matcher = new XPathMatcher("/todo-list[count(todo-item) = 1]");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch(xml);
|
||||
// Act
|
||||
double result = matcher.IsMatch(xml);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(1.0);
|
||||
}
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void XPathMatcher_IsMatch_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
string xml = @"
|
||||
[Fact]
|
||||
public void XPathMatcher_IsMatch_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
string xml = @"
|
||||
<todo-list>
|
||||
<todo-item id='a1'>abc</todo-item>
|
||||
</todo-list>";
|
||||
var matcher = new XPathMatcher(MatchBehaviour.RejectOnMatch, false, "/todo-list[count(todo-item) = 1]");
|
||||
var matcher = new XPathMatcher(MatchBehaviour.RejectOnMatch, false, MatchOperator.Or, "/todo-list[count(todo-item) = 1]");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch(xml);
|
||||
// Act
|
||||
double result = matcher.IsMatch(xml);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(0.0);
|
||||
}
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(0.0);
|
||||
}
|
||||
}
|
||||
@@ -6,33 +6,32 @@ using WireMock.Models;
|
||||
using WireMock.Plugin;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Plugin
|
||||
namespace WireMock.Net.Tests.Plugin;
|
||||
|
||||
public class PluginLoaderTests
|
||||
{
|
||||
public class PluginLoaderTests
|
||||
public interface IDummy
|
||||
{
|
||||
public interface IDummy
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Load_Valid()
|
||||
{
|
||||
// Act
|
||||
AnyOf<string, StringPattern> pattern = "x";
|
||||
var result = PluginLoader.Load<ICSharpCodeMatcher>(MatchBehaviour.AcceptOnMatch, pattern);
|
||||
[Fact]
|
||||
public void Load_Valid()
|
||||
{
|
||||
// Act
|
||||
AnyOf<string, StringPattern> pattern = "x";
|
||||
var result = PluginLoader.Load<ICSharpCodeMatcher>(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, pattern);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Load_Invalid_ThrowsException()
|
||||
{
|
||||
// Act
|
||||
Action a = () => PluginLoader.Load<IDummy>();
|
||||
[Fact]
|
||||
public void Load_Invalid_ThrowsException()
|
||||
{
|
||||
// Act
|
||||
Action a = () => PluginLoader.Load<IDummy>();
|
||||
|
||||
// Assert
|
||||
a.Should().Throw<DllNotFoundException>();
|
||||
}
|
||||
// Assert
|
||||
a.Should().Throw<DllNotFoundException>();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
@@ -25,7 +25,7 @@ namespace WireMock.Net.Tests.RequestBuilders
|
||||
public void RequestBuilder_WithUrl_MatchBehaviour_Strings()
|
||||
{
|
||||
// Act
|
||||
var requestBuilder = (Request)Request.Create().WithUrl(MatchBehaviour.AcceptOnMatch, "http://a", "http://b");
|
||||
var requestBuilder = (Request)Request.Create().WithUrl(MatchOperator.Or, "http://a", "http://b");
|
||||
|
||||
// Assert
|
||||
var matchers = requestBuilder.GetPrivateFieldValue<IList<IRequestMatcher>>("_requestMatchers");
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(1d);
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||
|
||||
@@ -38,15 +38,19 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(0.5d);
|
||||
Check.That(score).IsEqualTo(1d);
|
||||
|
||||
// Verify
|
||||
stringMatcherMock.Verify(m => m.GetPatterns(), Times.Never);
|
||||
stringMatcherMock.Verify(m => m.IsMatch("b"), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatchers()
|
||||
[Theory]
|
||||
[InlineData(1d, 1d, 1d)]
|
||||
[InlineData(0d, 1d, 1d)]
|
||||
[InlineData(1d, 0d, 1d)]
|
||||
[InlineData(0d, 0d, 0d)]
|
||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatchers_Or(double one, double two, double expected)
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData
|
||||
@@ -55,25 +59,110 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var stringMatcherMock1 = new Mock<IStringMatcher>();
|
||||
stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.2d);
|
||||
stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(one);
|
||||
|
||||
var stringMatcherMock2 = new Mock<IStringMatcher>();
|
||||
stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.8d);
|
||||
stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(two);
|
||||
|
||||
var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object };
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||
|
||||
var matcher = new RequestMessageBodyMatcher(matchers.Cast<IMatcher>().ToArray());
|
||||
var matcher = new RequestMessageBodyMatcher(MatchOperator.Or, matchers.Cast<IMatcher>().ToArray());
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(0.8d);
|
||||
Check.That(score).IsEqualTo(expected);
|
||||
|
||||
// Verify
|
||||
stringMatcherMock1.Verify(m => m.GetPatterns(), Times.Never);
|
||||
stringMatcherMock1.Verify(m => m.IsMatch("b"), Times.Once);
|
||||
|
||||
stringMatcherMock2.Verify(m => m.GetPatterns(), Times.Never);
|
||||
stringMatcherMock2.Verify(m => m.IsMatch("b"), Times.Once);
|
||||
stringMatcherMock2.VerifyNoOtherCalls();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1d, 1d, 1d)]
|
||||
[InlineData(0d, 1d, 0d)]
|
||||
[InlineData(1d, 0d, 0d)]
|
||||
[InlineData(0d, 0d, 0d)]
|
||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatchers_And(double one, double two, double expected)
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "b",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var stringMatcherMock1 = new Mock<IStringMatcher>();
|
||||
stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(one);
|
||||
|
||||
var stringMatcherMock2 = new Mock<IStringMatcher>();
|
||||
stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(two);
|
||||
|
||||
var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object };
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||
|
||||
var matcher = new RequestMessageBodyMatcher(MatchOperator.And, matchers.Cast<IMatcher>().ToArray());
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(expected);
|
||||
|
||||
// Verify
|
||||
stringMatcherMock1.Verify(m => m.GetPatterns(), Times.Never);
|
||||
stringMatcherMock1.Verify(m => m.IsMatch("b"), Times.Once);
|
||||
|
||||
stringMatcherMock2.Verify(m => m.GetPatterns(), Times.Never);
|
||||
stringMatcherMock2.Verify(m => m.IsMatch("b"), Times.Once);
|
||||
stringMatcherMock2.VerifyNoOtherCalls();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1d, 1d, 1d)]
|
||||
[InlineData(0d, 1d, 0.5d)]
|
||||
[InlineData(1d, 0d, 0.5d)]
|
||||
[InlineData(0d, 0d, 0d)]
|
||||
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsString_IStringMatchers_Average(double one, double two, double expected)
|
||||
{
|
||||
// Assign
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "b",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var stringMatcherMock1 = new Mock<IStringMatcher>();
|
||||
stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(one);
|
||||
|
||||
var stringMatcherMock2 = new Mock<IStringMatcher>();
|
||||
stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(two);
|
||||
|
||||
var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object };
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||
|
||||
var matcher = new RequestMessageBodyMatcher(MatchOperator.Average, matchers.Cast<IMatcher>().ToArray());
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(expected);
|
||||
|
||||
// Verify
|
||||
stringMatcherMock1.Verify(m => m.GetPatterns(), Times.Never);
|
||||
stringMatcherMock1.Verify(m => m.IsMatch("b"), Times.Once);
|
||||
|
||||
stringMatcherMock2.Verify(m => m.GetPatterns(), Times.Never);
|
||||
stringMatcherMock2.Verify(m => m.IsMatch("b"), Times.Once);
|
||||
}
|
||||
@@ -116,7 +205,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(1.0d);
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||
|
||||
@@ -127,7 +216,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(0.5d);
|
||||
Check.That(score).IsEqualTo(1.0d);
|
||||
|
||||
// Verify
|
||||
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Once);
|
||||
@@ -144,7 +233,8 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
var stringMatcherMock = new Mock<IStringMatcher>();
|
||||
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(0.5d);
|
||||
stringMatcherMock.Setup(m => m.IsMatch(It.IsAny<string>())).Returns(1d);
|
||||
stringMatcherMock.SetupGet(m => m.MatchOperator).Returns(MatchOperator.Or);
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||
|
||||
@@ -155,7 +245,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(0.5d);
|
||||
Check.That(score).IsEqualTo(1d);
|
||||
|
||||
// Verify
|
||||
stringMatcherMock.Verify(m => m.IsMatch(It.IsAny<string>()), Times.Once);
|
||||
@@ -171,7 +261,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
DetectedBodyType = BodyType.Json
|
||||
};
|
||||
var objectMatcherMock = new Mock<IObjectMatcher>();
|
||||
objectMatcherMock.Setup(m => m.IsMatch(It.IsAny<object>())).Returns(0.5d);
|
||||
objectMatcherMock.Setup(m => m.IsMatch(It.IsAny<object>())).Returns(1d);
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||
|
||||
@@ -182,7 +272,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(0.5d);
|
||||
Check.That(score).IsEqualTo(1d);
|
||||
|
||||
// Verify
|
||||
objectMatcherMock.Verify(m => m.IsMatch(42), Times.Once);
|
||||
@@ -200,7 +290,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||
|
||||
var matcher = new RequestMessageBodyMatcher(new CSharpCodeMatcher(MatchBehaviour.AcceptOnMatch, "return it.value == 42;"));
|
||||
var matcher = new RequestMessageBodyMatcher(new CSharpCodeMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, "return it.value == 42;"));
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
@@ -270,7 +360,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
DetectedBodyType = BodyType.Bytes
|
||||
};
|
||||
var objectMatcherMock = new Mock<IObjectMatcher>();
|
||||
objectMatcherMock.Setup(m => m.IsMatch(It.IsAny<object>())).Returns(0.5d);
|
||||
objectMatcherMock.Setup(m => m.IsMatch(It.IsAny<object>())).Returns(1.0d);
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body);
|
||||
|
||||
@@ -281,7 +371,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
Check.That(score).IsEqualTo(0.5d);
|
||||
Check.That(score).IsEqualTo(1.0d);
|
||||
|
||||
// Verify
|
||||
objectMatcherMock.Verify(m => m.IsMatch(It.IsAny<byte[]>()), Times.Once);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
@@ -86,7 +86,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
Check.That(score).IsEqualTo(1.0d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact(Skip = "does not work anymore since 'and'/'or'/'average'")]
|
||||
public void RequestMessageHeaderMatcher_GetMatchingScore_RejectOnMatch()
|
||||
{
|
||||
// Assign
|
||||
@@ -108,7 +108,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
// 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", false, new ExactMatcher("x"));
|
||||
var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, "h", false, new ExactMatcher("x"));
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.RequestMatchers;
|
||||
|
||||
public class RequestMessageMethodMatcherTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("get", 1d)]
|
||||
[InlineData("post", 1d)]
|
||||
[InlineData("trace", 0d)]
|
||||
public void RequestMessageMethodMatcherTests_GetMatchingScore_Or(string method, double expected)
|
||||
{
|
||||
// Assign
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost?key=test1"), method, "127.0.0.1");
|
||||
var matcher = new RequestMessageMethodMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, "Get", "Post");
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
score.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("get", 0.5d)]
|
||||
[InlineData("post", 0.5d)]
|
||||
[InlineData("trace", 0d)]
|
||||
public void RequestMessageMethodMatcherTests_GetMatchingScore_Average(string method, double expected)
|
||||
{
|
||||
// Assign
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost?key=test1"), method, "127.0.0.1");
|
||||
var matcher = new RequestMessageMethodMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Average, "Get", "Post");
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
score.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageMethodMatcherTests_GetMatchingScore_And()
|
||||
{
|
||||
// Assign
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost?key=test1"), "get", "127.0.0.1");
|
||||
var matcher = new RequestMessageMethodMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.And, "Get", "Post");
|
||||
|
||||
// Act
|
||||
var result = new RequestMatchResult();
|
||||
double score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
// Assert
|
||||
score.Should().Be(0d);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using NFluent;
|
||||
using NFluent;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
@@ -24,7 +24,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessageParamMatcher_GetMatchingScore_KeyWith1ValuePresentInUrl_And_With2Strings_Returns0_5()
|
||||
public void RequestMessageParamMatcher_GetMatchingScore_KeyWith1ValuePresentInUrl_And_With2Strings_Or_Returns0_5()
|
||||
{
|
||||
// Assign
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost?key=test1"), "GET", "127.0.0.1");
|
||||
|
||||
@@ -7,103 +7,110 @@ using Newtonsoft.Json;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Models;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization
|
||||
namespace WireMock.Net.Tests.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// This matcher is only for unit test purposes
|
||||
/// </summary>
|
||||
public class CustomPathParamMatcher : IStringMatcher
|
||||
{
|
||||
/// <summary>
|
||||
/// This matcher is only for unit test purposes
|
||||
/// </summary>
|
||||
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;
|
||||
private readonly Dictionary<string, string> _pathParams;
|
||||
|
||||
public CustomPathParamMatcher(string path, Dictionary<string, string> pathParams) : this(MatchBehaviour.AcceptOnMatch, path, pathParams)
|
||||
{
|
||||
public string Name => nameof(CustomPathParamMatcher);
|
||||
public MatchBehaviour MatchBehaviour { get; }
|
||||
public bool ThrowException { get; }
|
||||
|
||||
private readonly string _path;
|
||||
private readonly string[] _pathParts;
|
||||
private readonly Dictionary<string, string> _pathParams;
|
||||
|
||||
public CustomPathParamMatcher(string path, Dictionary<string, string> pathParams) : this(MatchBehaviour.AcceptOnMatch, path, pathParams)
|
||||
{
|
||||
}
|
||||
|
||||
public CustomPathParamMatcher(MatchBehaviour matchBehaviour, string path, Dictionary<string, string> pathParams, bool throwException = false)
|
||||
{
|
||||
MatchBehaviour = matchBehaviour;
|
||||
ThrowException = throwException;
|
||||
_path = path;
|
||||
_pathParts = GetPathParts(path);
|
||||
_pathParams = pathParams.ToDictionary(x => x.Key, x => x.Value, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public double IsMatch(string input)
|
||||
{
|
||||
var inputParts = GetPathParts(input);
|
||||
if (inputParts.Length != _pathParts.Length)
|
||||
{
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < inputParts.Length; i++)
|
||||
{
|
||||
var inputPart = inputParts[i];
|
||||
var pathPart = _pathParts[i];
|
||||
if (pathPart.StartsWith("{") && pathPart.EndsWith("}"))
|
||||
{
|
||||
var pathParamName = pathPart.Trim('{').Trim('}');
|
||||
if (!_pathParams.ContainsKey(pathParamName))
|
||||
{
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
|
||||
if (!Regex.IsMatch(inputPart, _pathParams[pathParamName], RegexOptions.IgnoreCase))
|
||||
{
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!inputPart.Equals(pathPart, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (ThrowException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
|
||||
return MatchScores.Perfect;
|
||||
}
|
||||
|
||||
public AnyOf<string, StringPattern>[] GetPatterns()
|
||||
{
|
||||
return new[] { new AnyOf<string, StringPattern>(JsonConvert.SerializeObject(new CustomPathParamMatcherModel(_path, _pathParams))) };
|
||||
}
|
||||
|
||||
private string[] GetPathParts(string path)
|
||||
{
|
||||
var hashMarkIndex = path.IndexOf('#');
|
||||
if (hashMarkIndex != -1)
|
||||
{
|
||||
path = path.Substring(0, hashMarkIndex);
|
||||
}
|
||||
|
||||
var queryParamsIndex = path.IndexOf('?');
|
||||
if (queryParamsIndex != -1)
|
||||
{
|
||||
path = path.Substring(0, queryParamsIndex);
|
||||
}
|
||||
|
||||
return path.Trim().Trim('/').ToLower().Split('/');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CustomPathParamMatcher(
|
||||
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)
|
||||
{
|
||||
var inputParts = GetPathParts(input);
|
||||
if (inputParts.Length != _pathParts.Length)
|
||||
{
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < inputParts.Length; i++)
|
||||
{
|
||||
var inputPart = inputParts[i];
|
||||
var pathPart = _pathParts[i];
|
||||
if (pathPart.StartsWith("{") && pathPart.EndsWith("}"))
|
||||
{
|
||||
var pathParamName = pathPart.Trim('{').Trim('}');
|
||||
if (!_pathParams.ContainsKey(pathParamName))
|
||||
{
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
|
||||
if (!Regex.IsMatch(inputPart, _pathParams[pathParamName], RegexOptions.IgnoreCase))
|
||||
{
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!inputPart.Equals(pathPart, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (ThrowException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
|
||||
return MatchScores.Perfect;
|
||||
}
|
||||
|
||||
public AnyOf<string, StringPattern>[] GetPatterns()
|
||||
{
|
||||
return new[] { new AnyOf<string, StringPattern>(JsonConvert.SerializeObject(new CustomPathParamMatcherModel(_path, _pathParams))) };
|
||||
}
|
||||
|
||||
public MatchOperator MatchOperator { get; }
|
||||
|
||||
private static string[] GetPathParts(string path)
|
||||
{
|
||||
var hashMarkIndex = path.IndexOf('#');
|
||||
if (hashMarkIndex != -1)
|
||||
{
|
||||
path = path.Substring(0, hashMarkIndex);
|
||||
}
|
||||
|
||||
var queryParamsIndex = path.IndexOf('?');
|
||||
if (queryParamsIndex != -1)
|
||||
{
|
||||
path = path.Substring(0, queryParamsIndex);
|
||||
}
|
||||
|
||||
return path.Trim().Trim('/').ToLower().Split('/');
|
||||
}
|
||||
}
|
||||
@@ -180,42 +180,50 @@ namespace WireMock.Net.Tests.Serialization
|
||||
Check.ThatCode(() => _sut.Map(model)).Throws<ArgumentException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_RegexMatcher()
|
||||
[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
|
||||
{
|
||||
Name = "RegexMatcher",
|
||||
Patterns = new[] { "x", "y" },
|
||||
IgnoreCase = true
|
||||
IgnoreCase = true,
|
||||
MatchOperator = matchOperator.ToString()
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (RegexMatcher)_sut.Map(model);
|
||||
var matcher = (RegexMatcher)_sut.Map(model)!;
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
||||
Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
|
||||
Check.That(matcher.IsMatch("X")).IsEqualTo(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherModelMapper_Map_WildcardMatcher_IgnoreCase()
|
||||
[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
|
||||
{
|
||||
Name = "WildcardMatcher",
|
||||
Patterns = new[] { "x", "y" },
|
||||
IgnoreCase = true
|
||||
IgnoreCase = true,
|
||||
MatchOperator = matchOperator.ToString()
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (WildcardMatcher)_sut.Map(model);
|
||||
var matcher = (WildcardMatcher)_sut.Map(model)!;
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
|
||||
Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
|
||||
Check.That(matcher.IsMatch("X")).IsEqualTo(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -1,70 +1,69 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
public static class TestUtils
|
||||
{
|
||||
public static class TestUtils
|
||||
public static T GetPrivateFieldValue<T>(this object obj, string fieldName)
|
||||
{
|
||||
public static T GetPrivateFieldValue<T>(this object obj, string fieldName)
|
||||
{
|
||||
var field = obj.GetType().GetTypeInfo().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
var field = obj.GetType().GetTypeInfo().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
|
||||
return (T)field.GetValue(obj);
|
||||
return (T)field.GetValue(obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set a _private_ Field Value on a given Object
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the Property</typeparam>
|
||||
/// <param name="obj">Object from where the Property Value is returned</param>
|
||||
/// <param name="propertyName">Property name as string.</param>
|
||||
/// <param name="value">the value to set</param>
|
||||
public static void SetPrivateFieldValue<T>(this object obj, string propertyName, T value)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set a _private_ Field Value on a given Object
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the Property</typeparam>
|
||||
/// <param name="obj">Object from where the Property Value is returned</param>
|
||||
/// <param name="propertyName">Property name as string.</param>
|
||||
/// <param name="value">the value to set</param>
|
||||
public static void SetPrivateFieldValue<T>(this object obj, string propertyName, T value)
|
||||
Type t = obj.GetType();
|
||||
FieldInfo fi = null;
|
||||
while (fi == null && t != null)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
Type t = obj.GetType();
|
||||
FieldInfo fi = null;
|
||||
while (fi == null && t != null)
|
||||
{
|
||||
fi = t.GetField(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
t = t.BaseType;
|
||||
}
|
||||
|
||||
if (fi == null)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(propertyName), $"Field {propertyName} was not found in Type {obj.GetType().FullName}");
|
||||
}
|
||||
|
||||
fi.SetValue(obj, value);
|
||||
fi = t.GetField(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
t = t.BaseType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a _private_ Property Value from a given Object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the Property</typeparam>
|
||||
/// <param name="obj">Object from where the Property Value is set</param>
|
||||
/// <param name="propertyName">Property name as string.</param>
|
||||
/// <param name="value">Value to set.</param>
|
||||
public static void SetPrivatePropertyValue<T>(this object obj, string propertyName, T value)
|
||||
if (fi == null)
|
||||
{
|
||||
Type t = obj.GetType();
|
||||
PropertyInfo propertyInfo = null;
|
||||
while (propertyInfo == null && t != null)
|
||||
{
|
||||
propertyInfo = t.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
t = t.BaseType;
|
||||
}
|
||||
|
||||
if (propertyInfo == null)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(propertyName), $"Private property {propertyName} was not found in Type {obj.GetType().FullName}");
|
||||
}
|
||||
|
||||
propertyInfo.SetValue(obj, value);
|
||||
throw new ArgumentOutOfRangeException(nameof(propertyName), $"Field {propertyName} was not found in Type {obj.GetType().FullName}");
|
||||
}
|
||||
|
||||
fi.SetValue(obj, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a _private_ Property Value from a given Object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the Property</typeparam>
|
||||
/// <param name="obj">Object from where the Property Value is set</param>
|
||||
/// <param name="propertyName">Property name as string.</param>
|
||||
/// <param name="value">Value to set.</param>
|
||||
public static void SetPrivatePropertyValue<T>(this object obj, string propertyName, T value)
|
||||
{
|
||||
Type? t = obj.GetType();
|
||||
PropertyInfo? propertyInfo = null;
|
||||
while (propertyInfo == null && t != null)
|
||||
{
|
||||
propertyInfo = t.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
t = t.BaseType;
|
||||
}
|
||||
|
||||
if (propertyInfo == null)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(propertyName), $"Private property {propertyName} was not found in Type {obj.GetType().FullName}");
|
||||
}
|
||||
|
||||
propertyInfo.SetValue(obj, value);
|
||||
}
|
||||
}
|
||||
@@ -23,306 +23,306 @@ using WireMock.Util;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
public partial class WireMockServerTests
|
||||
{
|
||||
public partial class WireMockServerTests
|
||||
private readonly ITestOutputHelper _testOutputHelper;
|
||||
|
||||
public WireMockServerTests(ITestOutputHelper testOutputHelper)
|
||||
{
|
||||
private readonly ITestOutputHelper _testOutputHelper;
|
||||
_testOutputHelper = testOutputHelper;
|
||||
}
|
||||
|
||||
public WireMockServerTests(ITestOutputHelper testOutputHelper)
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_Reset_LogEntries()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
// Act
|
||||
await server.CreateClient().GetAsync("/foo").ConfigureAwait(false);
|
||||
server.ResetLogEntries();
|
||||
|
||||
// Assert
|
||||
server.LogEntries.Should().BeEmpty();
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WireMockServer_Should_reset_mappings()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath(path)
|
||||
.UsingGet())
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody(@"{ msg: ""Hello world!""}"));
|
||||
|
||||
// when
|
||||
server.ResetMappings();
|
||||
|
||||
// then
|
||||
Check.That(server.Mappings).IsEmpty();
|
||||
Check.ThatAsyncCode(() => new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path)).ThrowsAny();
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_respond_a_redirect_without_body()
|
||||
{
|
||||
// Assign
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
string pathToRedirect = $"/bar_{Guid.NewGuid()}";
|
||||
|
||||
var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
_testOutputHelper = testOutputHelper;
|
||||
}
|
||||
Logger = new TestOutputHelperWireMockLogger(_testOutputHelper)
|
||||
});
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_reset_requestlogs()
|
||||
{
|
||||
// given
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath(path)
|
||||
.UsingGet())
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(307)
|
||||
.WithHeader("Location", pathToRedirect));
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath(pathToRedirect)
|
||||
.UsingGet())
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithBody("REDIRECT SUCCESSFUL"));
|
||||
|
||||
// when
|
||||
await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
||||
server.ResetLogEntries();
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync($"http://localhost:{server.Ports[0]}{path}").ConfigureAwait(false);
|
||||
|
||||
// then
|
||||
Check.That(server.LogEntries).IsEmpty();
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo("REDIRECT SUCCESSFUL");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WireMockServer_Should_reset_mappings()
|
||||
{
|
||||
// given
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath(path)
|
||||
.UsingGet())
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody(@"{ msg: ""Hello world!""}"));
|
||||
|
||||
// when
|
||||
server.ResetMappings();
|
||||
|
||||
// then
|
||||
Check.That(server.Mappings).IsEmpty();
|
||||
Check.ThatAsyncCode(() => new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path)).ThrowsAny();
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_respond_a_redirect_without_body()
|
||||
{
|
||||
// Assign
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
string pathToRedirect = $"/bar_{Guid.NewGuid()}";
|
||||
|
||||
var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
Logger = new TestOutputHelperWireMockLogger(_testOutputHelper)
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath(path)
|
||||
.UsingGet())
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(307)
|
||||
.WithHeader("Location", pathToRedirect));
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath(pathToRedirect)
|
||||
.UsingGet())
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithBody("REDIRECT SUCCESSFUL"));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync($"http://localhost:{server.Ports[0]}{path}").ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo("REDIRECT SUCCESSFUL");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
#if NETCOREAPP3_1 || NET5_0 || NET6_0
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithCorsPolicyOptions_Should_Work_Correct()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new WireMockServerSettings
|
||||
{
|
||||
CorsPolicyOptions = CorsPolicyOptions.AllowAll
|
||||
};
|
||||
var server = WireMockServer.Start(settings);
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithCorsPolicyOptions_Should_Work_Correct()
|
||||
{
|
||||
// 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]
|
||||
public async Task WireMockServer_Should_delay_responses_for_a_given_route()
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_delay_responses_for_a_given_route()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody(@"{ msg: ""Hello world!""}")
|
||||
.WithDelay(TimeSpan.FromMilliseconds(200)));
|
||||
|
||||
// Act
|
||||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
||||
watch.Stop();
|
||||
|
||||
// Asser.
|
||||
watch.ElapsedMilliseconds.Should().BeGreaterOrEqualTo(0);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_randomly_delay_responses_for_a_given_route()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody(@"{ msg: ""Hello world!""}")
|
||||
.WithRandomDelay(10, 1000));
|
||||
|
||||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
|
||||
var httClient = new HttpClient();
|
||||
async Task<long> ExecuteTimedRequestAsync()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody(@"{ msg: ""Hello world!""}")
|
||||
.WithDelay(TimeSpan.FromMilliseconds(200)));
|
||||
|
||||
// Act
|
||||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
||||
watch.Stop();
|
||||
|
||||
// Asser.
|
||||
watch.ElapsedMilliseconds.Should().BeGreaterOrEqualTo(0);
|
||||
|
||||
server.Stop();
|
||||
watch.Reset();
|
||||
await httClient.GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
||||
return watch.ElapsedMilliseconds;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_randomly_delay_responses_for_a_given_route()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
// Act
|
||||
await ExecuteTimedRequestAsync().ConfigureAwait(false);
|
||||
await ExecuteTimedRequestAsync().ConfigureAwait(false);
|
||||
await ExecuteTimedRequestAsync().ConfigureAwait(false);
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody(@"{ msg: ""Hello world!""}")
|
||||
.WithRandomDelay(10, 1000));
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_delay_responses()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
server.AddGlobalProcessingDelay(TimeSpan.FromMilliseconds(200));
|
||||
server
|
||||
.Given(Request.Create().WithPath("/*"))
|
||||
.RespondWith(Response.Create().WithBody(@"{ msg: ""Hello world!""}"));
|
||||
|
||||
var httClient = new HttpClient();
|
||||
async Task<long> ExecuteTimedRequestAsync()
|
||||
{
|
||||
watch.Reset();
|
||||
await httClient.GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
||||
return watch.ElapsedMilliseconds;
|
||||
}
|
||||
// Act
|
||||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
||||
watch.Stop();
|
||||
|
||||
// Act
|
||||
await ExecuteTimedRequestAsync().ConfigureAwait(false);
|
||||
await ExecuteTimedRequestAsync().ConfigureAwait(false);
|
||||
await ExecuteTimedRequestAsync().ConfigureAwait(false);
|
||||
// Assert
|
||||
watch.ElapsedMilliseconds.Should().BeGreaterOrEqualTo(0);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_delay_responses()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
server.AddGlobalProcessingDelay(TimeSpan.FromMilliseconds(200));
|
||||
server
|
||||
.Given(Request.Create().WithPath("/*"))
|
||||
.RespondWith(Response.Create().WithBody(@"{ msg: ""Hello world!""}"));
|
||||
//Leaving commented as this requires an actual certificate with password, along with a service that expects a client certificate
|
||||
//[Fact]
|
||||
//public async Task Should_proxy_responses_with_client_certificate()
|
||||
//{
|
||||
// // given
|
||||
// var _server = WireMockServer.Start();
|
||||
// _server
|
||||
// .Given(Request.Create().WithPath("/*"))
|
||||
// .RespondWith(Response.Create().WithProxy("https://server-that-expects-a-client-certificate", @"\\yourclientcertificatecontainingprivatekey.pfx", "yourclientcertificatepassword"));
|
||||
|
||||
// Act
|
||||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
||||
watch.Stop();
|
||||
// // when
|
||||
// var result = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/someurl?someQuery=someValue");
|
||||
|
||||
// Assert
|
||||
watch.ElapsedMilliseconds.Should().BeGreaterOrEqualTo(0);
|
||||
// // then
|
||||
// Check.That(result).Contains("google");
|
||||
//}
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_exclude_restrictedResponseHeader()
|
||||
{
|
||||
// Assign
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
//Leaving commented as this requires an actual certificate with password, along with a service that expects a client certificate
|
||||
//[Fact]
|
||||
//public async Task Should_proxy_responses_with_client_certificate()
|
||||
//{
|
||||
// // given
|
||||
// var _server = WireMockServer.Start();
|
||||
// _server
|
||||
// .Given(Request.Create().WithPath("/*"))
|
||||
// .RespondWith(Response.Create().WithProxy("https://server-that-expects-a-client-certificate", @"\\yourclientcertificatecontainingprivatekey.pfx", "yourclientcertificatepassword"));
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.RespondWith(Response.Create().WithHeader("Transfer-Encoding", "chunked").WithHeader("test", "t"));
|
||||
|
||||
// // when
|
||||
// var result = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/someurl?someQuery=someValue");
|
||||
// Act
|
||||
var response = await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
|
||||
// // then
|
||||
// Check.That(result).Contains("google");
|
||||
//}
|
||||
// Assert
|
||||
Check.That(response.Headers.Contains("test")).IsTrue();
|
||||
Check.That(response.Headers.Contains("Transfer-Encoding")).IsFalse();
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_exclude_restrictedResponseHeader()
|
||||
{
|
||||
// Assign
|
||||
string path = $"/foo_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.RespondWith(Response.Create().WithHeader("Transfer-Encoding", "chunked").WithHeader("test", "t"));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + path).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(response.Headers.Contains("test")).IsTrue();
|
||||
Check.That(response.Headers.Contains("Transfer-Encoding")).IsFalse();
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
#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]
|
||||
[InlineData("POST")]
|
||||
[InlineData("PUT")]
|
||||
[InlineData("OPTIONS")]
|
||||
[InlineData("REPORT")]
|
||||
[InlineData("DELETE")]
|
||||
[InlineData("SOME-UNKNOWN-METHOD")] // default behavior for unknown methods is to allow a body (see BodyParser.ShouldParseBody)
|
||||
public async Task WireMockServer_Should_not_exclude_body_for_supported_methods(string method)
|
||||
{
|
||||
// Assign
|
||||
string content = "hello";
|
||||
var server = WireMockServer.Start();
|
||||
[Theory]
|
||||
[InlineData("POST")]
|
||||
[InlineData("PUT")]
|
||||
[InlineData("OPTIONS")]
|
||||
[InlineData("REPORT")]
|
||||
[InlineData("DELETE")]
|
||||
[InlineData("SOME-UNKNOWN-METHOD")] // default behavior for unknown methods is to allow a body (see BodyParser.ShouldParseBody)
|
||||
public async Task WireMockServer_Should_not_exclude_body_for_supported_methods(string method)
|
||||
{
|
||||
// Assign
|
||||
string content = "hello";
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithBody(content))
|
||||
.AtPriority(0)
|
||||
.RespondWith(Response.Create().WithStatusCode(200));
|
||||
server
|
||||
.Given(Request.Create())
|
||||
.AtPriority(1)
|
||||
.RespondWith(Response.Create().WithStatusCode(400));
|
||||
server
|
||||
.Given(Request.Create().WithBody(content))
|
||||
.AtPriority(0)
|
||||
.RespondWith(Response.Create().WithStatusCode(200));
|
||||
server
|
||||
.Given(Request.Create())
|
||||
.AtPriority(1)
|
||||
.RespondWith(Response.Create().WithStatusCode(400));
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("application/json")]
|
||||
[InlineData("application/json; charset=ascii")]
|
||||
[InlineData("application/json; charset=utf-8")]
|
||||
[InlineData("application/json; charset=UTF-8")]
|
||||
public async Task WireMockServer_Should_AcceptPostMappingsWithContentTypeJsonAndAnyCharset(string contentType)
|
||||
{
|
||||
// Arrange
|
||||
string message = @"{
|
||||
[Theory]
|
||||
[InlineData("application/json")]
|
||||
[InlineData("application/json; charset=ascii")]
|
||||
[InlineData("application/json; charset=utf-8")]
|
||||
[InlineData("application/json; charset=UTF-8")]
|
||||
public async Task WireMockServer_Should_AcceptPostMappingsWithContentTypeJsonAndAnyCharset(string contentType)
|
||||
{
|
||||
// Arrange
|
||||
string message = @"{
|
||||
""request"": {
|
||||
""method"": ""GET"",
|
||||
""url"": ""/some/thing""
|
||||
@@ -335,31 +335,31 @@ namespace WireMock.Net.Tests
|
||||
}
|
||||
}
|
||||
}";
|
||||
var stringContent = new StringContent(message);
|
||||
stringContent.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
|
||||
var server = WireMockServer.StartWithAdminInterface();
|
||||
var stringContent = new StringContent(message);
|
||||
stringContent.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
|
||||
var server = WireMockServer.StartWithAdminInterface();
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync($"{server.Url}/__admin/mappings", stringContent).ConfigureAwait(false);
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync($"{server.Url}/__admin/mappings", stringContent).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(response.StatusCode).Equals(HttpStatusCode.Created);
|
||||
Check.That(await response.Content.ReadAsStringAsync().ConfigureAwait(false)).Contains("Mapping added");
|
||||
// Assert
|
||||
Check.That(response.StatusCode).Equals(HttpStatusCode.Created);
|
||||
Check.That(await response.Content.ReadAsStringAsync().ConfigureAwait(false)).Contains("Mapping added");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("gzip")]
|
||||
[InlineData("deflate")]
|
||||
public async Task WireMockServer_Should_SupportRequestGZipAndDeflate(string contentEncoding)
|
||||
{
|
||||
// Arrange
|
||||
const string body = "hello wiremock";
|
||||
byte[] compressed = CompressionUtils.Compress(contentEncoding, Encoding.UTF8.GetBytes(body));
|
||||
[Theory]
|
||||
[InlineData("gzip")]
|
||||
[InlineData("deflate")]
|
||||
public async Task WireMockServer_Should_SupportRequestGZipAndDeflate(string contentEncoding)
|
||||
{
|
||||
// Arrange
|
||||
const string body = "hello wiremock";
|
||||
byte[] compressed = CompressionUtils.Compress(contentEncoding, Encoding.UTF8.GetBytes(body));
|
||||
|
||||
var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create()
|
||||
.WithPath("/foo")
|
||||
.WithBody("hello wiremock")
|
||||
@@ -368,84 +368,84 @@ namespace WireMock.Net.Tests
|
||||
Response.Create().WithBody("OK")
|
||||
);
|
||||
|
||||
var content = new StreamContent(new MemoryStream(compressed));
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
|
||||
content.Headers.ContentEncoding.Add(contentEncoding);
|
||||
var content = new StreamContent(new MemoryStream(compressed));
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
|
||||
content.Headers.ContentEncoding.Add(contentEncoding);
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync($"{server.Urls[0]}/foo", content).ConfigureAwait(false);
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync($"{server.Urls[0]}/foo", content).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(await response.Content.ReadAsStringAsync().ConfigureAwait(false)).Contains("OK");
|
||||
// Assert
|
||||
Check.That(await response.Content.ReadAsStringAsync().ConfigureAwait(false)).Contains("OK");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
#if !NET452
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_respond_to_ipv4_loopback()
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_respond_to_ipv4_loopback()
|
||||
{
|
||||
// Assign
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithBody("from ipv4 loopback"));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync($"http://127.0.0.1:{server.Ports[0]}/foo").ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo("from ipv4 loopback");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_respond_to_ipv6_loopback()
|
||||
{
|
||||
// Assign
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithBody("from ipv6 loopback"));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync($"http://[::1]:{server.Ports[0]}/foo").ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo("from ipv6 loopback");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Using_JsonMapping_And_CustomMatcher_WithCorrectParams_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new WireMockServerSettings();
|
||||
settings.WatchStaticMappings = true;
|
||||
settings.WatchStaticMappingsInSubdirectories = true;
|
||||
settings.CustomMatcherMappings = new Dictionary<string, Func<MatcherModel, IMatcher>>();
|
||||
settings.CustomMatcherMappings[nameof(CustomPathParamMatcher)] = matcherModel =>
|
||||
{
|
||||
// Assign
|
||||
var server = WireMockServer.Start();
|
||||
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
|
||||
);
|
||||
};
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithBody("from ipv4 loopback"));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync($"http://127.0.0.1:{server.Ports[0]}/foo").ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo("from ipv4 loopback");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_respond_to_ipv6_loopback()
|
||||
{
|
||||
// Assign
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithBody("from ipv6 loopback"));
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync($"http://[::1]:{server.Ports[0]}/foo").ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(response).IsEqualTo("from ipv6 loopback");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Using_JsonMapping_And_CustomMatcher_WithCorrectParams_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new WireMockServerSettings();
|
||||
settings.WatchStaticMappings = true;
|
||||
settings.WatchStaticMappingsInSubdirectories = true;
|
||||
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 server = WireMockServer.Start(settings);
|
||||
server.WithMapping(@"{
|
||||
var server = WireMockServer.Start(settings);
|
||||
server.WithMapping(@"{
|
||||
""Request"": {
|
||||
""Path"": {
|
||||
""Matchers"": [
|
||||
@@ -463,37 +463,37 @@ namespace WireMock.Net.Tests
|
||||
},
|
||||
""Body"": ""OK""
|
||||
}
|
||||
}");
|
||||
}");
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/customer/132/document/pic.jpg", new StringContent("{ Hi = \"Hello World\" }")).ConfigureAwait(false);
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/customer/132/document/pic.jpg", new StringContent("{ Hi = \"Hello World\" }")).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Using_JsonMapping_And_CustomMatcher_WithIncorrectParams_ShouldNotMatch()
|
||||
[Fact]
|
||||
public async Task WireMockServer_Using_JsonMapping_And_CustomMatcher_WithIncorrectParams_ShouldNotMatch()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new WireMockServerSettings();
|
||||
settings.WatchStaticMappings = true;
|
||||
settings.WatchStaticMappingsInSubdirectories = true;
|
||||
settings.CustomMatcherMappings = new Dictionary<string, Func<MatcherModel, IMatcher>>();
|
||||
settings.CustomMatcherMappings[nameof(CustomPathParamMatcher)] = matcherModel =>
|
||||
{
|
||||
// Arrange
|
||||
var settings = new WireMockServerSettings();
|
||||
settings.WatchStaticMappings = true;
|
||||
settings.WatchStaticMappingsInSubdirectories = true;
|
||||
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 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 server = WireMockServer.Start(settings);
|
||||
server.WithMapping(@"{
|
||||
var server = WireMockServer.Start(settings);
|
||||
server.WithMapping(@"{
|
||||
""Request"": {
|
||||
""Path"": {
|
||||
""Matchers"": [
|
||||
@@ -511,16 +511,15 @@ namespace WireMock.Net.Tests
|
||||
},
|
||||
""Body"": ""OK""
|
||||
}
|
||||
}");
|
||||
}");
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/customer/132/document/pic", new StringContent("{ Hi = \"Hello World\" }")).ConfigureAwait(false);
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/customer/132/document/pic", new StringContent("{ Hi = \"Hello World\" }")).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
server.Stop();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user