title: Moq Verify() on a mocked HttpClientHandler can't access the Content object because it is disposed
body: <p>I'm doing a mock HttpClient so I can unit test my code. I want to check the Content that is being posted.</p>
  
  <p>I've done this:</p>
  
  <pre><code>MockHttpMessageHandler =  new Mock&lt;FakeHttpMessageHandler&gt;() { CallBase = true };
  HttpClient = new HttpClient(MockHttpMessageHandler.Object, false);
  </code></pre>
  
  <p>and</p>
  
  <pre><code> MockHttpMessageHandler.Setup(c =&gt; c.Send(It.IsAny&lt;HttpRequestMessage&gt;())).Returns(new HttpResponseMessage
              {
                  StatusCode = HttpStatusCode.OK,
              });
  </code></pre>
  
  <p>and</p>
  
  <pre><code>MockHttpMessageHandler.Verify(c =&gt; c.Send(It.Is&lt;HttpRequestMessage&gt;(
                  m =&gt; m.Content.Headers.ContentType.MediaType == "text/xml" &amp;&amp;
                      m.Method == HttpMethod.Post &amp;&amp;
                      m.RequestUri.ToString() == "http://www.test.com/" &amp;&amp;
                      m.Content.ReadAsStringAsync().Result == "TestContent")));
  </code></pre>
  
  <p>This works fine except for the <code>Content</code> line. I get an error saying that the Content is Disposed.</p>
  
  <p>I'm guessing this is because it's a stream.</p>
  
  <p>Is there an elegant way to be able to test the Content? It would be nice to directly get access to the content because I can test for encoding problems as well.</p>
  
  <p>Edit:</p>
  
  <p>The HttpClient is dependency injected into a class that does this:</p>
  
  <pre><code>public class MyHttpSenderClass
  {
      HttpClient _httpClient; // DI populates this
  
      //...
  
      public async Task&lt;HttpResponseMessage&gt; ComposeAndsendHttpRequestMessage(string url, string payload, string mediaType, string method)
      {    var httpRequestMessage = new HttpRequestMessage(method, new Uri(url));
  
          httpRequestMessage.Content = new StringContent(payload);
  
          httpRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
  
          using (_httpClient)
          {
              // I want to test this httpRequestMessage is correct
              var responseMessage = await _httpClient.SendAsync(httpRequestMessage);
  
              return responseMessage;
          }
      }
  }
  </code></pre>
  
  <p>The exception, for clarification:</p>
  
  <p>System.ObjectDisposedException: Cannot access a disposed object.
  Object name: 'System.Net.Http.StringContent'.</p>
  
  <p>This is on the <code>m.Content.ReadAsStringAsync().Result</code> part, because the Content is disposed.</p>