ExtractionRules.cs 5.58 KB
/************************************************************************************************
* 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)
        {
            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";
        }
    }
}