<commit_before>// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IIS.FunctionalTests.Utilities;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Server.IntegrationTesting.IIS;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;

namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests.InProcess
{
    [Collection(PublishedSitesCollection.Name)]
    public class MaxRequestBodySizeTests : IISFunctionalTestBase
    {
        public MaxRequestBodySizeTests(PublishedSitesFixture fixture) : base(fixture)
        {
        }

        [ConditionalFact]
        [RequiresNewHandler]
        public async Task MaxRequestBodySizeE2EWorks()
        {
            var deploymentParameters = Fixture.GetBaseDeploymentParameters();
            deploymentParameters.TransformArguments((a, _) => $"{a} DecreaseRequestLimit");

            var deploymentResult = await DeployAsync(deploymentParameters);

            var result = await deploymentResult.HttpClient.PostAsync("/ReadRequestBody", new StringContent("test"));
            Assert.Equal(HttpStatusCode.RequestEntityTooLarge, result.StatusCode);
        }

        [ConditionalFact]
        [RequiresNewHandler]
        public async Task SetIISLimitMaxRequestBodySizeE2EWorks()
        {
            var deploymentParameters = Fixture.GetBaseDeploymentParameters();
            deploymentParameters.ServerConfigActionList.Add(
                (config, _) => {
                    config
                        .RequiredElement("system.webServer")
                        .GetOrAdd("security")
                        .GetOrAdd("requestFiltering")
                        .GetOrAdd("requestLimits", "maxAllowedContentLength", "1");
                });
            var deploymentResult = await DeployAsync(deploymentParameters);

            var result = await deploymentResult.HttpClient.PostAsync("/ReadRequestBody", new StringContent("test"));

            // IIS returns a 404 instead of a 413... 
            Assert.Equal(HttpStatusCode.NotFound, result.StatusCode);
        }

        [ConditionalFact]
        [RequiresNewHandler]
        public async Task IISRejectsContentLengthTooLargeByDefault()
        {
            var deploymentParameters = Fixture.GetBaseDeploymentParameters();
            var deploymentResult = await DeployAsync(deploymentParameters);

            using (var connection = new TestConnection(deploymentResult.HttpClient.BaseAddress.Port))
            {
                await connection.Send(
                    "POST /HelloWorld HTTP/1.1",
                    $"Content-Length: 30000001",
                    "Host: localhost",
                    "",
                    "A");
                await connection.Receive("HTTP/1.1 404 Not Found");
            }
        }

        [ConditionalFact]
        [RequiresNewHandler]
        [RequiresIIS(IISCapability.PoolEnvironmentVariables)]
        public async Task SetIISLimitMaxRequestBodyLogsWarning()
        {
            var deploymentParameters = Fixture.GetBaseDeploymentParameters();
            deploymentParameters.ServerConfigActionList.Add(
                (config, _) => {
                    config
                        .RequiredElement("system.webServer")
                        .GetOrAdd("security")
                        .GetOrAdd("requestFiltering")
                        .GetOrAdd("requestLimits", "maxAllowedContentLength", "1");
                });
            var deploymentResult = await DeployAsync(deploymentParameters);

            var result = await deploymentResult.HttpClient.PostAsync("/DecreaseRequestLimit", new StringContent("1"));
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);

            StopServer();

            if (deploymentParameters.ServerType == ServerType.IISExpress)
            {
                Assert.Single(TestSink.Writes, w => w.Message.Contains("Increasing the MaxRequestBodySize conflicts with the max value for IIS limit maxAllowedContentLength." +
                    " HTTP requests that have a content length greater than maxAllowedContentLength will still be rejected by IIS." +
                    " You can disable the limit by either removing or setting the maxAllowedContentLength value to a higher limit."));
            }
        }
    }<commit_msg>Fix Max Request Body Size test (#12981)<commit_after>// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IIS.FunctionalTests.Utilities;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Server.IntegrationTesting.IIS;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;

namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests.InProcess
{
    [Collection(PublishedSitesCollection.Name)]
    public class MaxRequestBodySizeTests : IISFunctionalTestBase
    {
        public MaxRequestBodySizeTests(PublishedSitesFixture fixture) : base(fixture)
        {
        }

        [ConditionalFact]
        [RequiresNewHandler]
        public async Task MaxRequestBodySizeE2EWorks()
        {
            var deploymentParameters = Fixture.GetBaseDeploymentParameters();
            deploymentParameters.TransformArguments((a, _) => $"{a} DecreaseRequestLimit");

            var deploymentResult = await DeployAsync(deploymentParameters);

            var result = await deploymentResult.HttpClient.PostAsync("/ReadRequestBody", new StringContent("test"));
            Assert.Equal(HttpStatusCode.RequestEntityTooLarge, result.StatusCode);
        }

        [ConditionalFact]
        [RequiresNewHandler]
        public async Task SetIISLimitMaxRequestBodySizeE2EWorks()
        {
            var deploymentParameters = Fixture.GetBaseDeploymentParameters();
            deploymentParameters.ServerConfigActionList.Add(
                (config, _) => {
                    config
                        .RequiredElement("system.webServer")
                        .GetOrAdd("security")
                        .GetOrAdd("requestFiltering")
                        .GetOrAdd("requestLimits", "maxAllowedContentLength", "1");
                });
            var deploymentResult = await DeployAsync(deploymentParameters);

            var result = await deploymentResult.HttpClient.PostAsync("/ReadRequestBody", new StringContent("test"));

            // IIS returns a 404 instead of a 413... 
            Assert.Equal(HttpStatusCode.NotFound, result.StatusCode);
        }

        [ConditionalFact]
        [RequiresNewHandler]
        public async Task IISRejectsContentLengthTooLargeByDefault()
        {
            var deploymentParameters = Fixture.GetBaseDeploymentParameters();
            var deploymentResult = await DeployAsync(deploymentParameters);

            using (var connection = new TestConnection(deploymentResult.HttpClient.BaseAddress.Port))
            {
                await connection.Send(
                    "POST /HelloWorld HTTP/1.1",
                    $"Content-Length: 30000001",
                    "Host: localhost",
                    "",
                    "A");
                await connection.Receive("HTTP/1.1 404 Not Found");
            }
        }

        [ConditionalFact]
        [RequiresNewHandler]
        [RequiresIIS(IISCapability.PoolEnvironmentVariables)]
        public async Task SetIISLimitMaxRequestBodyLogsWarning()
        {
            var deploymentParameters = Fixture.GetBaseDeploymentParameters();

            // Logs get tangled up due to ANCM debug logs and managed logs logging at the same time.
            // Disable it for this test as we are trying to verify a log.
            deploymentParameters.HandlerSettings["debugLevel"] = "";
            deploymentParameters.ServerConfigActionList.Add(
                (config, _) => {
                    config
                        .RequiredElement("system.webServer")
                        .GetOrAdd("security")
                        .GetOrAdd("requestFiltering")
                        .GetOrAdd("requestLimits", "maxAllowedContentLength", "1");
                });
            var deploymentResult = await DeployAsync(deploymentParameters);

            var result = await deploymentResult.HttpClient.PostAsync("/DecreaseRequestLimit", new StringContent("1"));
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);

            StopServer();

            if (deploymentParameters.ServerType == ServerType.IISExpress)
            {
                Assert.Single(TestSink.Writes, w => w.Message.Contains("Increasing the MaxRequestBodySize conflicts with the max value for IIS limit maxAllowedContentLength." +
                    " HTTP requests that have a content length greater than maxAllowedContentLength will still be rejected by IIS." +
                    " You can disable the limit by either removing or setting the maxAllowedContentLength value to a higher limit."));
            }
        }
    }
{'fraction_non_alphanumeric': 0.08294930875576037, 'fraction_numerical': 0.006537348622870003, 'mean_word_length': 2.2697967764540996, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4336598', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2166, 'n_words': 493}