|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
public class UnauthorizedResultTests
{
[Fact]
public void UnauthorizedResult_InitializesStatusCode()
{
// Arrange & act
var result = new UnauthorizedHttpResult();
// Assert
Assert.Equal(StatusCodes.Status401Unauthorized, result.StatusCode);
}
[Fact]
public void UnauthorizedResult_ExecuteResultSetsResponseStatusCode()
{
// Arrange
var result = new UnauthorizedHttpResult();
var httpContext = GetHttpContext();
// Act
result.ExecuteAsync(httpContext);
// Assert
Assert.Equal(StatusCodes.Status401Unauthorized, httpContext.Response.StatusCode);
}
[Fact]
public async Task ExecuteAsync_ThrowsArgumentNullException_WhenHttpContextIsNull()
{
// Arrange
var result = new UnauthorizedHttpResult();
HttpContext httpContext = null;
// Act & Assert
await Assert.ThrowsAsync<ArgumentNullException>("httpContext", () => result.ExecuteAsync(httpContext));
}
[Fact]
public void UnauthorizedResult_Implements_IStatusCodeHttpResult_Correctly()
{
// Act & Assert
var result = Assert.IsAssignableFrom<IStatusCodeHttpResult>(new UnauthorizedHttpResult());
Assert.Equal(StatusCodes.Status401Unauthorized, result.StatusCode);
}
private static IServiceCollection CreateServices()
{
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
return services;
}
private static HttpContext GetHttpContext()
{
var services = CreateServices();
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = services.BuildServiceProvider();
return httpContext;
}
}
|