Я пишу интеграционный тест, который проверяет конечную точку для загрузки файла и проверяет правильность результата запроса!
Когда я запускаю тест, я получаю код состояния 415.
Моя конечная точка:
[HttpPost]
[Produces("application/json")]
[ProducesResponseType(200, Type = typeof(FileModel))]
public async Task<IActionResult> UploadNewFileAsync(UploadNewFileRequestModel fileModel)
{
IActionResult result = new StatusCodeResult(StatusCodes.Status500InternalServerError);
var dbResponse = await _fileManager.CreateFileAsync(fileModel, async (dbFileModel) =>
{
var uploadSuccess = await _fileService.PutFile(fileModel.Location, fileModel.FileName, fileModel.Buffer, true);
var fileData = await _fileService.GetFileMetadata(fileModel.Location, fileModel.FileName);
// update of S3 data into FileModel, but only valid fields which really comes from S3!!
dbFileModel.Key = fileData.Key;
dbFileModel.File_etag = fileData.File_etag;
dbFileModel.AwsOwner = fileData.AwsOwner;
dbFileModel.AwsToken = fileData.AwsToken;
dbFileModel.Bucket = fileData.Bucket;
return uploadSuccess;
});
if (dbResponse.Valid)
{
result = Ok(dbResponse.FileModel);
}
return result;
}
Мой интеграционный тест:
public FilesControllerIntegrationTest(CustomWebApplicationFactory<Startup> factory)
{
_factory = factory;
_client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
}
[Fact]
public async Task UploadFileAsync()
{
var expectedContentType = "application/json; charset=utf-8";
var file1 = File.OpenRead(@"C:\TestPhotos\img1.jpg");
HttpContent content1 = new StreamContent(file1);
var file2 = File.OpenRead(@"C:\TestPhotos\img2.jpg");
HttpContent content2 = new StreamContent(file2);
var formData = new MultipartFormDataContent();
// Add file (file, field name, file name)
formData.Add(content1, "fileModel", "img1.jpg");
formData.Add(content2, "fileModel", "img2.jpg");
var response = await _client.PostAsync("/api/files", formData);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(responseString);
Assert.Equal(expectedContentType, response.Content.Headers.ContentType.ToString());
response.Dispose();
_client.Dispose();
}
В PostAsyn c () я получаю ошибку 415, в formData я отправляю multipart / form-data, но на самом деле это приложение / json.
Надеюсь, вы мне поможете!