如何写asp.net网站的API集成休息 [英] How to write integration rest for asp.net web api

查看:317
本文介绍了如何写asp.net网站的API集成休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我忙着设计asp.net网站API上的Web服务。我想开始每个控制器上做单元测试。

I am busy design a web service with asp.net web api. And I want to start doing unit tests on each controller.

这是我的测试类到目前为止:

here is my test class so far:

[TestClass]
public class MyDevicesControllerTest
{
    [TestMethod]        
    public void TestValidUser()
    {
        MyDeviceController controller = new MyDeviceController();
        var result = controller.Get();

    }

    [TestMethod]
    public void TestInvalidUser()
    {
        MyDeviceController controller = new MyDeviceController();            
        var result = controller.Get();
    }
}

但我的web服务使用令牌认证的。所以,我一些如何需要模拟验证过程。

But my web service makes use of token authentication. So I some how need to emulate the authentication process.

所以,我想我不是我,也许做一个HTTP请求,以测试它的用户?即,而不是测试控制器我只是做一个HTTP请求和检查它的答案吗?

So I was thinking my i not maybe make user of a Http Request to test it? Ie instead of testing the controller I just make a http Request and check its answer?

什么是更容易/更好的方式去测试它?

What is the easier / better way to go about testing it?

推荐答案

在ASP.NET的Web API,认证发生在管道调用的控制器之前,所以你需要编写一些集成测试。我将带领你怎么做,在我的上线课程外,在TDD ,但这里是它的要点是:

In ASP.NET Web API, Authentication happens in the pipeline before the Controllers are invoked, so you'll need to write some Integration Tests for that. I walk you through how to do that in my on-line course on Outside-In TDD, but here's the gist of it:

使用的内存HTTP 请求下面是一个集成测试针对的资源。这种测试不使用网络

Here's an Integration Test against a resource using in-memory HTTP requests. This test doesn't use the network:

[Fact]
public void GetReturnsResponseWithCorrectStatusCode()
{
    var baseAddress = new Uri("http://localhost:8765");
    var config = new HttpSelfHostConfiguration(baseAddress);
    config.Routes.MapHttpRoute(
        name: "API Default",
        routeTemplate: "{controller}/{id}",
        defaults: new
        {
            controller = "Home",
            id = RouteParameter.Optional
        });
    var server = new HttpSelfHostServer(config);
    using (var client = new HttpClient(server))
    {
        client.BaseAddress = baseAddress;
        client.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue(
                "Bearer",
                new SimpleWebToken(new Claim("userName", "foo")).ToString());

        var response = client.GetAsync("").Result;

        Assert.True(
            response.IsSuccessStatusCode,
            "Actual status code: " + response.StatusCode);
    }
}

正如你所看到的,它增加了一个旗手令牌请求的HTTP头。在 SimpleWebToken 类只是一个自定义类,我写的场合,但你可以有更好的类,它创建了一个正确的身份验证令牌替换它。

As you can see, it adds a "Bearer" token to the HTTP headers of the request. The SimpleWebToken class is just a custom class I wrote for the occasion, but you can replace it with a better class that creates a correct authentication token.

如果您preFER不同的身份验证方案(如基本或摘要),您可以的设置授权头相应的。

If you prefer a different authentication scheme (such as Basic or Digest), you can set the Authorization header accordingly.

这篇关于如何写asp.net网站的API集成休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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