Commit 37321658 37321658e8f68fecc605c1719d7955fc4811dcc0 by Christian Gerdes

- Fixed null ref errors on empty json/xml body

- Added PassIfNotFound to XPathExtractionRule
1 parent 34618ddb
......@@ -86,25 +86,28 @@ namespace WebTest.WebService.Plugin.ResultTab
{
try
{
this.dom = dom;
XmlNode xNode = dom.DocumentElement;
// Load the XML into the TreeView.
treeView.Nodes.Clear();
TreeNode treeNode = new TreeNode(xNode.Name);
treeNode.Tag = xNode;
if (xNode.HasChildNodes)
{
treeNode.Nodes.Add(new TreeNode("dummy"));
this.dom = dom;
if (dom.DocumentElement != null)
{
XmlNode xNode = dom.DocumentElement;
// Load the XML into the TreeView.
treeView.Nodes.Clear();
TreeNode treeNode = new TreeNode(xNode.Name);
treeNode.Tag = xNode;
if (xNode.HasChildNodes)
{
treeNode.Nodes.Add(new TreeNode("dummy"));
}
else
{
treeNode.Text = xNode.InnerXml.Trim();
}
treeView.Nodes.Add(treeNode);
treeNode.Collapse();
}
else
{
treeNode.Text = xNode.InnerXml.Trim();
}
treeView.Nodes.Add(treeNode);
treeNode.Collapse();
}
catch (Exception ex)
{
......
......@@ -50,7 +50,12 @@ namespace WebTest.WebService.Plugin.Runtime
{
get { return randomMatch; }
set { randomMatch = value; }
}
}
[DisplayName("Pass if not found")]
[Description("If set to true, this rule will pass the request even if no match is found.")]
[DefaultValue(false)]
public bool passIfNotFound { get; set; }
/// <summary>
/// The Extract method. The parameter e contains the Web test context.
......@@ -62,64 +67,62 @@ namespace WebTest.WebService.Plugin.Runtime
try
{
ContentHandler contentHandler = ContentHandlerFactory.GetHandler(e.Response.ContentType);
if (contentHandler == null)
{
return;
}
if (contentHandler != null) {
if (contentHandler.IsBinary)
{
contentHandler.MessageBytes = e.Response.BodyBytes;
}
else
{
contentHandler.MessageString = e.Response.BodyString;
}
if (contentHandler.IsBinary)
{
contentHandler.MessageBytes = e.Response.BodyBytes;
}
else
{
contentHandler.MessageString = e.Response.BodyString;
}
XPathNodeIterator iterator = contentHandler.EvaluateXPath(XPathToSearch);
XPathNodeIterator iterator = contentHandler.EvaluateXPath(XPathToSearch);
List<string> values = new List<string>();
List<string> values = new List<string>();
int i = 0;
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
int i = 0;
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
string value;
if (nav2.NodeType.Equals(XPathNodeType.Element))
{
value = nav2.InnerXml;
}
else
{
value = nav2.Value;
}
string value;
if (nav2.NodeType.Equals(XPathNodeType.Element))
{
value = nav2.InnerXml;
}
else
{
value = nav2.Value;
values.Add(value);
if (!ExtractRandomMatch && (i == Index))
{
// add the extracted value to the Web test context
e.WebTest.Context.Add(this.ContextParameterName, value);
e.Success = true;
return;
}
i++;
}
values.Add(value);
if (!ExtractRandomMatch && (i == Index))
if (ExtractRandomMatch && (values.Count > 0))
{
// add the extracted value to the Web test context
e.WebTest.Context.Add(this.ContextParameterName, value);
Random random = new Random();
e.WebTest.Context.Add(this.ContextParameterName, values[random.Next(values.Count)]);
e.Success = true;
return;
}
i++;
}
if (ExtractRandomMatch && (values.Count > 0))
{
Random random = new Random();
e.WebTest.Context.Add(this.ContextParameterName, values[random.Next(values.Count)]);
e.Success = true;
return;
}
}
catch (Exception ex)
{
e.Message = ex.Message;
}
e.Success = false;
}
e.Success = passIfNotFound;
e.Message += " (XPath not found)";
return;
}
}
......