Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
/// <summary>
/// This test ensures that the ApiWebRequestFactory behaves correctly when
/// a different type of WebRequest is assigned to the http:// prefix
/// </summary>
public void OverrideHttpPrefix()
// Couldn't find a way to "officially" unregister a prefix but that shouldn't stop us
var prefixListProperty = typeof(WebRequest).GetProperty("PrefixList", BindingFlags.Static | BindingFlags.NonPublic);
var oldPrefixList = prefixListProperty.GetValue(null);
WebRequest.RegisterPrefix("http://", new CustomWebRequestCreator());
// Make sure we properly hooked the WebRequest factory
var factory = new ApiWebRequestFactory();
var request = factory.Create(new Uri("http://localhost"));
// Unregister the prefix
prefixListProperty.SetValue(null, oldPrefixList);
// Make sure we properly restored the old WebRequest factory
private class CustomWebRequestCreator : IWebRequestCreate
public WebRequest Create(Uri uri)
return new FakeWebRequest();
private class FakeWebRequest : WebRequest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment