如何测试需要身份验证和授权的webapi控制器xunit?

我有一个自定义授权属性。 我尝试使用r1 = typeSortHelper([1,2,3].runtimeType,2.runtimeType); ,其中TestServer是我使用内存EF数据库创建的另一个Startup类。

Startup

这是一个示例方法。如果我使用邮递员尝试该方法,则会收到禁止的请求。

 testServer = new TestServer(new WebHostBuilder().UseStartup<Startup>());
 httpCLient = testServer.Createclient();

当我尝试这样做时:

[RoleAuthorize(arguments)]
[HttpGet]
public IactionResult Getall()
{
    var users = _userService.Getall();
    return Ok(users);
}

没有未经授权的回复,我可以得到用户。

然后我得到var scope = testServer.Host.Services.CreateScope(); var userService = scope.ServiceProvider.GetRequiredService<IUserService>(); var userController = new UsersController(userService); userController.Getall();

NotFound

如何测试授权和认证?

wodedark 回答:如何测试需要身份验证和授权的webapi控制器xunit?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3169524.html

大家都在问