Owin测试服务器如何在执行中间件之前添加标头

时间:2017-06-05 19:45:33

标签: c# .net unit-testing owin owin-middleware

我正在使用OWIN中间件,我正在尝试设置一个在控制器上设置的标头。对于单元测试,我试图在测试中设置标题无济于事。设置此标头的最佳方法是什么?

[TestMethod]
public async Task HasHeader()
{
        using (var server = TestServer.Create(app =>
        {
            //need to set header before calling the below middleware header name is bobsyouruncle
            app.Use(typeof(HeaderMiddleware));

        }))

        using (var client = new HttpClient(server.Handler))
        {
            var response = await client.GetAsync("http://win.example.com/bob/");
            System.Diagnostics.Debugger.Break();
        }
}

1 个答案:

答案 0 :(得分:0)

创建一个设置标头的测试中间件。

 [TestMethod]
 public async Task HasHeader()
  {
    using (var server = TestServer.Create(app =>
    {
        //need to set header before calling the below middleware header name is bobsyouruncle
        app.Use(typeof(TestMiddleware));
        app.Use(typeof(HeaderMiddleware));

    }))
    using (var client = new HttpClient(server.Handler))
    {
        var response = await client.GetAsync("http://win.example.com/bob/");
        System.Diagnostics.Debugger.Break();
    }
}

中间件

 public TestMiddleware(OwinMiddleware next)
        : base(next)
    {


    }

    public async override Task Invoke(IOwinContext context)
    {
        await Next.Invoke(context);
        context.Response.Headers.Add("bobsyouruncle", new string[] { "test1234" });


    }

我不确定如何传递该标头值。