'Posting form file to ASP .NET Core API using AWS SDK Test Lambda Context and APIGatewayProxyRequest

I have an ASP .NET Core API endpoint which accepts the below model:

public class FileUploadModel
{
    public IFormFile? File { get; set; }
    public DateTime? Time { get; set; }
    public string Comment { get; set; }
}

The project has been created via aws lambda template. I am trying to write a test scenario, which asserts the endpoint behavior when I am sending a request to it. For this purpose, I am using APIGatewayProxyFunction.

Below is the test function:

[Fact]
public async Task TestPost()
{
    var lambdaFunction = new LambdaEntryPoint();

    var requestStr = File.ReadAllText("./SampleRequests/Files-Post.json");
    var request = JsonSerializer.Deserialize<APIGatewayProxyRequest>(requestStr, new JsonSerializerOptions
    {
        PropertyNameCaseInsensitive = true
    });

    request.Body = @"{""time"":""2022-04-27T20:00:00.000Z"",""comment"":""dummy comment""}";

    // How to put file in request?
    
    var context = new TestLambdaContext();
    var response = await lambdaFunction.FunctionHandlerAsync(request, context);

    Assert.Equal(200, response.StatusCode);
    Assert.Equal(@"{""fileId"":""xxxxxxxxxx""}", response.Body);
}

My problem is that the APIGatewayProxyRequest accepts string as the request body, and I want to add a file to the request. How could I put a file alongside other inputs in the same request? Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source