0
我有一个OAuthServerProvider认证用户名和密码后发出令牌。当用户名或密码无效时,我拒绝owin上下文,默认情况下将返回400 Bad Request
作为状态码。NUnit测试用例跳过owin中间件
但我想用401 Unauthorized
回应要做到这一点我写了一个中间件将检查头,看看自定义标题是存在,如果是将401
替换状态码if (context.Response.StatusCode == 400 && context.Response.Headers.ContainsKey(Constants.OwinChallengeFlag))
{
var headerValues = context.Response.Headers.GetValues(Constants.OwinChallengeFlag);
context.Response.StatusCode = Convert.ToInt16(headerValues.FirstOrDefault());
context.Response.Headers.Remove(Constants.OwinChallengeFlag);
}
这个工作绝对没问题,当我用小提琴打它时,但是我在下面写的单元测试总是得到400.不知何故,当我用我的单元测试请求中间件时,跳过了。
[TestFixture]
public class UnitTest1
{
private TestServer _server;
[SetUp]
public void SetUp()
{
_server = TestServer.Create<Startup>();
}
[Test]
public void ShouldReturnUnauthorizedResponse()
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/token");
//wrong password
var requestContent = "grant_type=password&UserName=foo&Password=bar";
request.Content = new StringContent(requestContent, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = _server.HttpClient.SendAsync(request).Result;
//This assert fails, but shouldn't
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
}
}
需要知道我在做什么错在这里。
1)这是一个集成测试而不是单元测试。 2)显示启动配置和完整的中间件。 3)你是否缓冲响应以便下载访问它? – Nkosi