Cherry-pick @pranavkm's functional test for #7562
- 30a5eda508 / origin/prkrishn/form-file-value-provider Was: Design: Use a value provider to allow nested form files Fixes https://github.com/aspnet/Mvc/issues/7562
This commit is contained in:
parent
47d6d4e82c
commit
c73b13f544
|
|
@ -4,6 +4,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -623,6 +624,41 @@ Hello from /Pages/Shared/";
|
|||
Assert.Equal("Value from Page", valueSetInPage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RoundTrippingFormFileInputWorks()
|
||||
{
|
||||
// Arrange
|
||||
var url = "/PropertyBinding/BindFormFile";
|
||||
var response = await Client.GetAsync(url);
|
||||
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
|
||||
|
||||
var document = await response.GetHtmlDocumentAsync();
|
||||
|
||||
var property1 = document.RequiredQuerySelector("#property1").GetAttribute("name");
|
||||
var file1 = document.RequiredQuerySelector("#file1").GetAttribute("name");
|
||||
var file2 = document.RequiredQuerySelector("#file2").GetAttribute("name");
|
||||
var file3 = document.RequiredQuerySelector("#file3").GetAttribute("name");
|
||||
var antiforgeryToken = document.RetrieveAntiforgeryToken();
|
||||
|
||||
var cookie = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(response);
|
||||
|
||||
var content = new MultipartFormDataContent();
|
||||
content.Add(new StringContent("property1-value"), property1);
|
||||
content.Add(new StringContent("test-value1"), file1, "test1.txt");
|
||||
content.Add(new StringContent("test-value2"), file3, "test2.txt");
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, url)
|
||||
{
|
||||
Content = content,
|
||||
};
|
||||
request.Headers.Add("Cookie", cookie.Key + "=" + cookie.Value);
|
||||
request.Headers.Add("RequestVerificationToken", antiforgeryToken);
|
||||
|
||||
response = await Client.SendAsync(request);
|
||||
|
||||
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
private async Task AddAntiforgeryHeadersAsync(HttpRequestMessage request)
|
||||
{
|
||||
var response = await Client.GetAsync(request.RequestUri);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
@page
|
||||
@model BindFormFile
|
||||
@addTagHelper "*, Microsoft.AspNetCore.Mvc.TagHelpers"
|
||||
|
||||
<form method="post" enctype="multipart/form-data">
|
||||
<input id="property1" asp-for="Property1" />
|
||||
<input id="file1" asp-for="Forms.Form1" />
|
||||
<input id="file2" asp-for="Forms.Form2" />
|
||||
<input id="file3" asp-for="Form3" />
|
||||
</form>
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
|
||||
namespace RazorPagesWebSite
|
||||
{
|
||||
[BindProperties]
|
||||
public class BindFormFile : PageModel
|
||||
{
|
||||
public string Property1 { get; set; }
|
||||
|
||||
public IFormFile Form3 { get; set; }
|
||||
|
||||
public FormFiles Forms { get; set; }
|
||||
|
||||
public IActionResult OnPost()
|
||||
{
|
||||
if (string.IsNullOrEmpty(Property1))
|
||||
{
|
||||
throw new Exception($"{nameof(Property1)} is not bound.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(Form3.Name) || Form3.Length == 0)
|
||||
{
|
||||
throw new Exception($"{nameof(Form3)} is not bound.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(Forms.Form1.Name) || Forms.Form1.Length == 0)
|
||||
{
|
||||
throw new Exception($"{nameof(Forms.Form1)} is not bound.");
|
||||
}
|
||||
|
||||
if (Forms.Form2 != null)
|
||||
{
|
||||
throw new Exception($"{nameof(Forms.Form2)} is bound.");
|
||||
}
|
||||
|
||||
return new OkResult();
|
||||
}
|
||||
}
|
||||
|
||||
public class FormFiles
|
||||
{
|
||||
public IFormFile Form1 { get; set; }
|
||||
|
||||
public IFormFile Form2 { get; set; }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue