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 DefaultProtoContext(); var responseFeature = new FakeResponseFeature(); context.Features.Set <IProtoResponseFeature>(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); }
public async Task ReadFormAsync_MultipartWithEncodedFilename_ReturnsParsedFormCollection(bool bufferRequest) { var formContent = Encoding.UTF8.GetBytes(MultipartFormWithEncodedFilename); 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(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(); }
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(); }
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); }
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); }
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(); }
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(); }
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(); }