I am attempting to write a test for a Web API method that uses HttpContext.Current.Request.Files
and after exhaustive searching and experimentation I cannot figure out how to mock for it. The method being tested looks like this:
[HttpPost]
public HttpResponseMessage Post()
{
var requestFiles = HttpContext.Current.Request.Files;
var file = requestFiles.Get(0);
//do some other stuff...
}
I realize that there are other questions similar to this, but they do not address this specific situation.
If I attempt to mock the context, I run into issues with the Http*
object hierarchy. Say I set up various mock objects (using Moq) like this:
var mockFiles = new Mock<HttpFileCollectionBase>();
mockFiles.Setup(s => s.Count).Returns(1);
var mockFile = new Mock<HttpPostedFileBase>();
mockFile.Setup(s => s.InputStream).Returns(new MemoryStream());
mockFiles.Setup(s => s.Get(It.IsAny<int>())).Returns(mockFile.Object);
var mockRequest = new Mock<HttpRequestBase>();
mockRequest.Setup(s => s.Files).Returns(mockFiles.Object);
var mockContext = new Mock<HttpContextBase>();
mockContext.Setup(s => s.Request).Returns(mockRequest.Object);
Attempting to assign it to the current context...
HttpContext.Current = mockContext.Object;
...results in a compiler error/redline because it Cannot convert source type 'System.Web.HttpContextBase' to target type 'System.Web.HttpContext'
.
I have also tried drilling into various context objects that come with the constructed controller object, but can't find one that a) is the return object of an HttpContext.Current
call in the controller method body and b) provides access to standard HttpRequest
properties, like Files
.
var requestMsg = controller.Request; //returns HttpRequestMessage
var context = controller.ControllerContext; //returns HttpControllerContext
var requestContext = controller.RequestContext; //read-only returns HttpRequestContext
It is also important to note that I cannot change the controller that I'm testing at all, so I cannot change the constructor to allow the context to be injected.
Is there any way to mock HttpContext.Current.Request.Files
for unit testing in Web API?
Update
Though I'm not sure this will be accepted by the team, I am experimenting with changing the Post method to use Request.Content
, as suggested by Martin Liversage. It currently looks something like this:
public async Task<HttpResponseMessage> Post()
{
var uploadFileStream = new MultipartFormDataStreamProvider(@"C:emp");
await Request.Content.ReadAsMultipartAsync(uploadFileStream);
//do the stuff to get the file
return ActionContext.Request.CreateResponse(HttpStatusCode.OK, "it worked!");
}
My test looks similar to this:
var byteContent = new byte[]{};
var content = new MultipartContent { new ByteArrayContent(byteContent) };
content.Headers.Add("Content-Disposition", "form-data");
var controllerContext = new HttpControllerContext
{
Request = new HttpRequestMessage
{
Content = new MultipartContent { new ByteArrayContent(byteContent) }
}
};
Now I'm getting an error on ReadAsMultipartAsync
:
System.IO.IOException: Error writing MIME multipart body part to output stream. ---> System.InvalidOperationException: The stream provider of type 'MultipartFormDataStreamProvider' threw an exception. ---> System.InvalidOperationException: Did not find required 'Content-Disposition' header field in MIME multipart body part.
See Question&Answers more detail:
os