mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 08:35:15 +01:00
Support for xml namespaces in XPathMatcher (#1005)
* Support for xml namespaces in XPathMatcher * Review findings of Stef implemented. * Fix of build error * New review findings by Stef --------- Co-authored-by: Carsten Alder <carsten.alder@schleupen.de>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using NFluent;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
|
||||
@@ -49,6 +50,71 @@ public class XPathMatcherTests
|
||||
Check.That(result).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void XPathMatcher_IsMatch_WithNamespaces_AcceptOnMatch()
|
||||
{
|
||||
// Assign
|
||||
string input =
|
||||
@"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
|
||||
<s:Body>
|
||||
<QueryRequest xmlns=""urn://MyWcfService"">
|
||||
<MaxResults i:nil=""true"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>
|
||||
<Restriction i:nil=""true"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>
|
||||
<SearchMode i:nil=""true"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>
|
||||
</QueryRequest>
|
||||
</s:Body>
|
||||
</s:Envelope>";
|
||||
var xmlNamespaces = new[]
|
||||
{
|
||||
new XmlNamespace { Prefix = "s", Uri = "http://schemas.xmlsoap.org/soap/envelope/" },
|
||||
new XmlNamespace { Prefix = "i", Uri = "http://www.w3.org/2001/XMLSchema-instance" }
|
||||
};
|
||||
var matcher = new XPathMatcher(
|
||||
MatchBehaviour.AcceptOnMatch,
|
||||
MatchOperator.Or,
|
||||
xmlNamespaces,
|
||||
"/s:Envelope/s:Body/*[local-name()='QueryRequest' and namespace-uri()='urn://MyWcfService']");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void XPathMatcher_IsMatch_WithNamespaces_OneSelfDefined_AcceptOnMatch()
|
||||
{
|
||||
// Assign
|
||||
string input =
|
||||
@"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
|
||||
<s:Body>
|
||||
<QueryRequest xmlns=""urn://MyWcfService"">
|
||||
<MaxResults i:nil=""true"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>
|
||||
<Restriction i:nil=""true"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>
|
||||
<SearchMode i:nil=""true"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""/>
|
||||
</QueryRequest>
|
||||
</s:Body>
|
||||
</s:Envelope>";
|
||||
var xmlNamespaces = new[]
|
||||
{
|
||||
new XmlNamespace { Prefix = "s", Uri = "http://schemas.xmlsoap.org/soap/envelope/" },
|
||||
new XmlNamespace { Prefix = "i", Uri = "http://www.w3.org/2001/XMLSchema-instance" },
|
||||
new XmlNamespace { Prefix = "q", Uri = "urn://MyWcfService" }
|
||||
};
|
||||
var matcher = new XPathMatcher(
|
||||
MatchBehaviour.AcceptOnMatch,
|
||||
MatchOperator.Or,
|
||||
xmlNamespaces,
|
||||
"/s:Envelope/s:Body/q:QueryRequest");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch(input).Score;
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsEqualTo(1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void XPathMatcher_IsMatch_RejectOnMatch()
|
||||
{
|
||||
@@ -57,7 +123,7 @@ public class XPathMatcherTests
|
||||
<todo-list>
|
||||
<todo-item id='a1'>abc</todo-item>
|
||||
</todo-list>";
|
||||
var matcher = new XPathMatcher(MatchBehaviour.RejectOnMatch, MatchOperator.Or, "/todo-list[count(todo-item) = 1]");
|
||||
var matcher = new XPathMatcher(MatchBehaviour.RejectOnMatch, MatchOperator.Or, null, "/todo-list[count(todo-item) = 1]");
|
||||
|
||||
// Act
|
||||
double result = matcher.IsMatch(xml).Score;
|
||||
|
||||
@@ -145,6 +145,26 @@ public class MatcherMapperTests
|
||||
model.IgnoreCase.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_XPathMatcher()
|
||||
{
|
||||
// Assign
|
||||
var xmlNamespaceMap = new[]
|
||||
{
|
||||
new XmlNamespace { Prefix = "s", Uri = "http://schemas.xmlsoap.org/soap/envelope/" },
|
||||
new XmlNamespace { Prefix = "i", Uri = "http://www.w3.org/2001/XMLSchema-instance" },
|
||||
new XmlNamespace { Prefix = "q", Uri = "urn://MyWcfService" }
|
||||
};
|
||||
var matcher = new XPathMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.And, xmlNamespaceMap);
|
||||
|
||||
// Act
|
||||
var model = _sut.Map(matcher)!;
|
||||
|
||||
// Assert
|
||||
model.XmlNamespaceMap.Should().NotBeNull();
|
||||
model.XmlNamespaceMap.Should().BeEquivalentTo(xmlNamespaceMap);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_MatcherModel_Null()
|
||||
{
|
||||
@@ -522,4 +542,47 @@ public class MatcherMapperTests
|
||||
matcher.ContentTypeMatcher.Should().BeAssignableTo<ContentTypeMatcher>().Which.GetPatterns().Should().ContainSingle("text/json");
|
||||
}
|
||||
#endif
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_MatcherModel_XPathMatcher_WithXmlNamespaces_As_String()
|
||||
{
|
||||
// Assign
|
||||
var pattern = "/s:Envelope/s:Body/*[local-name()='QueryRequest']";
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "XPathMatcher",
|
||||
Pattern = pattern,
|
||||
XmlNamespaceMap = new[]
|
||||
{
|
||||
new XmlNamespace { Prefix = "s", Uri = "http://schemas.xmlsoap.org/soap/envelope/" }
|
||||
}
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (XPathMatcher)_sut.Map(model)!;
|
||||
|
||||
// Assert
|
||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
|
||||
matcher.XmlNamespaceMap.Should().NotBeNull();
|
||||
matcher.XmlNamespaceMap.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_MatcherModel_XPathMatcher_WithoutXmlNamespaces_As_String()
|
||||
{
|
||||
// Assign
|
||||
var pattern = "/s:Envelope/s:Body/*[local-name()='QueryRequest']";
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = "XPathMatcher",
|
||||
Pattern = pattern
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (XPathMatcher)_sut.Map(model)!;
|
||||
|
||||
// Assert
|
||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
|
||||
matcher.XmlNamespaceMap.Should().BeNull();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user