Gillardo
Gillardo

Reputation: 9818

MaxRequestSize attribute not stopping request aspnet dotnet core 3

I have created a sample project in which i want to call a post request with a file, in my live version this is an action in which users upload a file, but in my example, i just return a result of Created.

When i add the attribute RequestSizeLimit and run this code on my website, files over 100MB are not uploaded and the call to this request fails. However, if i run the project using a TestServerFixture (as i am running this for integration tests) then the method doesnt fail?

I have attached the code for the normal and test project to this issue, so you can download, restore the packages and just run the tests.

Click here to download files

As you will see the test called "should_return_bad_request_when_file_over_100_mb" which fails.

Here is the basics of the code.

FileController in the api site

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!");
        }
    }
}

The test methods

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);
        }
    }
}

Here are the references in my test project

<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>

Does anyone have any idea why the second test doesnt fail? Any help would be much appreciated.

Upvotes: 0

Views: 2390

Answers (1)

Rena
Rena

Reputation: 36655

It is by design that Test Server does not support RequestSizeLimit.

You could see the following log by running your testing project:

Microsoft.AspNetCore.Mvc.Filters.RequestSizeLimitFilter: Warning: A request body size limit could not be applied. This server does not support the IHttpRequestBodySizeFeature.

UPDATE:

Add the following code and then you could see the log:

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

Upvotes: 2

Related Questions