From 5ec8a7134eaec5e878771e69b5b9b9c913ef9afe Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Mon, 7 Aug 2017 14:29:33 -0700 Subject: [PATCH] Adds FormFeature quoted boundary test --- .../Features/FormFeatureTests.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/test/Microsoft.AspNetCore.Http.Tests/Features/FormFeatureTests.cs b/test/Microsoft.AspNetCore.Http.Tests/Features/FormFeatureTests.cs index 9cf4f872ce..591f46a43e 100644 --- a/test/Microsoft.AspNetCore.Http.Tests/Features/FormFeatureTests.cs +++ b/test/Microsoft.AspNetCore.Http.Tests/Features/FormFeatureTests.cs @@ -49,11 +49,15 @@ namespace Microsoft.AspNetCore.Http.Features private const string MultipartContentType = "multipart/form-data; boundary=WebKitFormBoundary5pDRpGheQXaM8k3T"; + private const string MultipartContentTypeWithSpecialCharacters = "multipart/form-data; boundary=\"WebKitFormBoundary/:5pDRpGheQXaM8k3T\""; + private const string EmptyMultipartForm = "--WebKitFormBoundary5pDRpGheQXaM8k3T--"; // Note that CRLF (\r\n) is required. You can't use multi-line C# strings here because the line breaks on Linux are just LF. private const string MultipartFormEnd = "--WebKitFormBoundary5pDRpGheQXaM8k3T--\r\n"; + private const string MultipartFormEndWithSpecialCharacters = "--WebKitFormBoundary/:5pDRpGheQXaM8k3T--\r\n"; + private const string MultipartFormField = "--WebKitFormBoundary5pDRpGheQXaM8k3T\r\n" + "Content-Disposition: form-data; name=\"description\"\r\n" + "\r\n" + @@ -71,6 +75,12 @@ namespace Microsoft.AspNetCore.Http.Features "\r\n" + "Hello World\r\n"; + private const string MultipartFormFileSpecialCharacters = "--WebKitFormBoundary/:5pDRpGheQXaM8k3T\r\n" + +"Content-Disposition: form-data; name=\"description\"\r\n" + +"\r\n" + +"Foo\r\n"; + + private const string MultipartFormWithField = MultipartFormField + MultipartFormEnd; @@ -88,6 +98,10 @@ namespace Microsoft.AspNetCore.Http.Features MultipartFormEncodedFilename + MultipartFormEnd; + private const string MultipartFormWithSpecialCharacters = + MultipartFormFileSpecialCharacters + + MultipartFormEndWithSpecialCharacters; + [Theory] [InlineData(true)] [InlineData(false)] @@ -208,6 +222,43 @@ namespace Microsoft.AspNetCore.Http.Features await responseFeature.CompleteAsync(); } + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task ReadFormAsync_MultipartWithFileAndQuotedBoundaryString_ReturnsParsedFormCollection(bool bufferRequest) + { + var formContent = Encoding.UTF8.GetBytes(MultipartFormWithSpecialCharacters); + var context = new DefaultHttpContext(); + var responseFeature = new FakeResponseFeature(); + context.Features.Set(responseFeature); + context.Request.ContentType = MultipartContentTypeWithSpecialCharacters; + context.Request.Body = new NonSeekableReadStream(formContent); + + IFormFeature formFeature = new FormFeature(context.Request, new FormOptions() { BufferBody = bufferRequest }); + context.Features.Set(formFeature); + + var formCollection = context.Request.Form; + + Assert.NotNull(formCollection); + + // Cached + formFeature = context.Features.Get(); + Assert.NotNull(formFeature); + Assert.NotNull(formFeature.Form); + Assert.Same(formCollection, formFeature.Form); + Assert.Same(formCollection, await context.Request.ReadFormAsync()); + + // Content + Assert.Equal(1, formCollection.Count); + Assert.Equal("Foo", formCollection["description"]); + + Assert.NotNull(formCollection.Files); + Assert.Equal(0, formCollection.Files.Count); + + // Cleanup + await responseFeature.CompleteAsync(); + } + [Theory] [InlineData(true)] [InlineData(false)]