When it comes time to test my web controllers, I have found that mocking the HttpRequestMessage object can be a pain. It's quite a cumbersome object to have to mock. I'm also not a fan of the statefulness the Request object carries into my functions.
For these reasons, I have started "ignoring" the request object and have begun using things like:
- Implementing ParameterBindingAttributes to get specific access to the Headers I want
- Manually creating and returning an HttpResponseMessage instead of calling
this.Response.CreateResponse(...)
I have found that this makes both my code easier to read and easier to test.
That said, before I go further with this approach, I wanted to make sure there were not any explicit reasons why I shouldn't write my code like this.
Is there any specific information that would be difficult to access unless I use the HttpRequestMessage intance?
Is there any thing info that my manually created HttpResponseMessage will be missing because it wasn't created using the instance method
CreateResponse
?Is there any reason this might comeback to bite me in the future?