2025-03-12 18:30:40 +01:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using PlanTempus.Core.CommandQueries;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace PlanTempus.X.TDD.CommandQueryHandlerTests;
|
|
|
|
|
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class ProblemDetailsTests
|
|
|
|
|
{
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void TestFormatOfProblemDetails()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var problemDetails = new ProblemDetails
|
|
|
|
|
{
|
|
|
|
|
Type = "https://example.com/errors/invalid-input",
|
|
|
|
|
Title = "Invalid Input",
|
|
|
|
|
Status = 400,
|
|
|
|
|
Detail = "The request body is invalid.",
|
|
|
|
|
Instance = "/api/users"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
problemDetails.AddExtension("invalidFields", new[]
|
|
|
|
|
{
|
|
|
|
|
new { Field = "name", Message = "The 'name' field is required." },
|
|
|
|
|
new { Field = "email", Message = "The 'email' field must be a valid email address." }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var json = JsonConvert.SerializeObject(problemDetails, Formatting.Indented);
|
|
|
|
|
|
|
|
|
|
var expectedJson = """
|
|
|
|
|
{
|
|
|
|
|
"Type": "https://example.com/errors/invalid-input",
|
|
|
|
|
"Title": "Invalid Input",
|
|
|
|
|
"Status": 400,
|
|
|
|
|
"Detail": "The request body is invalid.",
|
|
|
|
|
"Instance": "/api/users",
|
|
|
|
|
"invalidFields": [
|
|
|
|
|
{
|
|
|
|
|
"Field": "name",
|
|
|
|
|
"Message": "The 'name' field is required."
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"Field": "email",
|
|
|
|
|
"Message": "The 'email' field must be a valid email address."
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
""";
|
|
|
|
|
|
2026-01-08 21:51:43 +01:00
|
|
|
JsonConvert.DeserializeObject(json).ShouldBeSameAs(JsonConvert.DeserializeObject(expectedJson));
|
2025-03-12 18:30:40 +01:00
|
|
|
}
|
|
|
|
|
}
|