|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.AspNetCore.Builder; |
| 9 | +using Microsoft.AspNetCore.Hosting; |
| 10 | +using Microsoft.AspNetCore.Http; |
| 11 | +using Microsoft.AspNetCore.Http.Features; |
| 12 | +using Microsoft.AspNetCore.TestHost; |
| 13 | +using Microsoft.Azure.WebJobs.Script.WebHost.Middleware; |
| 14 | +using Microsoft.Extensions.DependencyInjection; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Middleware |
| 18 | +{ |
| 19 | + public class HttpRequestBodySizeMiddlewareTests |
| 20 | + { |
| 21 | + [Theory] |
| 22 | + [InlineData("", false)] |
| 23 | + [InlineData(null, false)] |
| 24 | + [InlineData("invalid", false)] |
| 25 | + [InlineData("1024", true)] |
| 26 | + public async Task MaxRequestBodySizeLimit_RequestBodySizeLimit_SetExpectedValue(string requestBodySizeLimit, bool validData) |
| 27 | + { |
| 28 | + var environment = new TestEnvironment(); |
| 29 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.FunctionsRequestBodySizeLimit, requestBodySizeLimit); |
| 30 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "0"); |
| 31 | + |
| 32 | + bool nextInvoked = false; |
| 33 | + long? configuredLimit = 0; |
| 34 | + RequestDelegate next = (ctxt) => |
| 35 | + { |
| 36 | + nextInvoked = true; |
| 37 | + ctxt.Response.StatusCode = (int)HttpStatusCode.Accepted; |
| 38 | + configuredLimit = ctxt.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize; |
| 39 | + return Task.CompletedTask; |
| 40 | + }; |
| 41 | + |
| 42 | + var middleware = new HttpRequestBodySizeMiddleware(next, environment); |
| 43 | + |
| 44 | + var httpContext = new DefaultHttpContext(); |
| 45 | + |
| 46 | + httpContext.Features.Set<IHttpMaxRequestBodySizeFeature>(new TestHttpMaxRequestBodySizeFeature()); |
| 47 | + await middleware.Invoke(httpContext); |
| 48 | + Assert.True(nextInvoked); |
| 49 | + |
| 50 | + if (validData) |
| 51 | + { |
| 52 | + Assert.Equal(configuredLimit, long.Parse(requestBodySizeLimit)); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + Assert.Equal(configuredLimit, ScriptConstants.DefaultMaxRequestBodySize); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + [Theory] |
| 61 | + [InlineData("")] |
| 62 | + [InlineData(null)] |
| 63 | + [InlineData("invalid")] |
| 64 | + [InlineData("1024")] |
| 65 | + public async Task MaxRequestBodySizeLimit_InPlaceHolderMode_DoesNotUpdateConfiguration(string requestBodySizeLimit) |
| 66 | + { |
| 67 | + var environment = new TestEnvironment(); |
| 68 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.FunctionsRequestBodySizeLimit, requestBodySizeLimit); |
| 69 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1"); |
| 70 | + |
| 71 | + bool nextInvoked = false; |
| 72 | + long? configuredLimit = 0; |
| 73 | + RequestDelegate next = (ctxt) => |
| 74 | + { |
| 75 | + nextInvoked = true; |
| 76 | + ctxt.Response.StatusCode = (int)HttpStatusCode.Accepted; |
| 77 | + configuredLimit = ctxt.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize; |
| 78 | + return Task.CompletedTask; |
| 79 | + }; |
| 80 | + |
| 81 | + var middleware = new HttpRequestBodySizeMiddleware(next, environment); |
| 82 | + |
| 83 | + var httpContext = new DefaultHttpContext(); |
| 84 | + |
| 85 | + httpContext.Features.Set<IHttpMaxRequestBodySizeFeature>(new TestHttpMaxRequestBodySizeFeature()); |
| 86 | + await middleware.Invoke(httpContext); |
| 87 | + Assert.True(nextInvoked); |
| 88 | + Assert.Equal(configuredLimit, ScriptConstants.DefaultMaxRequestBodySize); |
| 89 | + } |
| 90 | + |
| 91 | + [Theory] |
| 92 | + [InlineData(true, true)] |
| 93 | + [InlineData(true, false)] |
| 94 | + [InlineData(false, false)] |
| 95 | + [InlineData(false, true)] |
| 96 | + public async Task MaxRequestBodySizeLimit_MiddleWareInvoke_GetWiredUpCorrectly(bool inPlaceHolderMode, bool isLimitSet) |
| 97 | + { |
| 98 | + bool nextInvoked = false; |
| 99 | + long? configuredLimit = 0; |
| 100 | + RequestDelegate next = (ctxt) => |
| 101 | + { |
| 102 | + nextInvoked = true; |
| 103 | + ctxt.Response.StatusCode = (int)HttpStatusCode.Accepted; |
| 104 | + configuredLimit = ctxt.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize; |
| 105 | + return Task.CompletedTask; |
| 106 | + }; |
| 107 | + |
| 108 | + var environment = new TestEnvironment(); |
| 109 | + |
| 110 | + string placeHolderModeValue = inPlaceHolderMode ? "1" : "0"; |
| 111 | + string limit = isLimitSet ? "100" : "invalid"; |
| 112 | + |
| 113 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, placeHolderModeValue); |
| 114 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.FunctionsRequestBodySizeLimit, limit); |
| 115 | + |
| 116 | + var middleware = new HttpRequestBodySizeMiddleware(next, environment); |
| 117 | + var httpContext = new DefaultHttpContext(); |
| 118 | + |
| 119 | + httpContext.Features.Set<IHttpMaxRequestBodySizeFeature>(new TestHttpMaxRequestBodySizeFeature()); |
| 120 | + await middleware.Invoke(httpContext); |
| 121 | + Assert.True(nextInvoked); |
| 122 | + |
| 123 | + if (inPlaceHolderMode) |
| 124 | + { |
| 125 | + Assert.Equal(middleware.InnerInvoke, middleware.InvokeBeforeSpecialization); |
| 126 | + } |
| 127 | + |
| 128 | + if (!inPlaceHolderMode && isLimitSet) |
| 129 | + { |
| 130 | + Assert.Equal(middleware.InnerInvoke, middleware.InvokeAfterSpecialization); |
| 131 | + } |
| 132 | + |
| 133 | + if (!inPlaceHolderMode && !isLimitSet) |
| 134 | + { |
| 135 | + Assert.Equal(middleware.InnerInvoke, next); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public class TestHttpMaxRequestBodySizeFeature : IHttpMaxRequestBodySizeFeature |
| 140 | + { |
| 141 | + private long? _maxRequestBodySize = ScriptConstants.DefaultMaxRequestBodySize; |
| 142 | + |
| 143 | + public bool IsReadOnly => false; |
| 144 | + |
| 145 | + long? IHttpMaxRequestBodySizeFeature.MaxRequestBodySize { get => _maxRequestBodySize; set => _maxRequestBodySize = value; } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments