Commit ce576f2a ce576f2a59c0e5bbea8c7d783ece1afc6315b9b9 by Christian Gerdes

Two new plugins

ExtractionRule: Extract Cookie Value
RequestPlugin: Add Session Cookie
1 parent 582965e0
......@@ -23,6 +23,35 @@ using Microsoft.VisualStudio.TestTools.LoadTesting;
namespace LIL_VSTT_Plugins
{
[DisplayName("Add Session Cookie")]
[Description("Adds a cookie to the webtest cookie collection for this request and all remaining requests in this session.")]
public class AddSessionCookie : WebTestRequestPlugin
{
[DisplayName("Cookie Name"), DefaultValue(""), Description("Name of the cookie to set.")]
public string CookieName { get; set; }
[DisplayName("Cookie Value"), DefaultValue(""), Description("Value of the cookie to set.")]
public string CookieValue { get; set; }
[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.")]
public string CookieParameter { get; set; }
[DisplayName("Cookie Path"), DefaultValue("/"), Description("Path for the cookie to set. Cookie will only be sent if the URL Path matches.")]
public string CookiePath { get; set; }
[DisplayName("Cookie Domain"), DefaultValue(""), Description("Domain for the cookie to set. Cookie will only be sent if the host name matches.")]
public string CookieDomain { get; set; }
[DisplayName("Skip URL Encode"), DefaultValue(false), Description("By default the cookie value is URL Encoded for transfer. This will turn it off.")]
public bool SkipURLEncode { get; set; }
public override void PreRequestDataBinding(object sender, PreRequestDataBindingEventArgs e)
{
if (!String.IsNullOrEmpty(CookieParameter) && e.WebTest.Context.ContainsKey(CookieParameter))
CookieValue = e.WebTest.Context[CookieParameter].ToString();
if (!SkipURLEncode)
{
CookieValue = System.Net.WebUtility.UrlEncode(CookieValue);
CookieName = System.Net.WebUtility.UrlEncode(CookieName);
}
e.WebTest.Context.CookieContainer.Add(new System.Net.Cookie(CookieName, CookieValue, CookiePath, CookieDomain));
}
}
[DisplayName("Add Parameter To Reporting Name")]
[Description("This request plugin will add the specified parameter to the end of the reporting name of the request")]
public class AddParameterToReportingName : WebTestRequestPlugin
......
......@@ -108,4 +108,50 @@ namespace LIL_VSTT_Plugins
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)
{
WebHeaderCollection col = e.Response.Headers;
foreach (string h in col.Keys)
{
if(h.Equals("Set-Cookie"))
{
string[] cookie = col[h].Split('=');
if(cookie[0].Equals(cookieName))
{
string value = "";
if (cookie.Length > 1)
{
value = cookie[1].Split(';')[0];
}
e.WebTest.Context.Add(this.ContextParameterName, WebUtility.UrlDecode(value));
e.Success = true;
e.Message = "Cookie found";
return;
}
}
}
e.Success = false;
e.Message = "Cookie not found";
}
}
}
......