Search code examples
c#unit-testingasp.net-web-apimoqhttpcontext

Testing a Web API method that uses HttpContext.Current.Request.Files?


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:\temp");
    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.


Solution

  • Web API has been built to support unit testing by allowing you to mock various context objects. However, by using HttpContext.Current you are using "old-style" System.Web code that uses the HttpContext class which makes it impossible to unit test your code.

    To allow your code to be unit testable you have to stop using HttpContext.Current. In Sending HTML Form Data in ASP.NET Web API: File Upload and Multipart MIME you can see how to upload files using Web API. Ironically, this code also uses HttpContext.Current to get access to the MapPath but in Web API you should use HostingEnvironment.MapPath that also works outside IIS. Mocking the later is also problematic but for now I am focusing on your question about mocking the request.

    Not using HttpContext.Current allows you to unit test your controller by assigning the ControllerContext property of the controller:

    var content = new ByteArrayContent( /* bytes in the file */ );
    content.Headers.Add("Content-Disposition", "form-data");
    var controllerContext = new HttpControllerContext {
      Request = new HttpRequestMessage {
        Content = new MultipartContent { content }
      }
    };
    var controller = new MyController();
    controller.ControllerContext = controllerContext;