Skip to content

Instantly share code, notes, and snippets.

@gareth-cheeseman
Created April 5, 2018 08:38
Show Gist options
  • Save gareth-cheeseman/d2f6889000bde1d1070ba72f6ffdea6d to your computer and use it in GitHub Desktop.
Save gareth-cheeseman/d2f6889000bde1d1070ba72f6ffdea6d to your computer and use it in GitHub Desktop.
HTMLParsingPlugin for use with VS loadtesting. Must be file in main solution
using System;
using System.Net.Configuration;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.LoadTesting;
namespace Tests
{
public class HMTLParsingPlugin : ILoadTestPlugin
{
public void Initialize(LoadTest loadTest)
{
ToggleAllowUnsafeHeaderParsing(true);
}
public static bool ToggleAllowUnsafeHeaderParsing(bool enable)
{
//Get the assembly that contains the internal class
Assembly assembly = Assembly.GetAssembly(typeof(SettingsSection));
if (assembly != null)
{
//Use the assembly in order to get the internal type for the internal class
Type settingsSectionType = assembly.GetType("System.Net.Configuration.SettingsSectionInternal");
if (settingsSectionType != null)
{
//Use the internal static property to get an instance of the internal settings class.
//If the static instance isn't created already invoking the property will create it for us.
object anInstance = settingsSectionType.InvokeMember("Section",
BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });
if (anInstance != null)
{
//Locate the private bool field that tells the framework if unsafe header parsing is allowed
FieldInfo aUseUnsafeHeaderParsing = settingsSectionType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
if (aUseUnsafeHeaderParsing != null)
{
aUseUnsafeHeaderParsing.SetValue(anInstance, enable);
return true;
}
}
}
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment