How can I shim HttpWebResponse.GetResponseAsync() for unit tests?
<p>I have been assigned to write unit tests for an application that I do not have the right to modify. The method I want to unit test makes a call like this:</p>

<pre><code>HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
</code></pre>

<p>I have used Microsoft Fakes for thousands of other tests in this project, so I thought I would do the same here. The simplest and cleanest solution seems to me to shim the <code>request.GetResponseAsync()</code> method. Then I can return some fake content and ensure that the method handles it properly without actually making a request.</p>

<p><code>GetResponseAsync()</code> returns a <code>Task&lt;WebResponse&gt;</code> object. So I would normally do something like:</p>

<pre><code>using (ShimsContext.Create())
{
    System.Net.Fakes.ShimWebRequest.AllInstances.GetResponseAsync = (x) =&gt;
    {
        return new Task&lt;WebResponse&gt;(() =&gt;
        {
            HttpWebResponse toRet = new HttpWebResponse();
            return toRet;
        });
    }
}
</code></pre>

<p>The problem is that the above doesn't compile because </p>

<blockquote>
  <p>'System.Net.HttpWebResponse.HttpWebResponse()' is obsolete: 'This API
  supports the .NET Framework infrastructure and is not intended to be
  used directly from your code.'</p>
</blockquote>

<p>I understand that the type is obsolete and we should be using something different now, but the legacy code I am trying to test does not allow me that luxury. I have looked at many questions on this topic, but none seem to answer this question.</p>