【发布时间】:2015-05-31 12:38:36
【问题描述】:
假设我在中间件的 http 上下文中设置了一个值。例如 HttpContext.User。
如何在我的单元测试中测试 http 上下文。这是我正在尝试做的一个例子
中间件
public class MyAuthMiddleware
{
private readonly RequestDelegate _next;
public MyAuthMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
context.User = SetUser();
await next(context);
}
}
测试
[Fact]
public async Task UserShouldBeAuthenticated()
{
var server = TestServer.Create((app) =>
{
app.UseMiddleware<MyAuthMiddleware>();
});
using(server)
{
var response = await server.CreateClient().GetAsync("/");
// After calling the middleware I want to assert that
// the user in the HttpContext was set correctly
// but how can I access the HttpContext here?
}
}
【问题讨论】:
标签: asp.net unit-testing httpcontext asp.net-core-mvc