我正在编写一个函数,该函数使用IF语句从提示中搜索数组。因此,对于成功的输出,我希望获得它的输出,但同时也会输出失败的输出。
// The array I'm searching through
var statesArray = new Array();
statesArray['WI'] = "Wisconsin";
statesArray['MN'] = "Minnesota";
statesArray['IL'] = "Illinois";
// Now I'm trying to let the user search for the full state name from the two-letter abbreviation.
var stateSearch = prompt("enter a two letter state abbreviation")
for(var key in statesArray){
var value = statesArray[key]
if(stateSearch == key){
alert(value);
}else{
alert("try again");
}
}Run Code Online (Sandbox Code Playgroud)
因此,如果我在提示符下输入“ WI”,则会得到“ Wisconsin”并“重试”。
我正在尝试使用授权端点为 ASP.NET Web API 设置集成测试。
我已按照 Microsoft 的文档将模拟身份验证添加到集成测试中,以允许测试客户端访问授权端点。https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0
例如
builder.ConfigureTestServices(services =>
{
services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes("Test")
.RequireAuthenticatedUser()
.Build();
});
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是默认身份验证方案,则可以很好地工作,您可以在集成测试启动时更改该方案以使用测试方案。但是,我的授权端点正在使用指定的AuthenticationSchemes,因此测试方案永远不会被端点授权。例如
[Authorize(AuthenticationSchemes = "Scheme1,Scheme2")]
public class AppVersionController : ControllerBase
{
...
}
Run Code Online (Sandbox Code Playgroud)
我可以通过在测试时指定环境变量、检查它并将测试方案动态添加到授权端点来解决这个问题。然而,这为应用程序添加了许多特定于测试的逻辑,这在主项目中并不好。
这会起作用:
// Test scheme added dynamically from an environment variable to get the below result
[Authorize(AuthenticationSchemes = "Scheme1,Scheme2,Test")]
public class AppVersionController : ControllerBase
{
...
}
Run Code Online (Sandbox Code Playgroud)
我通过创建一个自定义属性来完成此操作,该属性基本上如下所示:
public class AuthorizeAll : AuthorizeAttribute
{
public AuthorizeAll()
{
var authenticationSchemes = "Scheme1,Scheme2"; …Run Code Online (Sandbox Code Playgroud) c# integration-testing authorization asp.net-core-webapi .net-6.0