File: EmptyResultTests.cs
Web Access
Project: src\src\Http\Http.Results\test\Microsoft.AspNetCore.Http.Results.Tests.csproj (Microsoft.AspNetCore.Http.Results.Tests)
// 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;
 
namespace Microsoft.AspNetCore.Http.HttpResults;
 
public class EmptyResultTests
{
    [Fact]
    public async Task EmptyResult_DoesNothing()
    {
        // Arrange
        var emptyResult = EmptyHttpResult.Instance;
 
        // Act
        var httpContext = GetHttpContext();
        var memoryStream = httpContext.Response.Body as MemoryStream;
        await emptyResult.ExecuteAsync(httpContext);
 
        // Assert
        Assert.Equal(StatusCodes.Status200OK, httpContext.Response.StatusCode);
        Assert.Equal(0, memoryStream.Length);
    }
 
    [Fact]
    public async Task ExecuteAsync_ThrowsArgumentNullException_WhenHttpContextIsNull()
    {
        // Arrange
        var result = EmptyHttpResult.Instance;
        HttpContext httpContext = null;
 
        // Act & Assert
        await Assert.ThrowsAsync<ArgumentNullException>("httpContext", () => result.ExecuteAsync(httpContext));
    }
 
    private static HttpContext GetHttpContext()
    {
        var httpContext = new DefaultHttpContext();
        httpContext.Request.PathBase = new PathString("");
        httpContext.Response.Body = new MemoryStream();
        httpContext.RequestServices = CreateServices();
        return httpContext;
    }
 
    private static IServiceProvider CreateServices()
    {
        return new ServiceCollection().BuildServiceProvider();
    }
}