public async Task ReadFormAsync_MultipartWithFieldAndFile_ReturnsParsedFormCollection(bool bufferRequest) { var formContent = Encoding.UTF8.GetBytes(MultipartFormWithFieldAndFile); 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); 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 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(); }
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); }
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); }
public HttpContext Create(IFeatureCollection featureCollection) { if (featureCollection == null) { throw new ArgumentNullException(nameof(featureCollection)); } var responseCookiesFeature = new ResponseCookiesFeature(featureCollection, _builderPool); featureCollection.Set<IResponseCookiesFeature>(responseCookiesFeature); var httpContext = new DefaultHttpContext(featureCollection); if (_httpContextAccessor != null) { _httpContextAccessor.HttpContext = httpContext; } var formFeature = new FormFeature(httpContext.Request, _formOptions); featureCollection.Set<IFormFeature>(formFeature); return httpContext; }
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(); }
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(); }
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(); }
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(); }