简体   繁体   English

如何针对仅SSL的Web Api控制器对测试请求进行单元化?

[英]How do I unit test requests against SSL-only Web Api Controller?

I have a unit test which uses the OWIN TestServer class to host my Web Api ApiController classes for testing. 我有一个单元测试,该单元测试使用OWIN TestServer类来承载Web Api ApiController类进行测试。

I first wrote the unit test when the REST API did not have the HTTPS (SSL) requirement baked into the Controller itself. 当REST API没有将HTTPS(SSL)要求引入Controller本身时,我首先编写了单元测试。

My unit test looked something like this: 我的单元测试看起来像这样:

[TestMethod]
[TestCategory("Unit")]
public async Task Test_MyMethod()
{
    using (var server = TestServer.Create<TestStartup>())
    {
        //Arrange
        var jsonBody = new JsonMyRequestObject();
        var request = server.CreateRequest("/api/v1/MyMethod")
            .And(x => x.Method = HttpMethod.Post)
            .And(x => x.Content = new StringContent(JsonConvert.SerializeObject(jsonBody), Encoding.UTF8, "application/json"));

        //Act
        var response = await request.PostAsync();
        var jsonResponse =
            JsonConvert.DeserializeObject<JsonMyResponseObject>(await response.Content.ReadAsStringAsync());

        //Assert
        Assert.IsTrue(response.IsSuccessStatusCode);
    }

}

Now that I've applied the attribute to enforce HTTPS, my unit test fails. 现在,我已经应用了该属性来实施HTTPS,我的单元测试将失败。

How do I fix my test so that, all things being equal, the test passes again? 如何修正测试,以便在所有条件相同的情况下再次通过测试?

To fix this unit test, you need to change the base address for the TestServer . 要修复此单元测试,您需要更改TestServer的基地址。

Once the server has been created set the BaseAddress property on the created object to use an "https" address. 创建服务器后,在创建的对象上设置BaseAddress属性以使用“ https”地址。 Remember the default BaseAddress value is http://localhost . 请记住,默认的BaseAddress值为http://localhost

In which case, you can use https://localhost . 在这种情况下,可以使用https://localhost

The changed unit test would look as follows: 更改后的单元测试如下所示:

[TestMethod]
[TestCategory("Unit")]
public async Task Test_MyMethod()
{
    using (var server = TestServer.Create<TestStartup>())
    {
        //Arrange
        server.BaseAddress = new Uri("https://localhost");
        var jsonBody = new JsonMyRequestObject();
        var request = server.CreateRequest("/api/v1/MyMethod")
            .And(x => x.Method = HttpMethod.Post)
            .And(x => x.Content = new StringContent(JsonConvert.SerializeObject(jsonBody), Encoding.UTF8, "application/json"));

        //Act
        var response = await request.PostAsync();
        var jsonResponse =
            JsonConvert.DeserializeObject<JsonMyResponseObject>(await response.Content.ReadAsStringAsync());

        //Assert
        Assert.IsTrue(response.IsSuccessStatusCode);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 XUnit 对 Web API controller 进行单元测试 - How to unit test a Web API controller using XUnit 如何模拟对Web API控制器单元的依赖关系测试? - How can mock dependencies to Web API controller unit test? Web API控制器的无效测试方法 - How to unit test void method of web api controller 单元测试 .NET 核心 3 Web API Controller - Unit test .NET Core 3 Web API Controller 如何编写Web Api单元测试,我应该从UserManager获取信息 - How do I write Web Api unit test which I should get info from the UserManager 如何在返回IHttpActionResult时对web api动作方法进行单元测试? - How do I unit test web api action method when it returns IHttpActionResult? 为什么我在单元测试中有迁移问题但在控制器 c# Web API 中没有 - Why i have migration problem in unit test but not in controller c# Web API Api控制器的单元测试 - Unit test for Api controller 单元测试带有伪HTTPContext的ASP.NET Web API控制器 - Unit Test ASP.NET Web API controller with fake HTTPContext 如何在API控制器的单元测试中注册我的System.Web.Security.Membership以避免此问题? - How should register my System.Web.Security.Membership to avoid this issue in the unit test of an API controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM