コード例 #1
0
ファイル: FormFeatureTests.cs プロジェクト: tarynt/AspNetCore
    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();
    }
コード例 #2
0
ファイル: FormFeatureTests.cs プロジェクト: tarynt/AspNetCore
    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();
    }
コード例 #3
0
ファイル: FormFeatureTests.cs プロジェクト: tarynt/AspNetCore
    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();
    }
コード例 #4
0
ファイル: FormFeatureTests.cs プロジェクト: tarynt/AspNetCore
    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();
    }
コード例 #5
0
ファイル: FormFeatureTests.cs プロジェクト: tarynt/AspNetCore
    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();
    }
コード例 #6
0
ファイル: FormFeatureTests.cs プロジェクト: tarynt/AspNetCore
    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();
    }