We have a .NET Core 3.1 API that provides a DELETE method.
[HttpDelete("email")]
[Authorize]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IResponse> DeleteEmail([FromBody] EmailApiModel emailApiModel)
{
var loginEmail = this.User.FindFirst(ClaimTypes.Email).Value;
await this.userService.DeleteEmailAsync(loginEmail, emailApiModel.Email);
return await Task.FromResult(new Response((int)HttpStatusCode.NoContent));
}
The EmailApiModel looks like this:
public class EmailApiModel
{
[Email]
[Required]
public string Email { get; set; } = string.Empty;
}
When trying to write an integration test for this method, I got stuck.
The integration test method is the following:
[Fact]
public async Task DeleteEmail_WhenCalled_Then402IsReturnedAndEmailIsDeleted()
{
const string emailToDelete = "delete@email.com";
this.GivenUserWithLoginEmailAndAdditionalEmails(ValidUserMail, new [] { emailToDelete });
this.TheApplicationIsUpAndRunningWithFakedAuthentication();
var emailApiModel = new EmailApiModel { Email = emailToDelete };
var httpResponseMessage = await this.DeleteEmail(emailApiModel);
this.ThenTheResponseMessageShouldBe(httpResponseMessage, HttpStatusCode.OK);
this.ThenTheResponseShouldBe(httpResponseMessage, HttpStatusCode.NoContent);
}
protected async Task<HttpResponseMessage> DeleteEmail(EmailApiModel emailApiModel)
{
var jsonData = JsonSerializer.Serialize(emailApiModel);
var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
return await this.HttpClient.DeleteAsync(Api.User.Email, content);
}
The HttpClient
is set up via a CustomWebApplicationFactory
//https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.0
this.httpClient = this.WebApplicationFactory.CreateClient(new WebApplicationFactoryClientOptions {AllowAutoRedirect = false});
The problem I have is, that the DeleteAsync
-Method on the HttpClient doesn't allow me to provide a content (so this call is not possible: await this.HttpClient.DeleteAsync(Api.User.Email, content);
)
The only overload of the DeleteAsync-Method takes an additional CancellationToken.
How can I provide the content in the integration test?
Thanks in advance