单元测试 – 测试ASP.NET Web API多部分表单数据文件上载
发布时间:2020-05-24 12:54:50 所属栏目:asp.Net 来源:互联网
导读:我正在尝试使用N-UNIT来测试我的Web API应用程序,但我无法找到一种正确的方法来测试我的文件上传方法.哪种方法是测试方法的最佳方法? Web API控制器: [AcceptVerbs(post)]public async TaskHttpResponseMessage Validate() { // Check if the request conta
|
我正在尝试使用N-UNIT来测试我的Web API应用程序,但我无法找到一种正确的方法来测试我的文件上传方法.哪种方法是测试方法的最佳方法? Web API控制器: [AcceptVerbs("post")]
public async Task<HttpResponseMessage> Validate()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
return Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType,"please submit a valid request");
}
var provider = new MultipartMemoryStreamProvider(); // this loads the file into memory for later on processing
try
{
await Request.Content.ReadAsMultipartAsync(provider);
var resp = new HttpResponseMessage(HttpStatusCode.OK);
foreach (var item in provider.Contents)
{
if (item.Headers.ContentDisposition.FileName != null)
{
Stream stream = item.ReadAsStreamAsync().Result;
// do some stuff and return response
resp.Content = new StringContent(result,Encoding.UTF8,"application/xml"); //text/plain "application/xml"
return resp;
}
}
return resp;
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError,e);
}
}
解决方法根据您的上述评论,以下是一个示例:HttpClient client = new HttpClient();
MultipartFormDataContent formDataContent = new MultipartFormDataContent();
formDataContent.Add(new StringContent("Hello World!"),name: "greeting");
StreamContent file1 = new StreamContent(File.OpenRead(@"C:ImagesImage1.jpeg"));
file1.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
file1.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
file1.Headers.ContentDisposition.FileName = "Image1.jpeg";
formDataContent.Add(file1);
StreamContent file2 = new StreamContent(File.OpenRead(@"C:ImagesImage2.jpeg"));
file2.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
file2.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
file2.Headers.ContentDisposition.FileName = "Image1.jpeg";
formDataContent.Add(file2);
HttpResponseMessage response = client.PostAsync("http://loclhost:9095/api/fileuploads",formDataContent).Result;
通过电线的请求将是: POST http://localhost:9095/api/fileuploads HTTP/1.1 Content-Type: multipart/form-data; boundary="34d56c28-919b-42ab-8462-076b400bd03f" Host: localhost:9095 Content-Length: 486 Expect: 100-continue Connection: Keep-Alive --34d56c28-919b-42ab-8462-076b400bd03f Content-Type: text/plain; charset=utf-8 Content-Disposition: form-data; name=greeting Hello World! --34d56c28-919b-42ab-8462-076b400bd03f Content-Type: image/jpeg Content-Disposition: form-data; filename=Image1.jpeg ----Your Image here------- --34d56c28-919b-42ab-8462-076b400bd03f Content-Type: image/jpeg Content-Disposition: form-data; filename=Image2.jpeg ----Your Image here------- --34d56c28-919b-42ab-8462-076b400bd03f-- (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api – 如何使用OWIN自动主机的web api来提供i
- asp.net-mvc – ASP.NET MVC:很多路由 – 总是只有一个控制
- 具有Entity Framework Code Only和ASP.NET MVC的模块化应用
- asp.net-mvc – ASP.Net MVC加载进度指标
- asp.net-web-api – HttpClient不报告从Web API返回的异常
- asp.net – Response.Redirect和线程被中止错误?
- asp.net-mvc – asp.net mvc强类型助手 – 你的渲染绑定对象
- asp.net-mvc – 奇怪的错误w / NinjectValidatorFactory更新
- asp.net – 直接将.aspx转换为.pdf [已关闭]
- asp.net – App.Config和Web.Config之间的区别?
推荐文章
站长推荐
- asp.net – 用于bower.json文件的Visual Studio
- asp.net-mvc – MVC中的部分视图
- asp.net-mvc – ASP.NET MVC视图模型命名约定
- asp.net-mvc – ASP.net MVC DropDownList预选项
- ASP.NET httpRedirect:重定向所有页面,除了一个
- asp.net – DBContext.Entry做什么?
- asp-classic – 哪里可以存储经典ASP的连接字符串
- asp.net-core – 如何在VS2017 final中定位多个框
- asp.net-mvc – 在MVC中将值从Controller传输到S
- asp.net-mvc – 我应该为ASP.Net MVC项目使用什么
热点阅读
