【发布时间】:2017-07-15 02:35:46
【问题描述】:
我在现有的 Web API 中实现了一个 swagger 接口。当前的 API 控制器公开了一个异步上传功能,该功能使用 Request.Content 异步传输图像。已经使用的代码在this文章中解释。
我的 api 控制器:
[HttpPost]
[Route("foo/bar/upload")]
public async Task<HttpResponseMessage> Upload()
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var provider = await Request.Content.ReadAsMultipartAsync(new InMemoryMultipartFormDataStreamProvider());
NameValueCollection formData = provider.FormData;
HttpResponseMessage response;
//access files
IList<HttpContent> files = provider.Files;
if (files.Count > 0)
{
HttpContent file1 = files[0];
using (Stream input = await file1.ReadAsStreamAsync())
{
object responseObj = ExternalProcessInputStream(input)
response = Request.CreateResponse(HttpStatusCode.OK, responseObj);
}
}
else
{
response = Request.CreateResponse(HttpStatusCode.BadRequest);
}
return response;
}
这很有效,但是当我通过 swagger 公开它时,我有一个无参数函数,使用时会返回错误。
我的问题是如何提供一个合适的值来测试这个方法?
【问题讨论】: