1

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

xeraphim
  • 4,375
  • 9
  • 54
  • 102
  • 1
    Have you tried using `SendAsync` and then set the content of the request yourself? I can't remember if it works or if there is internal logic to ignore the body when sending.. but worth a try? – A Friend Jan 30 '20 at 08:49
  • good idea! It worked :-) I've changed the DeleteEmail to this: `var httpRequestMessage = new HttpRequestMessage(HttpMethod.Delete, Api.User.Email) { Content = content }; return await this.HttpClient.SendAsync(httpRequestMessage);` If you add this as an answer, I'll gladly accept it :-) – xeraphim Jan 30 '20 at 08:56
  • 1
    Does this answer your question? [Passing body content when calling a Delete Web API method using System.Net.Http](https://stackoverflow.com/questions/38976260/passing-body-content-when-calling-a-delete-web-api-method-using-system-net-http) – A Friend Jan 30 '20 at 08:58
  • Found a duplicate for it instead :) – A Friend Jan 30 '20 at 08:58
  • perfect, I've voted to close the question because of the duplicate, thanks again :) – xeraphim Jan 30 '20 at 09:00

0 Answers0