将 FromForm 绑定到 IFormFile 属性的 C# 集成测试控制器

Gre*_*uff 4 c# multipartform-data asp.net-core

我正在使用:

  • Visual Studio 2017 专业版
  • dotnet 核心 SDK 2.2.102
  • XUnit 2.4.1

我想做什么

集成测试接受表单数据的 API 控制器方法。

设置

  • 我的控制器中的 API 路由使用[FromForm]属性接受 CommandObject
  • CommandObject 的属性之一是类型 List<IFormFile>,它旨在管理属于请求的任何文件
  • 当我从 Postman 手动测试时,Controller 方法按预期工作。

问题

文件不绑定到List<IFormFile>属性。其他一切都按预期工作,但文件没有。这是我第一次使用 Multipart Form Data,所以不知道该尝试什么。

当我调试测试时,你可以看到除了Documents属性之外的一切都正常(注意,这与下面的代码不 100% 匹配,因为我不得不混淆一些东西)

在此处输入图片说明

我看过的东西

有很多与多部分表单数据相关的东西,我尝试过的一些解决方案是:

MyIntegrationTest.cs

我的集成测试设置背后有很多代码。如果我把它都贴在这里,我认为它不会很有帮助。最重要的一条信息是变量server的类型Microsoft.AspNetCore.TestHost.TestServer

[Fact]
async Task Post_ItemAsync_HappyPath_ReturnsOKStatusCode()
{
    var fileDir = @"C:/path/to/files";
    var fileNames = new string[] { "test.docx", "test.txt" };

    using (var server = CreateTestServer())
    {
        // Arrange
        var formData = new MultipartFormDataContent()
        {
            { new StringContent("Test Title"), "Title" },
            { new StringContent("Test Description"), "Description" },
            { new StringContent("String_1"), "AListOfStrings" },
            { new StringContent("String_2"), "AListOfStrings" },
            { new StringContent("3"), "NumberOfThings" }
        };

        foreach (var fileName in fileNames)
        {
            var document = File.ReadAllBytes($"{fileDir}/{fileName}");
            formData.Add(new ByteArrayContent(document), "file", fileName);
        }

        string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
        string contentType = "multipart/form-data; boundary=" + formDataBoundary;

        var request = new HttpRequestMessage(HttpMethod.Post, "api/v1/item")
        {
            Headers =
            {
                { HttpRequestHeader.ContentType.ToString(), contentType }
            },
            Content = formData
        };

        // Act
        var response = await server.CreateClient().SendAsync(request);

        // Assert
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);

        // Cleanup
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我的控制器

[HttpPost]
ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> CreateItemAsync([FromForm]CreateItemCommand command)
{
    bool commandResult = false;

    commandResult = await _mediator.Send(command);

    if (!commandResult)
    {
        return BadRequest();
    }

    return Ok();
}
Run Code Online (Sandbox Code Playgroud)

创建项目命令.cs

[DataContract]
public class CreateItemCommand
    :IRequest<bool>
{
    [DataMember]
    public string Title { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]
    public HashSet<string> AListOfThings { get; set; }

    [DataMember]
    public int NumberOfThings { get; set; }

    [DataMember]
    public List<IFormFile> Documents { get; private set; }

    public CreateITemCommand()
    {
        AListOfThings = new HashSet<string>();
    }

    public CreateItemCommand(string title, string description, HashSet<string> aListOfThings, int NumberOfThings, List<IFormFile> documents)
        : this()
    {
        Title = title;
        Description = description;
        AListOfStrings = aListOfStrings;
        NumberOfThings = numberOfThings;
        Documents = documents;
    }
}
Run Code Online (Sandbox Code Playgroud)

Nko*_*osi 5

表单数据边界应MultipartFormDataContent在初始化时添加到 ,并且文件的名称需要与要填充的模型所需的属性相匹配。

//...

// Arrange
string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());

var formData = new MultipartFormDataContent(formDataBoundary) { //<---- NOTE HERE
    { new StringContent("Test Title"), "Title" },
    { new StringContent("Test Description"), "Description" },
    { new StringContent("String_1"), "AListOfStrings" },
    { new StringContent("String_2"), "AListOfStrings" },
    { new StringContent("3"), "NumberOfThings" }
};

foreach (var fileName in fileNames) {
    var document = File.ReadAllBytes($"{fileDir}/{fileName}");
    formData.Add(new ByteArrayContent(document), "Documents", fileName); //<-- NOTE HERE
}

// Act
var response = await server.CreateClient().PostAsync("api/v1/item", formData);

//...
Run Code Online (Sandbox Code Playgroud)

模型的Documents属性需要设置为 public 以便模型绑定器可以在解析表单数据时填充它。