[Fixes #4099] Add a functional test for creating a form with a file upload field
This commit is contained in:
parent
abafceab52
commit
5423dc8c34
|
|
@ -0,0 +1,51 @@
|
||||||
|
// 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.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
|
{
|
||||||
|
public class FormFileUploadTest : IClassFixture<MvcTestFixture<FilesWebSite.Startup>>
|
||||||
|
{
|
||||||
|
public FormFileUploadTest(MvcTestFixture<FilesWebSite.Startup> fixture)
|
||||||
|
{
|
||||||
|
Client = fixture.Client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpClient Client { get; }
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CanUploadFileInFrom()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var content = new MultipartFormDataContent();
|
||||||
|
content.Add(new StringContent("John"), "Name");
|
||||||
|
content.Add(new StringContent("23"), "Age");
|
||||||
|
content.Add(new StringContent("John's biography content"), "Biography", "Bio.txt");
|
||||||
|
|
||||||
|
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/UploadFiles");
|
||||||
|
request.Content = content;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await Client.SendAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var user = await response.Content.ReadAsAsync<User>();
|
||||||
|
|
||||||
|
Assert.Equal("John", user.Name);
|
||||||
|
Assert.Equal(23, user.Age);
|
||||||
|
Assert.Equal("John's biography content", user.Biography);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class User
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public int Age { get; set; }
|
||||||
|
public string Biography { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
// 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.Threading.Tasks;
|
||||||
|
using FilesWebSite.Models;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace FilesWebSite.Controllers
|
||||||
|
{
|
||||||
|
public class UploadFilesController : Controller
|
||||||
|
{
|
||||||
|
[HttpPost("UploadFiles")]
|
||||||
|
public async Task<IActionResult> Post(User user)
|
||||||
|
{
|
||||||
|
var resultUser = new
|
||||||
|
{
|
||||||
|
Name = user.Name,
|
||||||
|
Age = user.Age,
|
||||||
|
Biography = await user.ReadBiography()
|
||||||
|
};
|
||||||
|
|
||||||
|
return Json(resultUser);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
// 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.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
namespace FilesWebSite.Models
|
||||||
|
{
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public int Age { get; set; }
|
||||||
|
public IFormFile Biography { get; set; }
|
||||||
|
|
||||||
|
public async Task<string> ReadBiography()
|
||||||
|
{
|
||||||
|
if (Biography != null)
|
||||||
|
{
|
||||||
|
using (var reader = new StreamReader(Biography.OpenReadStream()))
|
||||||
|
{
|
||||||
|
return await reader.ReadToEndAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue