title: Is there a unit-testable way to upload files to ASP.NET WebAPI?
body: <p>I'm working in a project that uses the new ASP.NET WebAPI. My current task is to accept an uploaded file. So far, I have used TDD to drive out the WebAPI code, but I've hit a wall with uploading. I'm currently following the advice found at <a href="http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2">http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2</a>, but there seems to be no way at all to drive this out of a unit test. In order to get at the file and form data, I have to use <code>MultipartFormDataStreamProvider</code>, which is impossible to mock and/or override. Short of forsaking my TDD approach, what can I do?</p>
  
  <p>Here's the code from the example:</p>
  
  <pre><code>public Task&lt;HttpResponseMessage&gt; PostFormData()
  {
      // Check if the request contains multipart/form-data.
      if (!Request.Content.IsMimeMultipartContent())
      {
          throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
      }
  
      string root = HttpContext.Current.Server.MapPath("~/App_Data");
      var provider = new MultipartFormDataStreamProvider(root);
  
      // Read the form data and return an async task.
      var task = Request.Content.ReadAsMultipartAsync(provider).
          ContinueWith&lt;HttpResponseMessage&gt;(t =&gt;
          {
              if (t.IsFaulted || t.IsCanceled)
              {
                  Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
              }
  
              // This illustrates how to get the file names.
              foreach (MultipartFileData file in provider.FileData)
              {
                  Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                  Trace.WriteLine("Server file path: " + file.LocalFileName);
              }
              return Request.CreateResponse(HttpStatusCode.OK);
          });
  
      return task;
  }
  </code></pre>
  
  <p>The first problem is this line:</p>
  
  <pre><code>var provider = new MultipartFormDataStreamProvider(root);
  </code></pre>
  
  <p>For starters, to unit test this code, I need to be able to inject such a provider. It does WAY too much in that simple constructor call to be "newing it up" in line. There's got to be another way. (If not, WebAPI fails)</p>