【问题标题】:MaxRequestSize attribute not stopping request aspnet dotnet core 3MaxRequestSize 属性不停止请求 aspnet dotnet core 3
【发布时间】:2020-01-20 10:47:42
【问题描述】:

我创建了一个示例项目,我想在其中调用带有文件的post 请求,在我的实时版本中,这是用户上传文件的操作,但在我的示例中,我只返回结果Created.

当我添加属性RequestSizeLimit 并在我的网站上运行此代码时,不会上传超过 100MB 的文件,并且对此请求的调用失败。但是,如果我使用 TestServerFixture 运行项目(因为我正在运行它以进行集成测试),那么该方法不会失败?

我已将正常项目和测试项目的代码附加到此问题,因此您可以下载、恢复包并运行测试。

点击here下载文件

您将看到名为“should_return_bad_request_when_file_over_100_mb”的测试失败。

这里是基本的代码。

api 站点中的文件控制器

using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;

namespace UploadAFile.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class FileController : ControllerBase
    {
        [HttpPost("upload")]
        [RequestSizeLimit(105906176)] // 101 MB
        public IActionResult PostFile()
        {
            return Created(Request.GetDisplayUrl(), "Yes it worked!");
        }
    }
}

测试方法

using System.Net.Http;
using Xunit;

namespace Tests
{
    public class TestFileController
    {
        [Fact]
        public async void should_return_success_when_file_1_kb()
        {
            var fixture = new TestServerFixture();

            // get a file to upload
            var bytes = new byte[1024]; // 1 KB
            HttpContent bytesContent = new ByteArrayContent(bytes);

            // act
            var client = fixture.CreateClient();
            var formData = new MultipartFormDataContent();
            formData.Add(bytesContent, "bigfile.txt", "bigfile.txt");
            var response = await client.PostAsync("file/upload", formData);

            // assert
            Assert.Equal(System.Net.HttpStatusCode.Created, response.StatusCode);
        }

        [Fact]
        public async void should_return_bad_request_when_file_over_100_mb()
        {
            var fixture = new TestServerFixture();

            // get a file to upload
            var bytes = new byte[106954752]; // 102 MB
            HttpContent bytesContent = new ByteArrayContent(bytes);

            // act
            var client = fixture.CreateClient();
            var formData = new MultipartFormDataContent();
            formData.Add(bytesContent, "bigfile.txt", "bigfile.txt");
            var response = await client.PostAsync("file/upload", formData);

            // assert
            Assert.Equal(System.Net.HttpStatusCode.BadRequest, response.StatusCode);
        }
    }
}

这是我的测试项目中的引用

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.0.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
    <PackageReference Include="coverlet.collector" Version="1.0.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\UploadAFile\UploadAFile.csproj" />
  </ItemGroup>

</Project>

有人知道为什么第二次测试没有失败吗?任何帮助将不胜感激。

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc asp.net-core


    【解决方案1】:

    Test Server 不支持 RequestSizeLimit 是设计使然。

    您可以通过运行测试项目看到以下日志:

    Microsoft.AspNetCore.Mvc.Filters.RequestSizeLimitFilter:警告:无法应用请求正文大小限制。此服务器不支持 IHttpRequestBodySizeFeature。

    更新:

    添加以下代码即可看到日志:

    var builder = new WebHostBuilder()
                .UseContentRoot(System.IO.Directory.GetCurrentDirectory())
                .ConfigureLogging(config =>
                {
                    config.SetMinimumLevel(LogLevel.Debug);
                    config.AddConsole();
                    config.AddDebug();
                })
                .UseTestServer()
                .UseStartup<Startup>();
    

    【讨论】:

    • 非常感谢,出于好奇,您是在哪里看到这条消息的?
    • 嗨@Gillardo,请检查我的回答。
    • 那么我们应该如何测试这种类型的配置呢?
    猜你喜欢
    • 2019-10-13
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    相关资源
    最近更新 更多