diff options
-rw-r--r-- | src/Http/Http.Results/src/ProblemHttpResult.cs | 2 | ||||
-rw-r--r-- | src/Http/Http.Results/test/ProblemResultTests.cs | 32 |
2 files changed, 33 insertions, 1 deletions
diff --git a/src/Http/Http.Results/src/ProblemHttpResult.cs b/src/Http/Http.Results/src/ProblemHttpResult.cs index bd7e72ff87..9fdf1f2c1d 100644 --- a/src/Http/Http.Results/src/ProblemHttpResult.cs +++ b/src/Http/Http.Results/src/ProblemHttpResult.cs @@ -53,7 +53,7 @@ public sealed class ProblemHttpResult : IResult httpContext.Response.StatusCode = code; } - return HttpResultsHelper.WriteResultAsJsonAsync( + return HttpResultsHelper.WriteResultAsJsonAsync<object?>( httpContext, logger, value: ProblemDetails, diff --git a/src/Http/Http.Results/test/ProblemResultTests.cs b/src/Http/Http.Results/test/ProblemResultTests.cs index 4e6c38a567..688bd1ecf7 100644 --- a/src/Http/Http.Results/test/ProblemResultTests.cs +++ b/src/Http/Http.Results/test/ProblemResultTests.cs @@ -70,6 +70,38 @@ public class ProblemResultTests } [Fact] + public async Task ExecuteAsync_IncludeErrors_ForValidationProblemDetails() + { + // Arrange + var details = new HttpValidationProblemDetails(new Dictionary<string, string[]> + { + { "testError", new string[] { "message" } } + }); + + var result = new ProblemHttpResult(details); + var stream = new MemoryStream(); + var httpContext = new DefaultHttpContext() + { + RequestServices = CreateServices(), + Response = + { + Body = stream, + }, + }; + + // Act + await result.ExecuteAsync(httpContext); + + // Assert + Assert.Equal(StatusCodes.Status400BadRequest, httpContext.Response.StatusCode); + stream.Position = 0; + var responseDetails = JsonSerializer.Deserialize<HttpValidationProblemDetails>(stream); + Assert.Equal(StatusCodes.Status400BadRequest, responseDetails.Status); + var error = Assert.Single(responseDetails.Errors); + Assert.Equal("testError", error.Key); + } + + [Fact] public async Task ExecuteAsync_GetsStatusCodeFromProblemDetails() { // Arrange |