一、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. [POJ1180&POJ3709]Batch Scheduling&K-Anonymous Sequence 斜率优化DP

    POJ1180 Batch Scheduling Description There is a sequence of N jobs to be processed on one machine. T ...

  2. 【STSRM12】夏令营

    [题意]n个数划分成k段,每段的价值为段内不同数字的数量,求最大总价值 [算法]DP+线段树 [题解] f[i][j]表示前i个数字划分成j段的最大价值. f[i][j]=max(f[k][j-1]+ ...

  3. DotNETCore 学习笔记 全球化和本地化

    Globalization and localization ********************************************************************* ...

  4. linux基础-临时和永久修改ip地址以及通配符相关

    一.临时配置网络(ip,网关,dns) 修改临时ip地址: 1.ifconfig查看当前的网卡和ip地址 2.临时修改IP地址:ifconfig ens32 192.168.16.200/24,ifc ...

  5. go语言实现拷贝文件

    package main import ( "fmt" "io" "os" ) func main(){ list := os.Args / ...

  6. P2654 原核生物培养

    P2654 原核生物培养 题目描述 W教授最近正在研究一种原核生物,这种生物的生长方式很奇特,只能通过吃掉同类而生长.两个该种生物相遇,较大质量的会把较小的吃掉(相同的话就看RP了),吃掉后较大的生物 ...

  7. Ubuntu 18.04 sublime text 3176 安装、汉化及配置中文输入

    转载自:https://blog.csdn.net/weixin_42508385/article/details/82152393 一.下载: 在https://www.sublimetext.co ...

  8. linux常用命令 查看文件

    Linux常用命令 查看文件 cat命令 cat命令的用途是连接文件或标准打印输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示. 命令格式: cat [ ...

  9. [BZOJ2151] 种树 贪心

    2151: 种树 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1151  Solved: 613[Submit][Status][Discuss] ...

  10. 理解和上手Redux

    顾名思义本文分两个部分,理解和上手,第一部分我先讲个故事,这个故事也许不是特别形象,但对大家理解Redux一定有所帮助.第二部分我举个例子. 先讲个故事: 一个餐厅(应用),我是顾客(用户),这个餐厅 ...