public ConditionCheckResultModel Validate(string stubId, StubConditionsModel conditions) { var result = new ConditionCheckResultModel(); var xpathConditions = conditions?.Xpath?.ToArray() ?? new StubXpathModel[0]; if (!xpathConditions.Any()) { return(result); } var validXpaths = 0; var body = _httpContextService.GetBody(); var doc = new XmlDocument(); doc.LoadXml(body); foreach (var condition in xpathConditions) { var nsManager = new XmlNamespaceManager(doc.NameTable); var namespaces = condition.Namespaces; if (namespaces != null) { foreach (var ns in namespaces) { nsManager.AddNamespace(ns.Key, ns.Value); } } else { // If no namespaces are defined, check the XML namespaces with a regex. nsManager.ParseBodyAndAssignNamespaces(body); } var elements = doc.SelectNodes(condition.QueryString, nsManager); if (elements != null && elements.Count == 0) { // No suitable XML results found. result.Log = $"No suitable XML results found with XPath query {condition}."; break; } validXpaths++; } // If the number of succeeded conditions is equal to the actual number of conditions, // the header condition is passed and the stub ID is passed to the result. result.ConditionValidation = validXpaths == xpathConditions.Length ? ConditionValidationType.Valid : ConditionValidationType.Invalid; return(result); }
public void ParseBodyAndAssignNamespaces_HappyFlow() { // Arrange var doc = new XmlDocument(); doc.LoadXml(Body); var nsManager = new XmlNamespaceManager(doc.NameTable); // Act nsManager.ParseBodyAndAssignNamespaces(Body); // Assert Assert.AreEqual("http://www.w3.org/2003/05/soap-envelope", nsManager.LookupNamespace("soap")); Assert.AreEqual("http://www.example.org/stock/Reddy", nsManager.LookupNamespace("m")); }
/// <inheritdoc /> public ConditionCheckResultModel Validate(StubModel stub) { var result = new ConditionCheckResultModel(); var xpathConditions = stub.Conditions?.Xpath?.ToArray() ?? Array.Empty <StubXpathModel>(); if (!xpathConditions.Any()) { return(result); } var validXpaths = 0; var body = _httpContextService.GetBody(); try { var doc = new XmlDocument(); doc.LoadXml(body); foreach (var condition in xpathConditions) { var nsManager = new XmlNamespaceManager(doc.NameTable); var namespaces = condition.Namespaces; if (namespaces != null) { foreach (var ns in namespaces) { nsManager.AddNamespace(ns.Key, ns.Value); } } else { // If no namespaces are defined, check the XML namespaces with a regex. nsManager.ParseBodyAndAssignNamespaces(body); } var elements = doc.SelectNodes(condition.QueryString, nsManager); if (elements is { Count: 0 }) { // No suitable XML results found. result.Log = $"No suitable XML results found with XPath query {condition.QueryString}."; break; } validXpaths++; }