|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Microsoft.AspNetCore.Http.HttpResults;
public class StatusCodeResultTests
{
[Fact]
public void StatusCodeResult_ExecuteResultSetsResponseStatusCode()
{
// Arrange
var result = new StatusCodeHttpResult(StatusCodes.Status404NotFound);
var httpContext = GetHttpContext();
// Act
result.ExecuteAsync(httpContext);
// Assert
Assert.Equal(StatusCodes.Status404NotFound, httpContext.Response.StatusCode);
}
[Fact]
public async Task ExecuteAsync_ThrowsArgumentNullException_WhenHttpContextIsNull()
{
// Arrange
var result = new StatusCodeHttpResult(200);
HttpContext httpContext = null;
// Act & Assert
await Assert.ThrowsAsync<ArgumentNullException>("httpContext", () => result.ExecuteAsync(httpContext));
}
[Fact]
public void StatusCodeResult_Implements_IStatusCodeHttpResult_Correctly()
{
// Act & Assert
var result = Assert.IsAssignableFrom<IStatusCodeHttpResult>(new StatusCodeHttpResult(StatusCodes.Status406NotAcceptable));
Assert.Equal(StatusCodes.Status406NotAcceptable, 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;
}
}
|