示例#1
0
        public async Task ReadFormAsync_MultipartWithFieldAndMediumFile_ReturnsParsedFormCollection(bool bufferRequest, int fileSize)
        {
            var fileContents    = CreateFile(fileSize);
            var formContent     = CreateMultipartWithFormAndFile(fileContents);
            var context         = new DefaultProtoContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IProtoResponseFeature>(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();
        }
示例#2
0
        public async Task ReadFormAsync_0ContentLength_ReturnsEmptyForm()
        {
            var context         = new DefaultProtoContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IProtoResponseFeature>(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);
        }
示例#3
0
        public async Task ReadFormAsync_MultipartWithInvalidContentDisposition_Throw()
        {
            var formContent     = Encoding.UTF8.GetBytes(MultipartFormWithInvalidContentDispositionValue);
            var context         = new DefaultProtoContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IProtoResponseFeature>(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);
        }
示例#4
0
        public async Task ReadFormAsync_MultipartWithFileAndQuotedBoundaryString_ReturnsParsedFormCollection(bool bufferRequest)
        {
            var formContent     = Encoding.UTF8.GetBytes(MultipartFormWithSpecialCharacters);
            var context         = new DefaultProtoContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IProtoResponseFeature>(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();
        }
示例#5
0
        public async Task ReadFormAsync_SimpleData_ReplacePipeReader_ReturnsParsedFormCollection(bool bufferRequest)
        {
            var formContent     = Encoding.UTF8.GetBytes("foo=bar&baz=2");
            var context         = new DefaultProtoContext();
            var responseFeature = new FakeResponseFeature();

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

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

            pipe.Writer.Complete();

            context.Request.BodyReader = pipe.Reader;

            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();
        }
示例#6
0
        public async Task ReadForm_EmptyMultipart_ReturnsParsedFormCollection(bool bufferRequest)
        {
            var formContent     = Encoding.UTF8.GetBytes(EmptyMultipartForm);
            var context         = new DefaultProtoContext();
            var responseFeature = new FakeResponseFeature();

            context.Features.Set <IProtoResponseFeature>(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();
        }