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

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;

The accepted answer is perfect for the OP's question. I wanted to add my solution here, which derives from Martin's, as this is the page I was directed to when simply searching on how to Mock out the Request object for Web API so I can add headers my Controller is looking for. I had a difficult time finding the simple answer:

   var controllerContext = new HttpControllerContext();
   controllerContext.Request = new HttpRequestMessage();
   controllerContext.Request.Headers.Add("Accept", "application/xml");

   MyController controller = new MyController(MockRepository);
   controller.ControllerContext = controllerContext;

And there you are; a very simple way to create controller context with which you can "Mock" out the Request object and supply the correct headers for your Controller method.