WebApi单元测试记录
、定义一个handler
// Pipelines
HttpMessageHandler affiliateShipmentsPipeline =
HttpClientFactory.CreatePipeline(
new HttpControllerDispatcher(config),
new[] { new AffiliateShipmentsDispatcher() }); 、将handler应用在指定的router上
// Routes
routes.MapHttpRoute(
"AffiliateShipmentsHttpRoute",
"api/affiliates/{key}/shipments/{shipmentKey}",
defaults: new { controller = "AffiliateShipments", shipmentKey = RouteParameter.Optional },
constraints: new { key = new GuidRouteConstraint(), shipmentKey = new GuidRouteConstraint() },
handler: affiliateShipmentsPipeline);
HttpRequestMessage request = ConstructRequest(httpMethod, uri, mediaTypes);
request.Headers.Authorization = new AuthenticationHeaderValue(
"Basic",
EncodeToBase64(
string.Format("{0}:{1}", username, password))); private static string EncodeToBase64(string value) {
byte[] toEncodeAsBytes = Encoding.UTF8.GetBytes(value);
return Convert.ToBase64String(toEncodeAsBytes);
}
internal static async Task<HttpResponseMessage> GetResponseAsync(
HttpConfiguration config, HttpRequestMessage request) { using (var httpServer = new HttpServer(config))
using (var client = HttpClientFactory.Create(innerHandler: httpServer)) { return await client.SendAsync(request);
}
}
request.Content = new ObjectContent<ShipmentRequestModel>(
shipmentRequestModel, new System.Net.Http.Formatting.JsonMediaTypeFormatter());
internal static class HttpRequestMessageExtensions {
internal static IShipmentService GetShipmentService(this HttpRequestMessage request) {
return request.GetServic e<IShipmentService>();
}
internal static IMembershipService GetMembershipService(this HttpRequestMessage request) {
return request.GetService<IMembershipService>();
}
private static TService GetService<TService>(this HttpRequestMessage request) {
IDependencyScope dependencyScope = request.GetDependencyScope();
TService service = (TService)dependencyScope.GetService(typeof(TService));
return service;
}
}
WebApi单元测试记录的更多相关文章
- 在MVC或WEBAPI中记录每个Action的执行时间和记录下层方法调用时间
刚才在博客园看了篇文章,http://www.cnblogs.com/cmt/p/csharp_regex_timeout.html 突然联想到以前遇到的问题,w3wp进程吃光CPU都挂起IIS进程 ...
- mvc+webapi 单元测试
1.前言 现在这个项目已经有阶段性的模块完成了,所以就想着对这些模块进行单元测试,以保证项目的代码的质量.首先虽然标题是mvc+webapi实质上我只是对mvc进行的测试.用的时候vs的unit te ...
- Asp.net WebAPI 单元测试
现在Asp.net webapi 运用的越来越多,其单元而是也越来越重要.一般软件开发都是多层结构,上层调用下层的接口,而各层的实现人员不同,一般大家都只写自己对应单元测试.对下层的依赖我们通过IOC ...
- webapi单元测试时出现的ConfigurationManager.ConnectionStrings为空错误
这个是读取配置文件没读到的问题,解决方法很简单,把webapi的配置文件复制到单元测试项目中,并把名字改为App.config即可. 同时 ,推荐使用Unit Test Genertor来做测试,这个 ...
- Net Core WebApi单元测试
单元测试 本篇将结合这个系列的例子的基础上演示在Asp.Net Core里如何使用XUnit结合Moq进行单元测试,同时对整个项目进行集成测试. 第一部分.XUnit 修改 Project.json ...
- 使用 xUnit 编写 ASP.NET Core WebAPI单元测试
本文使用xUnit对ASP.NET Core WebAPI做单元测试,使用HttpClient的同步和异步请求,下面详细介绍xUnit的使用过程: 一.创建示例项目 模板为我们自动创建了一个Value ...
- postman调用webapi错误记录
1.webapi ,接口中header中,value 不能太长,太长会报错 结局:value中不要存太长的数据 2.如果key 中有中文,会获取不到数据 , 解决:需要把中文转码,然后后端解码 3.如 ...
- netframework webapi IogAttribute记录request参数和错误信息
参考博客 https://www.cnblogs.com/hnsongbiao/p/7039666.html 书写LogFilterAttribute public class LogFilterAt ...
- ASP.NET WebApi 简单记录
//获取当前提交过来的Request对象 var request = System.Web.HttpContext.Current.Request;
随机推荐
- 在Ubuntu下安装IntelliJ IDEA
1)在IDEA官网下载Linux版本安装包 2)将安装包shell到/usr/local目录下 3)切到安装目录下,验证文件校验和,官网上显示的校验和: 3d77ee82094dab51e345f16 ...
- bzoj1503: [NOI2004]郁闷的出纳员 fhqtreap版
这道题写法和之前差不多 但是fhqtreap在加点的时候为了同时维护大根堆以及二叉排序树的性质所以插入时也要注意分裂 fhqteap需要判断指针是否为空 不然就会re 这个我调了很久 #include ...
- ssh 免密码登录【用】
ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...
- linux下面某些常用命令的用法【转】
转自:http://blog.csdn.net/luo3532869/article/details/7584290 ls 命令用于常看目录,用法:ls [选项][目录或文件]例:使用ls命令显示/h ...
- [ 手记 ] 联想rd650服务器整列及系统安装
联想 RD650服务器 磁盘阵列:http://wenku.baidu.com/view/b364c2db5f0e7cd185253644.html?from=search 该服务器安装系统需要BIO ...
- 推荐下载App,如果本地安装则直接打开本地App(Android/IOS)
推荐下载App,如果本地安装则直接打开本地App(Android/IOS) - 纵观现在每家移动网站,打开首页的时候,都有各种各样的形式来提示你下载自身的移动App(Android/IOS),这是做移 ...
- python 如何将JSON数据原封不动的转为字符串(顺序不能变动)?
最好是采用 OrderedDict + json.dumps方案 1. 在存储 content 的时候就使用 OrderedDict 而非用默认的 dict from collections impo ...
- sqlalchemy源代码阅读随笔(4):url。py 阅读
在_to_string中,有 _rfc_1738_quote(text): 这个函数.这个主要是遵循 RFC 1738的规则.对传入的信息(主要是用户名或者密码)进行格式匹配.其代码就一行: retu ...
- [BZOJ4568][Scoi2016]幸运数字 倍增+线性基
4568: [Scoi2016]幸运数字 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1791 Solved: 685[Submit][Statu ...
- 浅谈C#多线程与UI响应
www.educity.cn 发布者:shenywww 来源:网络转载 发布日期:2014年10月06日 ...