一、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. TensorFlow_曲线拟合

    # coding:utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import os ...

  2. mac air 装ubuntu16.04

    前言 我的mac air购于14年,128GB款.最开始我只是在OS X系统里留出了70GB给ubuntu,然后通过u盘装了ubuntu 14.04,后来又陆续将系统升级到15.04.16.04.各方 ...

  3. python学习笔记 协程

    在学习异步IO模型前,先来了解协程 协程又叫做微线程,Coroutine 子程序或者成为函数,在所有语言中都是层级调用,比如a调用b,b调用c.c执行完毕返回,b执行完毕返回,最后a执行完毕返回 所以 ...

  4. servlet+forward和direct区别

    Servlet:是用于 java 编写的服务器端程序,其使用 java servlet API,当客户机发送请求到服务器时,服务器可以将请求信息发送给 servlet,并让 servlet 建立起服务 ...

  5. CentOS6.x 安装升级Python2.7.x Python3.4.x

    CentOS6.x 安装升级Python2.7.x Python3.4.x 2015-06-07• CentOS.Linux • 评论关闭 CentOS release 6.6 (Final) 安装升 ...

  6. UVA 11636.Hello World!-水题

    Hello World! Time limit: 1.000 seconds When you first made the computer to print the sentence “Hello ...

  7. SecureCRT 的上传和下载操作

    在网上找了两篇文章,分别关于ftp和ssh的上传下载,如果有好的大家可以留言分享,不胜感谢~ 因为关于ftp的比较少,就copy上面,本人并没有验证.关于ssh用sr和sz发现一条错误,而且网上也有解 ...

  8. qt资源下载网站

    1. 所有Qt版本下载地址: http://download.qt.io/archive/qt/ 2. 所有Qt Creator下载地址: http://download.qt.io/archive/ ...

  9. 福州3中集训day5

    数论,zld神犇认为我们都学过数论的,讲了一波高端(入门?)操作,从扩展欧几里得开始,同余方程诸如此类,早晚得重修.连课件都没,拿着画图讲了一上午 sro_zld_orz 具体内容都记在本上. 还是说 ...

  10. 洛谷——P1754 球迷购票问题

    题目背景 盛况空前的足球赛即将举行.球赛门票售票处排起了球迷购票长龙. 按售票处规定,每位购票者限购一张门票,且每张票售价为50元.在排成长龙的球迷中有N个人手持面值50元的钱币,另有N个人手持面值1 ...