Helpful Custom Request-Plugins for Web Tests

Here are a few Custom Request Plug-ins, that I created in order to help send custom requests that fit my needs. Custom Request Plug-ins will inherit from the base class WebTestRequestPlugin.

The first plug-in is one which will allow you to send your request as raw JSON. Most of the time when sending a request with a payload, it is in the form of JSON rather than using Form Post Parameters already built in in the Web Test Editor.


using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebTests.RequestPlugins
{
    [DisplayName("Add JSON content to Body")]
    [Description("HEY! Tip! Uglify your JSON before pasting.")]
    public class AddJsonContentToBody : WebTestRequestPlugin
    {
        [Description("Assigns the HTTP Body payload to the content provided and sets the content type to application/json")]
        public string JsonContent { get; set; }

        public override void PreRequest(object sender, PreRequestEventArgs e)
        {
            var stringBody = new StringHttpBody();
            stringBody.BodyString = JsonContent;
            stringBody.ContentType = "application/json";

            e.Request.Body = stringBody;
        }
    }
}

JSONBodyPlugin
One thing to note about this plug-in is that we override the PreRequest method, so the Body sent by the request is already overwritten with the new JSON String body we set in the Request in the Web Test.

Note: One awesome(?) thing I found out when trying to copy/paste some JSON into this plug-in was that Visual Studio does not handle new lines well. So, you’ll have to uglify your JSON if you want to paste it straight into the JsonContent section above.

The second plug-in I created allows us to set the Method Type in which we send the request as. In the Web Test Editor the only options we have to send the request as are POST and GET. Sometimes when sending a request in a WebApi project you may want to send it as something like a PATCH, or PUT. Now, this is possible to set the Method to something other than POST, or GET but you have to manually open up the Web Test in an XML editor and it just becomes a real hassle to maintain if you’re going to have numerous requests like this. If you have a custom plug-in, all you have to do is add the plug-in, and say what you want the Method type to be (assuming you want it to be something other than POST or GET).


using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebTests.RequestPlugins
{
    [DisplayName("Set HTTP Method Type")]
    [Description("Sets the HTTP Method for your request. Only necessary when needing a method type other than POST or GET which are included in the UI for RequestUI namespace.")]
    public class SetHttpMethod : WebTestRequestPlugin
    {
        [Description("HTTP Method for your Request. For example: GET, POST, PATCH, PUT")]
        public string MethodType { get; set; }
        public override void PreRequest(object sender, PreRequestEventArgs e)
        {
            e.Request.Method = MethodType;
        }
    }
}

SetMethodTypePlugin
As you can tell from the code above, all you will need to do is enter your desired Method Type (PATCH, PUT, POST, GET, DELETE, etc..) in the UI when adding a custom Request Plug-in.

Hopefully I can update this article with new plug-ins as I create them!

Leave a comment