Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 237
AJ. Avatar asked Jul 02 '15 15:07

AJ.


People also ask

Can we mock HttpContext?

HttpContextMock is an implementation of AspNetCore. Http. HttpContext that stores a Mock<HttpContext> instance and works as a proxy for the real Mock. The HttpContextMock constructor initializes a Mock for Request, Response, Header and etc to work as a proxy as well.

What is the use of HttpContext?

What is HttpContext? It holds the current information about the Http request. It contains the information like authorization, authentication, request, response, session, items, users, formOptions, etc. Every HTTP request creates a new object of HttpContext with current information.

How can get HttpContext current in ASP.NET Core?

Current property, instead it is available in the HttpContext class in ASP.Net Core applications. Session can be enabled using the Configure method. Inside this method, you will have to call the UseSession method of the app object. Note: It is mandatory to call the UseSession method before the UseMvc method.


2 Answers

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;
like image 197
Martin Liversage Avatar answered Sep 18 '22 12:09

Martin Liversage


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.

like image 39
iGanja Avatar answered Sep 18 '22 12:09

iGanja