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;
随机推荐
- 【bzoj3924&&luogu3345】幻想乡战略游戏
这题可以用线段树做,不过正解恐怕是动态点分治?(点分树) 简单介绍下动态点分治的概念:在点分治的过程中,一般我们面对的问题都是静态的.如果涉及到修改这类的操作,我们就希望找到我们是如何处理到当前的修改 ...
- 搜索引擎--范例:django之初试牛刀
这学期学了一门课<信息检索>,也就是传说中的搜索引擎 大作业自然而然的让我们自己做一个小型的搜索引擎.于是乎,我们本次的主题就诞生了 我也是边学边用,下面和大家一起分享我在这个过程中学到的 ...
- 堆外内存: Chronicle Map
https://www.javacodegeeks.com/2015/04/chroniclemap-java-architecture-with-off-heap-memory.html Java堆 ...
- LNMP的基本配置
LNMP的基本配置cd /usr/local/nginx_php/etc/ > php-fpm.conf //清空php-fpm.conf vim ph ...
- 使用python获取整月每一天的系统监控数据生成报表
1.安装阿里开源监控工具tsar tsar官方网站 wget -O tsar.zip https://github.com/alibaba/tsar/archive/master.zip --no-c ...
- 使用 mybatis和oracle 数据库出现的问题
mybatis 官网教程 http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html 出现 Could not set parameters for ma ...
- php关键字
\b( (a(bstract|nd|rray|s))| (c(a(llable|se|tch)|l(ass|one)|on(st|tinue)))| (d(e(clare|fault)|ie|o))| ...
- SQL 插入多行数据语句整理
参考别人的,希望对大家有用. 1.只是插入简单的有限行数据时用: insert 要插入的表名(列名1,列名2,....) select '列名1需要的数据','列名2需要的数据',... union ...
- 2018 ACM-ICPC 南京网络赛
Problem A Problem B Problem C Problem D Problem E Problem F Problem G Problem H Problem I Problem J ...
- Kattis - boxes (dfn序)
Boxes There are N boxes, indexed by a number from 1 to N . Each box may (or not may not) be put into ...