ASP.net MVC 4的WebAPI - 测试MIME多内容 [英] ASP.net MVC 4 WebApi - Testing MIME Multipart Content
问题描述
我有一个ASP.net MVC 4(测试版)的WebAPI,看起来是这样的:
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.
}
我试图单元测试这个code,但不知道如何做到这一点。我在这里在正确的轨道上?
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();
}
这code抛出以下异常:
This code is throwing the following exception:
System.AggregateException:出现一个或多个错误。 ---> System.IO.IOException:MIME多流意外结束。哑剧
多部分消息是不完整的。在
System.Net.Http.MimeMultipartBodyPartParser.d__0.MoveNext()
在
System.Net.Http.HttpContentMultipartExtensions.MoveNextPart(MultipartAsyncContext
上下文)在
System.Net.Http.HttpContentMultipartExtensions.MultipartReadAsyncComplete(IAsyncResult的
结果)的
System.Net.Http.HttpContentMultipartExtensions.OnMultipartReadAsyncComplete(IAsyncResult的
结果)
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问题被张贴在不久前,我只是不得不面对TE同样的问题。
Altough the question was posted a while ago, i just had to deal with te same kind of problem.
这是我的解决方案:
创建一个假的实现HttpControllerContext类的,你添加MultipartFormDataContent到Htt的prequestMessage。
Create a fake implementation of the HttpControllerContext class where you add MultipartFormDataContent to the HttpRequestMessage.
public class FakeControllerContextWithMultiPartContentFactory
{
public static HttpControllerContext Create()
{
var request = new HttpRequestMessage(HttpMethod.Post, "");
var content = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(new Byte[100]);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Foo.txt"
};
content.Add(fileContent);
request.Content = content;
return new HttpControllerContext(new HttpConfiguration(), new HttpRouteData(new HttpRoute("")), request);
}
}
然后在您的测试:
then in your test:
[TestMethod]
public void Should_return_OK_when_valid_file_posted()
{
//Arrange
var sut = new yourController();
sut.ControllerContext = FakeControllerContextWithMultiPartContentFactory.Create();
//Act
var result = sut.Post();
//Arrange
Assert.IsType<OkResult>(result.Result);
}
这篇关于ASP.net MVC 4的WebAPI - 测试MIME多内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!