# Unit testing HTTP requests

I would like to unit test my web requests, so on top of my head I came up with the solution below.

Since it seems to be working fine, I'm posting it on this site. Though it doesn't feel very dynamic, I would like your input. For example, this solution can't simulate response headers, which I'm also interested in mocking:

public interface IWebRequest
{
}

public class RealWebRequest : IWebRequest
{

public RealWebRequest(string url)
{
this.url = url;
}

{
using (WebClient client = new WebClient())
{
}
}
}

public class FakeWebRequest : IWebRequest
{

public FakeWebRequest(string fakeReponse)
{
this.fakeReponse = fakeReponse;
}

{
}
}

public class TestClass
{

public TestClass(IWebRequest request)
{
this.request = request;
}

{
}
}


"Real" usage example:

IWebRequest realRequest = new RealWebRequest("http://www.test.com");
TestClass test = new TestClass(realRequest);
string realResult = await test.DoStuff();


Unit test usage example:

IWebRequest fakeRequest = new FakeWebRequest("fake response string");
TestClass test = new TestClass(fakeRequest);
string fakeResponse = await test.DoStuff();


I really rather think you have the right idea with the interface and your real class. I do heartily endorse your use of readonly which doesn't get a lot of exposure. However, there are many good mocking frameworks out there that you don't need to write your own fake class to unit test with. For instance, we use Moq where I work and your unit test example would look like this:
Mock<IWebRequest> fakeRequest = new Mock<IWebRequest>();