0

我正在使用 Owin Test Server 编写单元测试,但它的行为不像我预期的那样。我无法弄清楚我做错了什么。我希望状态代码已被修改,就像我在 owin 管道的最后一步中所做的那样,但它没有生效,因此第二个断言失败。

using Owin;
using Microsoft.Owin;
using Microsoft.Owin.Testing;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;

namespace My.Namespace
{
    [Trait("Category", "Test Category")]
    public class WhenIMakeWebRequest
    {
        [Fact]
        public async void IGetUnauthorizedError()
        {
            using (var server = TestServer.Create(app =>
            {
                app.Use((context, next) =>
                {
                    return next().ContinueWith(task =>
                    {
                        context.Response.WriteAsync(" Now Changing Status Code");
                        context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                    });
                });
                app.Run(async context =>
                {
                    await context.Response.WriteAsync("Hello world using OWIN TestServer.");
                });
            }))
            {
                var response = await server.CreateRequest("/Welcome").GetAsync();
                var result = await response.Content.ReadAsStringAsync();
                Assert.Equal(result, "Hello world using OWIN TestServer. Now Changing Status Code");
                Assert.Equal(response.StatusCode, System.Net.HttpStatusCode.Unauthorized);
            }

        }
    }
}
4

1 回答 1

0

找到了答案。实际上,在正文中的内容已经写出之后,无法设置标题。所以我需要缓冲输出,以便设置标题。

于 2016-11-23T01:33:47.243 回答