Testing MultipartFormDataContent

Topics: ASP.NET Web API
May 2, 2012 at 9:32 AM

Hi There.

I have an ASP.net MVC 4 (beta) WebApi that looks something like this:

    public void Post()
   
{
       
if (!Request.Content.IsMimeMultipartContent("form-data"))
       
{
           
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
       
}

       
IEnumerable<HttpContent> parts = Request.Content.ReadAsMultipartAsync().Result;

       
// Rest of code here.
   
}

I am trying to unit test this code, but can't work out how to do it. Am I on the right track here?

    [TestMethod]
   
public void Post_Test()
   
{
       
MultipartFormDataContent content = new MultipartFormDataContent();
        content
.Add(new StringContent("bar"), "foo");

       
this.controller.Request = new HttpRequestMessage();
       
this.controller.Request.Content = content;
       
this.controller.Post();
   
}

This code is throwing the following exception:

System.AggregateException: One or more errors occurred. ---> System.IO.IOException: Unexpected end of MIME multipart stream. MIME multipart message is not complete. at System.Net.Http.MimeMultipartBodyPartParser.d__0.MoveNext() at System.Net.Http.HttpContentMultipartExtensions.MoveNextPart(MultipartAsyncContext context) at System.Net.Http.HttpContentMultipartExtensions.MultipartReadAsyncComplete(IAsyncResult result) at System.Net.Http.HttpContentMultipartExtensions.OnMultipartReadAsyncComplete(IAsyncResult result)

Am I on the right track? How do I mark the end of the MultipartFormDataContent?

May 2, 2012 at 10:01 PM

We have looked into this and while it does repro using the beta bits we have confirmed that the issue has been resolved in the latest bits. You can either try out the latest nightly build (see instructions here [1]) or wait for our next drop.

Hope this helps and thanks for the report!

Henrik

[1] http://blogs.msdn.com/b/henrikn/archive/2012/04/29/using-nightly-nuget-packages-with-asp-net-web-stack.aspx

May 4, 2012 at 11:28 AM

Thanks Henrik! There are a few people involved with this project, so will probably have to wait for the next drop to give it a go, but good to know it's fixed.

Aug 7, 2012 at 5:11 PM

Hi Henrik,

 

I'm experiencing the same problem with the ASP.NET MVC4 version that ships with VS 2012 Ultimate RC. It's really annoying because I'm using Uploadify on the client side which means I can only make changes to the server code.

I'm not really comfortable with the nightly builds. Is there anything I can do to work around it?

Sep 5, 2012 at 11:57 PM

Hancock,

I hope you find the issue to have been resolved with the RTM bits.

Henrik

Sep 6, 2012 at 10:25 AM

Unfortunately that was not the case. I gave up on MVC4 and I'm sticking with MVC3 which does the job.

The issue is really simple but extremely hard to fix. The problem is that Uploadify does not add an "\r\n" at the end of the MultiPartForm message which causes the MVC4 parser to crash and throw an exception. I've tested this with Fiddler. If I add the missing "\r\n" it works. 

It's a shame that MVC4 does this because according to the RFC an "\r\n" at the end may or may not occur so the parser ought to be able to treat both cases.

Anyway, thanks for taking the time to answer.

 

hancock