0
我正在使用Owin測試服務器編寫單元測試,但它不像我預期的那樣運行。我無法弄清楚我做錯了什麼。我期望狀態代碼已被修改,因爲我在owin流水線的最後一步做了,但它沒有生效,所以第二個斷言失敗了。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);
}
}
}
}
或掛鉤OnSendingHeaders事件 – Tratcher