title: Specifying the content of a HttpRequestMessage for a Web API unit test
body: <p>I have a web API controller method I want to unit test. It takes in a <code>HttpRequestMessage</code> but I can't work out how to set the content that I want to pass in. Is it possible to create/mock the <code>HttpRequestMessage</code> so that I can give it a <code>string</code> that I want to be the result of <code>await request.Content.ReadAsStringAsync()</code>?</p>
  
  <p>This is my controller method:</p>
  
  <pre><code>[HttpPost]
  public async Task&lt;HttpResponseMessage&gt; Post(HttpRequestMessage request)
  {
      var data = await request.Content.ReadAsStringAsync();
      //do something with data
  }
  </code></pre>
  
  <p>I can easily create the <code>HttpRequestMessage</code> with its parameterless constructor, but I can't work out how to set the content to a meaningful value. I would like my test to work along these lines:</p>
  
  <pre><code>[TestMethod]
  public async Task PostMethodWorks()
  {
      var controller = new MyController();
  
      var data = "this will be JSON";
      var httpRequestMessage = new HttpRequestMessage();
      //set the content somehow so that httpRequestMessage.Content.ReadAsStringAsync returns data
  
      var response = await controller.Post(httpRequestMessage);
  
      //assert something about the response here
  }
  </code></pre>
  
  <p>Is it possible to set the value of the content to some JSON, or will I need to change the method so it takes in a different parameter?</p>
  
  <p>(For more context, the reason I want to have the method taking in a <code>HttpRequestMessage</code> is because I'm working on a legacy codebase with that has loads of controller methods which take in a <code>HttpRequestMessage</code>.)</p>