Another Fiddler Plugin Example

Here is another example of a fiddler plugin. This plugin will loop through each request and mark a request to not be written to the web test if it ends in a particular extension. This example shows you how to flag requests to not be written to the web test by setting the WriteToWebTest property on the Session object. Please see my previous post for how to deploy the plugin: Writing Fiddler Web Test Plugins

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Fiddler;

using Fiddler.WebTesting;

namespace FiddlerPluginExample

{

    public class FilterExtensionExample : IFiddlerWebTestPlugin

    {

        #region IFiddlerWebTestPlugin Members

        public void PreWebTestSave(object sender, PreWebTestSaveEventArgs e)

        {

            //create a list of extensions to exclude

            List<string> extensionsToExclude = new List<string>();

            extensionsToExclude.Add(".jpg");

            extensionsToExclude.Add(".jpeg");

            extensionsToExclude.Add(".gif");

            extensionsToExclude.Add(".js");

            extensionsToExclude.Add(".css");

            for (int i = 0; i < e.FiddlerWebTest.Sessions.Count; i++)

            {

                //get the path part of the URL

                string path = e.FiddlerWebTest.Sessions[i].FiddlerSession.PathAndQuery;

                //if there is a query string, strip it off

                int idxOfQueryString = path.IndexOf("?");

                if (idxOfQueryString > -1)

                {

                    path = path.Substring(0, idxOfQueryString);

                }

                //get the extension

                int idxOfExtension = path.LastIndexOf(".");

                if (idxOfExtension > -1)

                {

                    string extension = path.Substring(idxOfExtension);

                    //if extension is in the exclude list then filter it

                    if (extensionsToExclude.Contains(extension))

                    {

                        //set the WriteToWebTest property to false

                        //to prevent the request from being written

//during save

                        e.FiddlerWebTest.Sessions[i].WriteToWebTest = false;

                    }

                }

            }

        }

        #endregion

    }

}