public async Task WriteAsync_Works() { // Arrange var writer = GetWriter(); var stream = new MemoryStream(); var context = CreateContext(stream); var expectedProblem = new ProblemDetails() { Detail = "Custom Bad Request", Instance = "Custom Bad Request", Status = StatusCodes.Status400BadRequest, Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1-custom", Title = "Custom Bad Request", }; var problemDetailsContext = new ProblemDetailsContext() { HttpContext = context, ProblemDetails = expectedProblem }; //Act await writer.WriteAsync(problemDetailsContext); //Assert stream.Position = 0; var problemDetails = await JsonSerializer.DeserializeAsync <ProblemDetails>(stream); Assert.NotNull(problemDetails); Assert.Equal(expectedProblem.Status, problemDetails.Status); Assert.Equal(expectedProblem.Type, problemDetails.Type); Assert.Equal(expectedProblem.Title, problemDetails.Title); Assert.Equal(expectedProblem.Detail, problemDetails.Detail); Assert.Equal(expectedProblem.Instance, problemDetails.Instance); }
public async Task WriteAsync_AddExtensions() { // Arrange var writer = GetWriter(); var stream = new MemoryStream(); var context = CreateContext(stream); var expectedProblem = new ProblemDetails(); expectedProblem.Extensions["Extension1"] = "Extension1-Value"; expectedProblem.Extensions["Extension2"] = "Extension2-Value"; var problemDetailsContext = new ProblemDetailsContext() { HttpContext = context, ProblemDetails = expectedProblem }; //Act await writer.WriteAsync(problemDetailsContext); //Assert stream.Position = 0; var problemDetails = await JsonSerializer.DeserializeAsync <ProblemDetails>(stream); Assert.NotNull(problemDetails); Assert.Collection(problemDetails.Extensions, (extension) => { Assert.Equal("Extension1", extension.Key); Assert.Equal("Extension1-Value", extension.Value.ToString()); }, (extension) => { Assert.Equal("Extension2", extension.Key); Assert.Equal("Extension2-Value", extension.Value.ToString()); }); }
public ValueTask WriteAsync(ProblemDetailsContext context) => new(context.HttpContext.Response.WriteAsJsonAsync(_content));
public ProblemDetailsController(ProblemDetailsContext context) { _context = context; }
public static void Initialize(IServiceProvider serviceProvider) { var productContext = new ProductContext( serviceProvider.GetRequiredService < DbContextOptions <ProductContext> >()); var productTypeContext = new ProductTypeContext( serviceProvider.GetRequiredService < DbContextOptions <ProductTypeContext> >()); var problemDetailsContext = new ProblemDetailsContext( serviceProvider.GetRequiredService < DbContextOptions <ProblemDetailsContext> >()); if (!productContext.Products.Any()) { productContext.Products.AddRange( new Product { name = "OnePlus Nord", salesPrice = 350, productTypeId = 1 }, new Product { name = "Microsoft Surface Go 2", salesPrice = 600, productTypeId = 2 }, new Product { name = "BenQ Monitor", salesPrice = 20000, productTypeId = 3 }, new Product { name = "Alienware Area 51 M15X", salesPrice = 4000, productTypeId = 2 }, new Product { name = "Sony DSLR", salesPrice = 400, productTypeId = 4 } ); productContext.SaveChanges(); } if (!productTypeContext.ProductTypes.Any()) { productTypeContext.ProductTypes.AddRange( new ProductType { name = "smartphone", canBeInsured = true }, new ProductType { name = "laptop", canBeInsured = true }, new ProductType { name = "display", canBeInsured = true }, new ProductType { name = "digital camera", canBeInsured = true } ); productTypeContext.SaveChanges(); } if (!problemDetailsContext.ProblemDetails.Any()) { problemDetailsContext.ProblemDetails.AddRange( new ProblemDetails { type = "Not Found", title = "Info Not Found", status = 404, detail = "The requested info was not found", instance = $"Instance at {DateTime.Now}" } ); problemDetailsContext.SaveChanges(); } }