ValidationRules.cs
3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/************************************************************************************************
* All code in this file is under the MS-RL License (https://opensource.org/licenses/MS-RL) *
* By using the code in this file in any way, you agree to the above license terms. *
* Copyright (C) LIGHTS IN LINE AB (https://www.lightsinline.se) *
* Repository, Wiki, Issue tracker and more at https://git.lightsinline.se/products/VSTT-Plugins *
* *
* Contributors *
* LIGHTS IN LINE AB *
* SWEDBANK AB *
* SKATTEVERKET *
************************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.LoadTesting;
using System.IO;
using System.ComponentModel;
using System.Text.RegularExpressions;
namespace LIL_VSTT_Plugins
{
/// <summary>
/// Count Validation
/// Räknar hur många gånger ett visst reguljärt utryck finns i svaret och validerar detta
/// </summary>
[DisplayName("Count Validation")]
[Description("(C) Copyright 2011 LIGHTS IN LINE AB\r\nValiderar att ett reguljärt utryck finns minst ett visst antal gånger i svaret, eller inte.")]
public class CountValidation : ValidationRule
{
[DisplayName("RegEx"), Description("Regular Expression to match on the response.")]
public String RegEx { get; set; }
[DisplayName("Condition"), Description("Valid conditions are =,>,<,>=,<=")]
[DefaultValue("=")]
public String myCon { get; set; }
[DisplayName("Count"), Description("Expected count of occurences")]
public int Count { get; set; }
[DisplayName("Pass"), Description("Pass or fail if count matches. True passes if the count matches, False fails if the count matches")]
[DefaultValue(true)]
public bool Pass { get; set; }
public override void Validate(object sender, ValidationEventArgs e)
{
MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(
e.Response.BodyString,
RegEx,
RegexOptions.IgnoreCase);
switch (myCon) {
case "=":
if (matches.Count == Count) { e.IsValid = Pass; e.Message = "Number of matches equal to count. Valid set to " + Pass.ToString(); }
break;
case "<":
if (matches.Count < Count) { e.IsValid = Pass; e.Message = "Number of matches less than count. Valid set to " + Pass.ToString(); }
break;
case ">":
if (matches.Count > Count) { e.IsValid = Pass; e.Message = "Number of matches higher than count. Valid set to " + Pass.ToString(); }
break;
case "<=":
if (matches.Count <= Count) { e.IsValid = Pass; e.Message = "Number of matches less or equal to count. Valid set to " + Pass.ToString(); }
break;
case ">=":
if (matches.Count >= Count) { e.IsValid = Pass; e.Message = "Number of matches higher or equal to count. Valid set to " + Pass.ToString(); }
break;
default:
e.IsValid = true; e.Message = "Invalid condition specified. Validation will pass.";
break;
}
}
}
}