一、MessageHandler不一定是全局的,也可以只应用到指定的Router上
、定义一个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);
二、HttpRequest头添加Authorization信息
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);
}
三、模拟异步发送Http请求
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);
}
}
四、模拟WebApi HttpRequestMessage的Content
request.Content = new ObjectContent<ShipmentRequestModel>(
shipmentRequestModel, new System.Net.Http.Formatting.JsonMediaTypeFormatter());
 
五、HttpRequestMessage对象获取依赖注入接口
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单元测试记录的更多相关文章

  1. 在MVC或WEBAPI中记录每个Action的执行时间和记录下层方法调用时间

    刚才在博客园看了篇文章,http://www.cnblogs.com/cmt/p/csharp_regex_timeout.html  突然联想到以前遇到的问题,w3wp进程吃光CPU都挂起IIS进程 ...

  2. mvc+webapi 单元测试

    1.前言 现在这个项目已经有阶段性的模块完成了,所以就想着对这些模块进行单元测试,以保证项目的代码的质量.首先虽然标题是mvc+webapi实质上我只是对mvc进行的测试.用的时候vs的unit te ...

  3. Asp.net WebAPI 单元测试

    现在Asp.net webapi 运用的越来越多,其单元而是也越来越重要.一般软件开发都是多层结构,上层调用下层的接口,而各层的实现人员不同,一般大家都只写自己对应单元测试.对下层的依赖我们通过IOC ...

  4. webapi单元测试时出现的ConfigurationManager.ConnectionStrings为空错误

    这个是读取配置文件没读到的问题,解决方法很简单,把webapi的配置文件复制到单元测试项目中,并把名字改为App.config即可. 同时 ,推荐使用Unit Test Genertor来做测试,这个 ...

  5. Net Core WebApi单元测试

    单元测试 本篇将结合这个系列的例子的基础上演示在Asp.Net Core里如何使用XUnit结合Moq进行单元测试,同时对整个项目进行集成测试. 第一部分.XUnit 修改 Project.json  ...

  6. 使用 xUnit 编写 ASP.NET Core WebAPI单元测试

    本文使用xUnit对ASP.NET Core WebAPI做单元测试,使用HttpClient的同步和异步请求,下面详细介绍xUnit的使用过程: 一.创建示例项目 模板为我们自动创建了一个Value ...

  7. postman调用webapi错误记录

    1.webapi ,接口中header中,value 不能太长,太长会报错 结局:value中不要存太长的数据 2.如果key 中有中文,会获取不到数据 , 解决:需要把中文转码,然后后端解码 3.如 ...

  8. netframework webapi IogAttribute记录request参数和错误信息

    参考博客 https://www.cnblogs.com/hnsongbiao/p/7039666.html 书写LogFilterAttribute public class LogFilterAt ...

  9. ASP.NET WebApi 简单记录

    //获取当前提交过来的Request对象 var request = System.Web.HttpContext.Current.Request;

随机推荐

  1. 【BZOJ2820】YY的GCD [莫比乌斯反演]

    YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 求1<=x<=N, ...

  2. bzoj4839 [Neerc2016]Abbreviation

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4839 [题解] pkusc怎么出bzoj原题啊 字符串随便处理一下就行了,大模拟 pkusc ...

  3. bzoj 1798 维护序列seq 线段树

    裸的线段树,注意标签下放就行了 多么痛的领悟,一定要开int64 /************************************************************** Pro ...

  4. NPOI的使用Excel模板导出 可插入到指定行

    Excel模版建议把需要添加数据行的样式设置好 模版样式,导出后效果 [2017-11-22 对获取需插入数据的首行样式有时为空报错修改] /// <summary> /// 根据模版导出 ...

  5. javascript批量输入表单

    void((function(){ x=document.getElementsByTagName("a"); y = x[1] y.click() })()) void((fun ...

  6. python—用for循环、while循环和一句话打印九九乘法表

    用for循环打印九九乘法表: for i in range (1,10): for j in range(1,10): print(j,"x",i,"=",i* ...

  7. 关于hrtimer_forward小段代码的分析【转】

    转自:http://blog.csdn.net/wowuyinglingluan/article/details/45720151 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?) ...

  8. jq TAB切换

    <a href="http://www.w3.org/1999/xhtml">" target="_blank">http://ww ...

  9. 《JavaScript模式》精要

    P25. 如何避免eval()定义全局变量? 如: var jsstring = "var un = 1;"; eval(jsstring); console.log(typeof ...

  10. Delphi字符串、PChar与字符数组之间的转换

    来自:http://my.oschina.net/kavensu/blog/193719 ------------------------------------------------------- ...