Wednesday, June 27, 2012

Web test plugin to encode to base64


This is a Web Performance test plugin that get's the values from 2 context parameters and encrypts them in base64, before running the call:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;


namespace Base64
{
    public class Encrypt64 : WebTestPlugin
    {
        [System.ComponentModel.DisplayName("Context Parameter Name")]
        [System.ComponentModel.Description("Name of the webtest context parameter")]
        public string ContextParameterName
        {
            get;
            set;
        }


        // encode to Base64 function
        static public string EncodeTo64(string toEncode)
        {
            byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
            string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }


        // decode from Base64 function
        static public string DecodeFrom64(string encodedData)
        {
            byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
            string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
            return returnValue;
        }


        // this will be executed before the call
        public override void PreWebTest(object sender, PreWebTestEventArgs e)
        {            
            string decode64;
            decode64 = EncodeTo64(e.WebTest.Context["email"] + ":" + e.WebTest.Context["pass"]);
            e.WebTest.Context["GetBase64"] = decode64;
        }


        // this will be executed after the call
        public override void PostWebTest(object sender, PostWebTestEventArgs e)
        {
        }
    }
}

No comments:

Post a Comment