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 ...@@ -86,25 +86,28 @@ namespace WebTest.WebService.Plugin.ResultTab
86 { 86 {
87 try 87 try
88 { 88 {
89 this.dom = dom; 89 this.dom = dom;
90 XmlNode xNode = dom.DocumentElement; 90 if (dom.DocumentElement != null)
91 91 {
92 // Load the XML into the TreeView. 92 XmlNode xNode = dom.DocumentElement;
93 treeView.Nodes.Clear(); 93
94 94 // Load the XML into the TreeView.
95 TreeNode treeNode = new TreeNode(xNode.Name); 95 treeView.Nodes.Clear();
96 treeNode.Tag = xNode; 96
97 if (xNode.HasChildNodes) 97 TreeNode treeNode = new TreeNode(xNode.Name);
98 { 98 treeNode.Tag = xNode;
99 treeNode.Nodes.Add(new TreeNode("dummy")); 99 if (xNode.HasChildNodes)
100 {
101 treeNode.Nodes.Add(new TreeNode("dummy"));
102 }
103 else
104 {
105 treeNode.Text = xNode.InnerXml.Trim();
106 }
107
108 treeView.Nodes.Add(treeNode);
109 treeNode.Collapse();
100 } 110 }
101 else
102 {
103 treeNode.Text = xNode.InnerXml.Trim();
104 }
105
106 treeView.Nodes.Add(treeNode);
107 treeNode.Collapse();
108 } 111 }
109 catch (Exception ex) 112 catch (Exception ex)
110 { 113 {
......
...@@ -50,7 +50,12 @@ namespace WebTest.WebService.Plugin.Runtime ...@@ -50,7 +50,12 @@ namespace WebTest.WebService.Plugin.Runtime
50 { 50 {
51 get { return randomMatch; } 51 get { return randomMatch; }
52 set { randomMatch = value; } 52 set { randomMatch = value; }
53 } 53 }
54
55 [DisplayName("Pass if not found")]
56 [Description("If set to true, this rule will pass the request even if no match is found.")]
57 [DefaultValue(false)]
58 public bool passIfNotFound { get; set; }
54 59
55 /// <summary> 60 /// <summary>
56 /// The Extract method. The parameter e contains the Web test context. 61 /// The Extract method. The parameter e contains the Web test context.
...@@ -62,64 +67,62 @@ namespace WebTest.WebService.Plugin.Runtime ...@@ -62,64 +67,62 @@ namespace WebTest.WebService.Plugin.Runtime
62 try 67 try
63 { 68 {
64 ContentHandler contentHandler = ContentHandlerFactory.GetHandler(e.Response.ContentType); 69 ContentHandler contentHandler = ContentHandlerFactory.GetHandler(e.Response.ContentType);
65 if (contentHandler == null) 70 if (contentHandler != null) {
66 { 71 if (contentHandler.IsBinary)
67 return; 72 {
68 } 73 contentHandler.MessageBytes = e.Response.BodyBytes;
74 }
75 else
76 {
77 contentHandler.MessageString = e.Response.BodyString;
78 }
69 79
70 if (contentHandler.IsBinary) 80 XPathNodeIterator iterator = contentHandler.EvaluateXPath(XPathToSearch);
71 {
72 contentHandler.MessageBytes = e.Response.BodyBytes;
73 }
74 else
75 {
76 contentHandler.MessageString = e.Response.BodyString;
77 }
78 81
79 XPathNodeIterator iterator = contentHandler.EvaluateXPath(XPathToSearch); 82 List<string> values = new List<string>();
80 83
81 List<string> values = new List<string>(); 84 int i = 0;
85 while (iterator.MoveNext())
86 {
87 XPathNavigator nav2 = iterator.Current.Clone();
82 88
83 int i = 0; 89 string value;
84 while (iterator.MoveNext()) 90 if (nav2.NodeType.Equals(XPathNodeType.Element))
85 { 91 {
86 XPathNavigator nav2 = iterator.Current.Clone(); 92 value = nav2.InnerXml;
93 }
94 else
95 {
96 value = nav2.Value;
97 }
87 98
88 string value; 99 values.Add(value);
89 if (nav2.NodeType.Equals(XPathNodeType.Element)) 100 if (!ExtractRandomMatch && (i == Index))
90 { 101 {
91 value = nav2.InnerXml; 102 // add the extracted value to the Web test context
92 } 103 e.WebTest.Context.Add(this.ContextParameterName, value);
93 else 104 e.Success = true;
94 { 105 return;
95 value = nav2.Value; 106 }
107 i++;
96 } 108 }
97 109
98 values.Add(value); 110 if (ExtractRandomMatch && (values.Count > 0))
99 if (!ExtractRandomMatch && (i == Index))
100 { 111 {
101 // add the extracted value to the Web test context 112 Random random = new Random();
102 e.WebTest.Context.Add(this.ContextParameterName, value); 113 e.WebTest.Context.Add(this.ContextParameterName, values[random.Next(values.Count)]);
103 e.Success = true; 114 e.Success = true;
104 return; 115 return;
105 } 116 }
106 i++;
107 }
108
109 if (ExtractRandomMatch && (values.Count > 0))
110 {
111 Random random = new Random();
112 e.WebTest.Context.Add(this.ContextParameterName, values[random.Next(values.Count)]);
113 e.Success = true;
114 return;
115 } 117 }
116 } 118 }
117 catch (Exception ex) 119 catch (Exception ex)
118 { 120 {
119 e.Message = ex.Message; 121 e.Message = ex.Message;
120 } 122 }
121 123
122 e.Success = false; 124 e.Success = passIfNotFound;
125 e.Message += " (XPath not found)";
123 return; 126 return;
124 } 127 }
125 } 128 }
......