How to implement file upload using Web API POST, which can be easily tested

Here are some ideas that I have:

1 - Using HttpContext

public HttpResponseMessage PostFile()
{
    Save(HttpContext.Current.Request.Files[0]);
    // ...
}

2 - Using the request

public async Task<httpResponseMessage> PostFile()
{
     if (Request.Content.IsMimeMultipartContent())
     {
          var provider = new MultipartFormDataStreamProvider("~/files");
          await Request.Content.ReadAsMultipartAsync(provider);
          // ...
     }
}

First, the controller depends on the HttpContext, which is difficult to mock module testing.

The second approach also depends on the request, which is relatively easier to make fun of.

What is the simplest approach to implementing file uploads using the Web API POST method, which can be easily tested without using Mocking programs such as Moq?

+4
source share

Source: https://habr.com/ru/post/1584542/


All Articles