我如何测试ASP.NET MVC 4的Web API中的自定义DelegatingHandler? [英] How can I test a custom DelegatingHandler in the ASP.NET MVC 4 Web API?

查看:151
本文介绍了我如何测试ASP.NET MVC 4的Web API中的自定义DelegatingHandler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个问题,在一些地方出现,并没有看到任何伟大的答案。正如我不得不这样做我几次,我想我会后我的解决方案。如果您有什么更好的,请留言。

I've seen this question come up in a few places, and not seen any great answers. As I've had to do this myself a few times, I thought I'd post my solution. If you have anything better, please post.

N.B。这是使用ASP.NET MVC 4 Beta 2版本的Web API的 - 未来版本可能更改

N.B. This is using ASP.NET MVC 4 Beta 2 version of Web API - future versions may change!

更新:这仍然工作在ASP.NET MVC 4 RC

Update: This still works in ASP.NET MVC 4 RC

推荐答案

在这种方法中,我创建了一个TestHandler并将其设置为处理器的测试中的 InnerHandler 属性

In this approach, I create a TestHandler and set it as the InnerHandler property of the handler under test.

被测试的处理器可以被传递到的HttpClient - 这可能,如果你正在编写一个服务器端的处理看起来不直观,但其实这是一个很大的光重量的方式来测试一个处理程序 - 这将在,将在一个服务器中的相同的方式被称为

The handler under test can then be passed to an HttpClient - this may seem unintuitive if you are writing a server-side handler, but this is actually a great light-weight way to test a handler - it will be called in the same way it would in a server.

该TestHandler只是在默认情况下返回HTTP 200,但它的构造函数接受,你可以使用,使功能声称对通过请求消息
从测试的处理程序。最后,你可以断言从客户端SendAsync调用的结果。

The TestHandler will just return an HTTP 200 by default, but it's constructor accepts a function you can use to make asserts about the request message passed in from the handler under test. Finally you can make asserts on the result of the SendAsync call from the client.

一旦一切都建立了,叫 SendAsync 上的客户端实例调用您的处理程序。该请求将被传递到您的处理程序,它会在它传递给TestHandler(假设它通过在电话会议上),那么它会返回一个响应返回到您的处理。

Once everything is set up, call SendAsync on the client instance to invoke your handler. The request will be passed into your handler, it will pass this on to the TestHandler (assuming it passes the call on) which will then return a response back to your handler.

测试处理器看起来是这样的:

The test handler looks like this:

public class TestHandler : DelegatingHandler
{
    private readonly Func<HttpRequestMessage,
        CancellationToken, Task<HttpResponseMessage>> _handlerFunc;

    public TestHandler()
    {
        _handlerFunc = (r, c) => Return200();
    }

    public TestHandler(Func<HttpRequestMessage,
        CancellationToken, Task<HttpResponseMessage>> handlerFunc)
    {
        _handlerFunc = handlerFunc;
    }

    protected override Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
    {
        return _handlerFunc(request, cancellationToken);              
    }

    public static Task<HttpResponseMessage> Return200()
    {
        return Task.Factory.StartNew(
            () => new HttpResponseMessage(HttpStatusCode.OK));
    }
}

用法示例与想象 MyHandler的测试。使用NUnit的的断言:

Example usage with an imagined MyHandler under test. Uses NUnit for the asserts.:

var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
httpRequestMessage.Headers.Add("username", "test");

var handler = new MyHandler()
{
    InnerHandler = new TestHandler((r,c) =>
    {
        Assert.That(r.Headers.Contains("username"));
        return TestHandler.Return200();
    })
};

var client = new HttpClient(handler);
var result = client.SendAsync(httpRequestMessage).Result;

Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));

TestHandler的默认行为可能是许多试验罚款和使code简单。处理程序测试的设置则是这样的:

The default behaviour of TestHandler is probably fine for many tests and makes the code simpler. The setup of the handler under test then looks like this:

var handler = new MyHandler();
handler.InnerHandler = new TestHandler();

我喜欢这种方法,因为它使在试验方法中的所有断言和 TestHandler 非常重用的。

这篇关于我如何测试ASP.NET MVC 4的Web API中的自定义DelegatingHandler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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