HttpClient的与单​​元测试场景asp.net的WebAPI [英] HttpClient with asp.net WebApi in unit testing scenario

查看:147
本文介绍了HttpClient的与单​​元测试场景asp.net的WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集成测试,我想与测试我的WebAPI控制器的基础上使用。

起初我以为我会在本地HTTP建立的WebAPI自我主机模式和进位输出终端到终端的测试。

不过,我后来在WebApiContrib项目,其可能设立的HttpClient用的HttpServer设置了到的WebAPI控制器正确的服务路线看测试实现。我似乎我能单元测试控制器而无需在自托管模式下设置的WebAPI。我可以把任何域名在客户端请求和HttpClient的似乎自动绑定神奇到正确的控制器。

是否有任何HTTP传输发生在这里,使用一些地方间通讯科或纯粹'看到'服务器是在同一个应用程序域,因此使用反射?
什么是引擎盖下发生这一切的发生?

code:

[测试]
公共无效Test_WebApi_Controller()
{
    的Assembly.Load(myproj.Web);    VAR prodServiceMock =新的模拟< IProductService>();
    ObjectFactory.Initialize(X => x.For&下; IProductService方式>()使用(prodServiceMock.Object));    无功配置=新HttpConfiguration();
    config.Routes.MapHttpRoute(默认,的WebAPI / {}控制器/(编号),新{ID = RouteParameter.Optional});
    config.ServiceResolver.SetResolver(新WebApiDependencyResolver());    VAR服务器=新的HttpServer(配置);
    VAR的客户=新的HttpClient(服务器);    VAR响应= client.GetAsync(http://anything.com/webapi/product)。结果;}


解决方案

的HttpClient有一个可插拔的管道模式。通常,当你新建立一个HttpClient的你会得到一个HttpClientHandler实例作为默认的请求处理器。这是HttpClientHandler一个实际执行的HttpWebRequest。 HttpClientHandler从HttpMessageHandler派生的。

这是没有concidence的HttpServer也HttpMessageHandler派生。所以在这个例子中的HttpServer被传递到HttpClient的实例,以提供它的请求处理。通过将HttpMessageHandler到的HttpClient的构造函数,你告诉HttpClient的使用提供处理程序而不是默认的。如果你在System.Net.Http.WebRequest看WebRequestHandler你会看到这是从HttpClientHandler来源并添加了一些额外的功能,是专门针对Windows桌面操作系统。

这意味着,当你做出一个请求HttpClient的是直接传递到HttpServer的消息处理程序,然后进行处理,因为它通常会在服务器上。

I have an integration test which I wanted to use as the basis of testing my WebAPI controllers with.

Initially I thought I would have to set-up WebAPI in self-host mode and carry-out end-to-end tests over local Http.

However I realised later by looking at the tests in the WebApiContrib project that its possible to set up an HttpClient with an HttpServer set-up with the correct service route to the WebAPI controller. I seems I can unit test the controllers without setting up WebApi in self-host mode. I can put in any domain name in the request on the client and HttpClient seems to auto-magically bind to the correct controller.

Is there any Http transport happening here, using some local interprocess comms or purely 'seeing' that the server is in the same app domain and thus using reflection? What is happening under the hood for this to happen?

code:

[Test]
public void Test_WebApi_Controller()
{
    Assembly.Load("myproj.Web");

    var prodServiceMock = new Mock<IProductService>();
    ObjectFactory.Initialize(x => x.For<IProductService>().Use(prodServiceMock.Object));                      

    var config = new HttpConfiguration();
    config.Routes.MapHttpRoute("default", "webapi/{controller}/{id}", new { id = RouteParameter.Optional });
    config.ServiceResolver.SetResolver(new WebApiDependencyResolver());

    var server = new HttpServer(config);
    var client = new HttpClient(server);

    var response = client.GetAsync("http://anything.com/webapi/product").Result;

}

解决方案

HttpClient has a pluggable pipeline model. Normally when you new up a HttpClient you get a HttpClientHandler instance as the default request processor. That HttpClientHandler is the one actually does the HttpWebRequest. HttpClientHandler derives from HttpMessageHandler.

By no concidence HttpServer also derives from HttpMessageHandler. So in this example the HttpServer is being passed to the HttpClient instance to provide it's request processing. By passing a HttpMessageHandler to the constructor of HttpClient you are telling HttpClient to use the provided handler instead of the default one. If you look at WebRequestHandler in System.Net.Http.WebRequest you will see this is derived from HttpClientHandler and adds some extra functionality that is specific to the Windows Desktop OS.

This means when you make a request to the HTTPClient it is delivered directly to the HttpServer message handler and then processed as it normally would be on the server.

这篇关于HttpClient的与单​​元测试场景asp.net的WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆