title: Is it possible to mock out a .NET HttpWebResponse?
body: <p>i've got an integration test that grabs some json result from a 3rd party server. It's really simple and works great.</p>
  
  <p>I was hoping to stop actually hitting this server and using <code>Moq</code> (or any Mocking library, like ninject, etc) to hijack and force the return result.</p>
  
  <p>is this possible?</p>
  
  <p>Here is some sample code :-</p>
  
  <pre><code>public Foo GoGetSomeJsonForMePleaseKThxBai()
  {
      // prep stuff ...
  
      // Now get json please.
      HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("Http://some.fancypants.site/api/hiThere);
      httpWebRequest.Method = WebRequestMethods.Http.Get;
  
      string responseText;
  
      using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
      {
          using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
          {
              json = streamReader.ReadToEnd().ToLowerInvariant();
          }
      }
  
      // Check the value of the json... etc..
  }
  </code></pre>
  
  <p>and of course, this method is called from my test.</p>
  
  <p>I was thinking that maybe I need to pass into this method (or a property of the class?) a mocked <code>httpWebResponse</code> or something but wasn't too sure if this was the way. Also, the response is a output from an <code>httpWebRequest.GetResponse()</code> method .. so maybe I just need to pass in a mocked <code>HttpWebRequest</code> ?.</p>
  
  <p>any suggestions with some sample code would be most aprreciated!</p>