-1

I'm upgrading a project to .NET 5.0 and one of our exception extension classes fails in tests where others pass. This is only a problem in .NET 5.0 where it works fine in .NET Core 3.1.

Examples of the two exceptions being thrown in code that the tests check:

if (request.GroupId <= 0)
{
    throw new BadRequestException($"A valid {nameof(Data.Models.Group)} Id must be provided.");
}

var group = await Database.Groups.FindAsync(request.GroupId);
if (group == null)
{
    throw new EntityNotFoundException($"{nameof(Data.Models.Group)} not found.");
}

With the above code, when running the tests, any BadRequestException passes but any test that expects a NotFound response from the EntityNotFoundException class produces the following error:

Exception Message: 
        Test method MyProject.Test.Tests.Integration.Group.DeleteGroupTests.DeleteGroup_IdDoesNotExist_NotFound threw exception: 
        System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException:  ---> MyProject.Data.Exceptions.EntityNotFoundException: Group not found.
      Stack Trace: 
        DeleteGroupCommandHandler.Handle(DeleteGroupCommand request, CancellationToken cancellationToken) line 40
        RequestExceptionProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
...

EntityNotFoundException - Fails

public class EntityNotFoundException : ExceptionBase
{
    private static string DefaultMessageHeader => "Not found";

    public override HttpStatusCode StatusCode => HttpStatusCode.NotFound;

    public EntityNotFoundException(string message, string messageHeader = null)
            : base(message, messageHeader ?? DefaultMessageHeader) { }
}

BadRequestException - Passes

public class BadRequestException : ExceptionBase
{
    private static string DefaultMessageHeader => "Bad Request";

    public override HttpStatusCode StatusCode => HttpStatusCode.BadRequest;

    public BadRequestException(string message, string messageHeader = null)
            : base(message, messageHeader ?? DefaultMessageHeader) { }
}

What is the problem with the EntityNotFoundException class? Why do only those fail?

Update 1

Tests:

// FAILS
[DataTestMethod]
[DataRow(int.MaxValue)]
public async Task DeleteGroup_IdDoesNotExist_NotFound(int groupId)
{
    var response = await Client.DeleteAsync($"/api/groups/{groupId}");
    response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

// PASSES
[DataTestMethod]
[DataRow(0)]
[DataRow(-1)]
public async Task DeleteGroup_InvalidId_BadRequest(int groupId)
{
    var response = await Client.DeleteAsync($"/api/groups/{groupId}");
    response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}

1 Answers1

0

Found a solution to fix the tests but still unsure why the error happens only for the EntityNotFoundException exception classes and no others.

If anyone knows why the below code fixes the failing tests, I'd love to hear it.

The solution is to specify the HttpCompletionOption.ResponseHeadersRead flag which is not available in DeleteAsync, so switch to SendAsync instead.

From:

using var response = await Client.DeleteAsync($"/api/groups/{groupId}");

To:

using var response = await Client.SendAsync(new HttpRequestMessage(HttpMethod.Delete, $"/api/groups/{groupId}"), HttpCompletionOption.ResponseHeadersRead);