Пример #1
0
        public async Task ReadFormAsync_ValueCountLimitExceededWithFiles_Throw(bool bufferRequest)
        {
            var formContent = new List <byte>();

            formContent.AddRange(Encoding.UTF8.GetBytes(MultipartFormFile));
            formContent.AddRange(Encoding.UTF8.GetBytes(MultipartFormFile));
            formContent.AddRange(Encoding.UTF8.GetBytes(MultipartFormFile));
            formContent.AddRange(Encoding.UTF8.GetBytes(MultipartFormEnd));


            var context         = new DefaultHttpContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);
            context.Request.ContentType = MultipartContentType;
            context.Request.Body        = new NonSeekableReadStream(formContent.ToArray());

            IFormFeature formFeature = new FormFeature(context.Request, new FormOptions()
            {
                BufferBody = bufferRequest, ValueCountLimit = 2
            });

            context.Features.Set <IFormFeature>(formFeature);

            var exception = await Assert.ThrowsAsync <InvalidDataException>(() => context.Request.ReadFormAsync());

            Assert.Equal("Form value count limit 2 exceeded.", exception.Message);
        }
Пример #2
0
        public async Task ReadFormAsync_MultipartWithEncodedFilename_ReturnsParsedFormCollection(bool bufferRequest)
        {
            var formContent     = Encoding.UTF8.GetBytes(MultipartFormWithEncodedFilename);
            var context         = new DefaultHttpContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);
            context.Request.ContentType = MultipartContentType;
            context.Request.Body        = new NonSeekableReadStream(formContent);

            IFormFeature formFeature = new FormFeature(context.Request, new FormOptions()
            {
                BufferBody = bufferRequest
            });

            context.Features.Set <IFormFeature>(formFeature);

            var formCollection = await context.Request.ReadFormAsync();

            Assert.NotNull(formCollection);

            // Cached
            formFeature = context.Features.Get <IFormFeature>();
            Assert.NotNull(formFeature);
            Assert.NotNull(formFeature.Form);
            Assert.Same(formFeature.Form, formCollection);
            Assert.Same(formCollection, context.Request.Form);

            // Content
            Assert.Equal(0, formCollection.Count);

            Assert.NotNull(formCollection.Files);
            Assert.Equal(1, formCollection.Files.Count);

            var file = formCollection.Files["myfile1"];

            Assert.Equal("myfile1", file.Name);
            Assert.Equal("t\u00e9mp.html", file.FileName);
            Assert.Equal("text/html", file.ContentType);
            Assert.Equal(@"form-data; name=""myfile1""; filename=""temp.html""; filename*=utf-8''t%c3%a9mp.html", file.ContentDisposition);
            var body = file.OpenReadStream();

            using (var reader = new StreamReader(body))
            {
                Assert.True(body.CanSeek);
                var content = reader.ReadToEnd();
                Assert.Equal("<html><body>Hello World</body></html>", content);
            }

            await responseFeature.CompleteAsync();
        }
Пример #3
0
        public async Task ReadFormAsync_MultipartWithFieldAndMediumFile_ReturnsParsedFormCollection(bool bufferRequest, int fileSize)
        {
            var fileContents    = CreateFile(fileSize);
            var formContent     = CreateMultipartWithFormAndFile(fileContents);
            var context         = new DefaultHttpContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);
            context.Request.ContentType = MultipartContentType;
            context.Request.Body        = new NonSeekableReadStream(formContent);

            IFormFeature formFeature = new FormFeature(context.Request, new FormOptions()
            {
                BufferBody = bufferRequest
            });

            context.Features.Set <IFormFeature>(formFeature);

            var formCollection = await context.Request.ReadFormAsync();

            Assert.NotNull(formCollection);

            // Cached
            formFeature = context.Features.Get <IFormFeature>();
            Assert.NotNull(formFeature);
            Assert.NotNull(formFeature.Form);
            Assert.Same(formFeature.Form, formCollection);
            Assert.Same(formCollection, context.Request.Form);

            // Content
            Assert.Equal(1, formCollection.Count);
            Assert.Equal("Foo", formCollection["description"]);

            Assert.NotNull(formCollection.Files);
            Assert.Equal(1, formCollection.Files.Count);

            var file = formCollection.Files["myfile1"];

            Assert.Equal("text/html", file.ContentType);
            Assert.Equal(@"form-data; name=""myfile1""; filename=""temp.html""", file.ContentDisposition);
            using (var body = file.OpenReadStream())
            {
                Assert.True(body.CanSeek);
                CompareStreams(fileContents, body);
            }

            await responseFeature.CompleteAsync();
        }
Пример #4
0
        public async Task ReadFormAsync_0ContentLength_ReturnsEmptyForm()
        {
            var context         = new DefaultHttpContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);
            context.Request.ContentType   = MultipartContentType;
            context.Request.ContentLength = 0;

            var formFeature = new FormFeature(context.Request, new FormOptions());

            context.Features.Set <IFormFeature>(formFeature);

            var formCollection = await context.Request.ReadFormAsync();

            Assert.Same(FormCollection.Empty, formCollection);
        }
Пример #5
0
        public async Task ReadFormAsync_MultipartWithInvalidContentDisposition_Throw()
        {
            var formContent     = Encoding.UTF8.GetBytes(MultipartFormWithInvalidContentDispositionValue);
            var context         = new DefaultHttpContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);
            context.Request.ContentType = MultipartContentType;
            context.Request.Body        = new NonSeekableReadStream(formContent);

            IFormFeature formFeature = new FormFeature(context.Request, new FormOptions());

            context.Features.Set <IFormFeature>(formFeature);

            var exception = await Assert.ThrowsAsync <InvalidDataException>(() => context.Request.ReadFormAsync());

            Assert.Equal("Form section has invalid Content-Disposition value: " + InvalidContentDispositionValue, exception.Message);
        }
Пример #6
0
        public async Task ReadFormAsync_SimpleData_ReplacePipeReader_ReturnsParsedFormCollection(bool bufferRequest)
        {
            var formContent     = Encoding.UTF8.GetBytes("foo=bar&baz=2");
            var context         = new DefaultHttpContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);
            context.Request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";

            var pipe = new Pipe();
            await pipe.Writer.WriteAsync(formContent);

            pipe.Writer.Complete();

            var mockFeature = new MockRequestBodyPipeFeature();

            mockFeature.Reader = pipe.Reader;
            context.Features.Set <IRequestBodyPipeFeature>(mockFeature);

            IFormFeature formFeature = new FormFeature(context.Request, new FormOptions()
            {
                BufferBody = bufferRequest
            });

            context.Features.Set <IFormFeature>(formFeature);

            var formCollection = await context.Request.ReadFormAsync();

            Assert.Equal("bar", formCollection["foo"]);
            Assert.Equal("2", formCollection["baz"]);

            // Cached
            formFeature = context.Features.Get <IFormFeature>();
            Assert.NotNull(formFeature);
            Assert.NotNull(formFeature.Form);
            Assert.Same(formFeature.Form, formCollection);

            // Cleanup
            await responseFeature.CompleteAsync();
        }
Пример #7
0
        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 <IHttpResponseFeature>(responseFeature);
            context.Request.ContentType = MultipartContentTypeWithSpecialCharacters;
            context.Request.Body        = new NonSeekableReadStream(formContent);

            IFormFeature formFeature = new FormFeature(context.Request, new FormOptions()
            {
                BufferBody = bufferRequest
            });

            context.Features.Set <IFormFeature>(formFeature);

            var formCollection = context.Request.Form;

            Assert.NotNull(formCollection);

            // Cached
            formFeature = context.Features.Get <IFormFeature>();
            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();
        }
Пример #8
0
        public async Task ReadFormAsync_SimpleData_ReturnsParsedFormCollection(bool bufferRequest)
        {
            var formContent     = Encoding.UTF8.GetBytes("foo=bar&baz=2");
            var context         = new DefaultHttpContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);
            context.Request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
            context.Request.Body        = new NonSeekableReadStream(formContent);

            IFormFeature formFeature = new FormFeature(context.Request, new FormOptions()
            {
                BufferBody = bufferRequest
            });

            context.Features.Set <IFormFeature>(formFeature);

            var formCollection = await context.Request.ReadFormAsync();

            Assert.Equal("bar", formCollection["foo"]);
            Assert.Equal("2", formCollection["baz"]);
            Assert.Equal(bufferRequest, context.Request.Body.CanSeek);
            if (bufferRequest)
            {
                Assert.Equal(0, context.Request.Body.Position);
            }

            // Cached
            formFeature = context.Features.Get <IFormFeature>();
            Assert.NotNull(formFeature);
            Assert.NotNull(formFeature.Form);
            Assert.Same(formFeature.Form, formCollection);

            // Cleanup
            await responseFeature.CompleteAsync();
        }
Пример #9
0
        public async Task ReadForm_EmptyMultipart_ReturnsParsedFormCollection(bool bufferRequest)
        {
            var formContent     = Encoding.UTF8.GetBytes(EmptyMultipartForm);
            var context         = new DefaultHttpContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);
            context.Request.ContentType = MultipartContentType;
            context.Request.Body        = new NonSeekableReadStream(formContent);

            IFormFeature formFeature = new FormFeature(context.Request, new FormOptions()
            {
                BufferBody = bufferRequest
            });

            context.Features.Set <IFormFeature>(formFeature);

            var formCollection = context.Request.Form;

            Assert.NotNull(formCollection);

            // Cached
            formFeature = context.Features.Get <IFormFeature>();
            Assert.NotNull(formFeature);
            Assert.NotNull(formFeature.Form);
            Assert.Same(formCollection, formFeature.Form);
            Assert.Same(formCollection, await context.Request.ReadFormAsync());

            // Content
            Assert.Equal(0, formCollection.Count);
            Assert.NotNull(formCollection.Files);
            Assert.Equal(0, formCollection.Files.Count);

            // Cleanup
            await responseFeature.CompleteAsync();
        }