如何执行HttpClientHandler的内存测试

时间:2012-09-24 11:44:39

标签: asp.net-web-api

我正在通过将HttpServer实例直接传递给HttpClient来编写一些内存中集成测试。我怎样才能连接HttpClientHandler

Establish ctx = () =>
{
    var config = new HttpConfiguration();
    config.Routes.MapHttpRoute(name: "Default", routeTemplate: "api/{controller}/{id}", defaults: new { controller = "values", id = RouteParameter.Optional });
    config.MessageHandlers.Add(new CompressionHandler());

    var server = new HttpServer(config)
    {
        InnerHandler = new DecompressionHandler()
    };
    client = new HttpClient(server);
};

2 个答案:

答案 0 :(得分:2)

您可以链接处理程序:

var handler = new HttpServer(config)
{
    InnerHandler = new HttpClientHandler()
};

HttpClient client = new HttpClient(handler);

更多信息(惊喜!:-) at asp.net - http://www.asp.net/web-api/overview/working-with-http/http-message-handlers

答案 1 :(得分:1)

为了测试客户端处理程序,我必须将HttpServer实际设置为内部处理程序:

var server = new HttpServer(config);

var handler = new DecompressionHandler
{
    InnerHandler = server
};

client = new HttpClient(handler);