Two new plugins
ExtractionRule: Extract Cookie Value RequestPlugin: Add Session Cookie
Showing
2 changed files
with
75 additions
and
0 deletions
| ... | @@ -23,6 +23,35 @@ using Microsoft.VisualStudio.TestTools.LoadTesting; | ... | @@ -23,6 +23,35 @@ using Microsoft.VisualStudio.TestTools.LoadTesting; |
| 23 | 23 | ||
| 24 | namespace LIL_VSTT_Plugins | 24 | namespace LIL_VSTT_Plugins |
| 25 | { | 25 | { |
| 26 | [DisplayName("Add Session Cookie")] | ||
| 27 | [Description("Adds a cookie to the webtest cookie collection for this request and all remaining requests in this session.")] | ||
| 28 | public class AddSessionCookie : WebTestRequestPlugin | ||
| 29 | { | ||
| 30 | [DisplayName("Cookie Name"), DefaultValue(""), Description("Name of the cookie to set.")] | ||
| 31 | public string CookieName { get; set; } | ||
| 32 | [DisplayName("Cookie Value"), DefaultValue(""), Description("Value of the cookie to set.")] | ||
| 33 | public string CookieValue { get; set; } | ||
| 34 | [DisplayName("Cookie Value Parameter Name"), DefaultValue(""), Description("Parameter for the cookie value. If the parameter is not found, the Cookie Value will be used instead.")] | ||
| 35 | public string CookieParameter { get; set; } | ||
| 36 | [DisplayName("Cookie Path"), DefaultValue("/"), Description("Path for the cookie to set. Cookie will only be sent if the URL Path matches.")] | ||
| 37 | public string CookiePath { get; set; } | ||
| 38 | [DisplayName("Cookie Domain"), DefaultValue(""), Description("Domain for the cookie to set. Cookie will only be sent if the host name matches.")] | ||
| 39 | public string CookieDomain { get; set; } | ||
| 40 | [DisplayName("Skip URL Encode"), DefaultValue(false), Description("By default the cookie value is URL Encoded for transfer. This will turn it off.")] | ||
| 41 | public bool SkipURLEncode { get; set; } | ||
| 42 | public override void PreRequestDataBinding(object sender, PreRequestDataBindingEventArgs e) | ||
| 43 | { | ||
| 44 | if (!String.IsNullOrEmpty(CookieParameter) && e.WebTest.Context.ContainsKey(CookieParameter)) | ||
| 45 | CookieValue = e.WebTest.Context[CookieParameter].ToString(); | ||
| 46 | if (!SkipURLEncode) | ||
| 47 | { | ||
| 48 | CookieValue = System.Net.WebUtility.UrlEncode(CookieValue); | ||
| 49 | CookieName = System.Net.WebUtility.UrlEncode(CookieName); | ||
| 50 | } | ||
| 51 | e.WebTest.Context.CookieContainer.Add(new System.Net.Cookie(CookieName, CookieValue, CookiePath, CookieDomain)); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 26 | [DisplayName("Add Parameter To Reporting Name")] | 55 | [DisplayName("Add Parameter To Reporting Name")] |
| 27 | [Description("This request plugin will add the specified parameter to the end of the reporting name of the request")] | 56 | [Description("This request plugin will add the specified parameter to the end of the reporting name of the request")] |
| 28 | public class AddParameterToReportingName : WebTestRequestPlugin | 57 | public class AddParameterToReportingName : WebTestRequestPlugin | ... | ... |
| ... | @@ -108,4 +108,50 @@ namespace LIL_VSTT_Plugins | ... | @@ -108,4 +108,50 @@ namespace LIL_VSTT_Plugins |
| 108 | e.Success = true; | 108 | e.Success = true; |
| 109 | } | 109 | } |
| 110 | } | 110 | } |
| 111 | |||
| 112 | /// <summary> | ||
| 113 | /// Extract Cookie Value | ||
| 114 | /// Fångar värdet av en cookie till en parameter | ||
| 115 | /// </summary> | ||
| 116 | [DisplayName("Extract Cookie Value")] | ||
| 117 | [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.")] | ||
| 118 | public class ExtractCookie : ExtractionRule | ||
| 119 | { | ||
| 120 | // Indata | ||
| 121 | |||
| 122 | private string cookieName; | ||
| 123 | [DisplayName("Cookie Name")] | ||
| 124 | [Description("Name of the cookie to extract the value from")] | ||
| 125 | public string CookieName | ||
| 126 | { | ||
| 127 | get { return cookieName; } | ||
| 128 | set { cookieName = value; } | ||
| 129 | } | ||
| 130 | |||
| 131 | public override void Extract(object sender, ExtractionEventArgs e) | ||
| 132 | { | ||
| 133 | WebHeaderCollection col = e.Response.Headers; | ||
| 134 | foreach (string h in col.Keys) | ||
| 135 | { | ||
| 136 | if(h.Equals("Set-Cookie")) | ||
| 137 | { | ||
| 138 | string[] cookie = col[h].Split('='); | ||
| 139 | if(cookie[0].Equals(cookieName)) | ||
| 140 | { | ||
| 141 | string value = ""; | ||
| 142 | if (cookie.Length > 1) | ||
| 143 | { | ||
| 144 | value = cookie[1].Split(';')[0]; | ||
| 145 | } | ||
| 146 | e.WebTest.Context.Add(this.ContextParameterName, WebUtility.UrlDecode(value)); | ||
| 147 | e.Success = true; | ||
| 148 | e.Message = "Cookie found"; | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | e.Success = false; | ||
| 154 | e.Message = "Cookie not found"; | ||
| 155 | } | ||
| 156 | } | ||
| 111 | } | 157 | } | ... | ... |
-
Please register or sign in to post a comment