ExtractionRules.cs
5.17 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/************************************************************************************************
* 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;
namespace LIL_VSTT_Plugins
{
/// <summary>
/// Nästlad extraction rule
/// Kombination av två extractions rules i en. Den andra söker i resultatet av den första.
/// </summary>
[DisplayName("Nested Extraction")]
[Description("(C) Copyright 2011 LIGHTS IN LINE AB\r\nKombination av två extractions där den andra söker i resultatet av den första.")]
public class NestedExtractionRule : ExtractionRule
{
// Indata
private string start1;
[DisplayName("Extraction 1 Starts With")]
[Description("")]
public string Start1
{
get { return start1; }
set { start1 = value; }
}
private string end1;
[DisplayName("Extraction 1 Ends With")]
[Description("")]
public string End1
{
get { return end1; }
set { end1 = value; }
}
private string start2;
[DisplayName("Extraction 2 Starts With")]
[Description("")]
public string Start2
{
get { return start2; }
set { start2 = value; }
}
private string end2;
[DisplayName("Exctration 2 Ends With")]
[Description("")]
public string End2
{
get { return end2; }
set { end2 = value; }
}
public override void Extract(object sender, ExtractionEventArgs e)
{
// Find the first extraction
// TODO: Du behöver spola fram till att börja sökningen av end1 EFTER start1!
string extraction1;
int s1, s2, e1, e2;
s1 = e.Response.BodyString.IndexOf(start1) + start1.Length;
e1 = e.Response.BodyString.IndexOf(end1);
// Validate
if (s1 > e.Response.BodyString.Length || (e1 - s1 < 1))
{
// Error
e.Success = false;
return;
}
extraction1 = e.Response.BodyString.Substring(s1, e1 - s1);
// Find the second extraction
// TODO: Du behöver spola fram till att börja sökningen av end2 EFTER start2!
string extraction2;
s2 = extraction1.IndexOf(start2) + start2.Length;
e2 = extraction1.IndexOf(end2);
// Validate
if (s2 > extraction1.Length || (e2 - s2 < 1))
{
// Error
e.Success = false;
return;
}
extraction2 = extraction1.Substring(s2, e2 - s2);
e.WebTest.Context.Add(this.ContextParameterName, extraction2);
e.Success = true;
}
}
/// <summary>
/// Extract Cookie Value
/// Fångar värdet av en cookie till en parameter
/// </summary>
[DisplayName("Extract Cookie Value")]
[Description("(C) Copyright 2019 LIGHTS IN LINE AB\r\nExtracts the value of a cookie into a context parameter. Note: Can only find cookies that are valid for the current URI of the request.")]
public class ExtractCookie : ExtractionRule
{
// Indata
private string cookieName;
[DisplayName("Cookie Name")]
[Description("Name of the cookie to extract the value from")]
public string CookieName
{
get { return cookieName; }
set { cookieName = value; }
}
public override void Extract(object sender, ExtractionEventArgs e)
{
foreach (Cookie ck in e.Response.Cookies)
{
if(ck.Name.Equals(cookieName))
{
e.WebTest.Context.Add(this.ContextParameterName, WebUtility.UrlDecode(ck.Value));
e.Success = true;
e.Message = "Cookie found";
return;
}
}
e.Success = false;
e.Message = "Cookie not found";
}
}
}