I have a web API controller method I want to unit test. It takes in a HttpRequestMessage but I can't work out how to set the content that I want to pass in. Is it possible to create/mock the HttpRequestMessage so that I can give it a string that I want to be the result of await request.Content.ReadAsStringAsync()?
This is my controller method:
[HttpPost]
public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
{
var data = await request.Content.ReadAsStringAsync();
//do something with data
}
I can easily create the HttpRequestMessage 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:
[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
}
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?
(For more context, the reason I want to have the method taking in a HttpRequestMessage is because I'm working on a legacy codebase with that has loads of controller methods which take in a HttpRequestMessage.)