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)
Any idea what the best way to do this is?
Altough the question was posted a while ago, i just had to deal with te same kind of problem.
This was my solution:
Create a fake implementation of the HttpControllerContext class where you add MultipartFormDataContent to the HttpRequestMessage.
then in your test: