diff --git a/src/HealthChecks.UI.Client/UIResponseWriter.cs b/src/HealthChecks.UI.Client/UIResponseWriter.cs index 803163bc64..5735b93e3f 100644 --- a/src/HealthChecks.UI.Client/UIResponseWriter.cs +++ b/src/HealthChecks.UI.Client/UIResponseWriter.cs @@ -15,40 +15,34 @@ public static class UIResponseWriter private static readonly byte[] _emptyResponse = new byte[] { (byte)'{', (byte)'}' }; private static readonly Lazy _options = new(CreateJsonOptions); -#pragma warning disable IDE1006 // Naming Styles - public static async Task WriteHealthCheckUIResponse(HttpContext httpContext, HealthReport report) // TODO: rename public API -#pragma warning restore IDE1006 // Naming Styles + private static async Task WriteHealthCheckUIResponseAsync(HttpContext httpContext, HealthReport report, Lazy jsonOptions, Func? exceptionFormatter = null) { if (report != null) { httpContext.Response.ContentType = DEFAULT_CONTENT_TYPE; - var uiReport = UIHealthReport.CreateFrom(report); + var uiReport = UIHealthReport.CreateFrom(report, exceptionFormatter); - await JsonSerializer.SerializeAsync(httpContext.Response.Body, uiReport, _options.Value).ConfigureAwait(false); + await JsonSerializer.SerializeAsync(httpContext.Response.Body, uiReport, jsonOptions.Value).ConfigureAwait(false); } else { await httpContext.Response.BodyWriter.WriteAsync(_emptyResponse).ConfigureAwait(false); } } - -#pragma warning disable IDE1006 // Naming Styles - public static async Task WriteHealthCheckUIResponseNoExceptionDetails(HttpContext httpContext, HealthReport report) -#pragma warning restore IDE1006 // Naming Styles + public static Task WriteHealthCheckUIResponse(HttpContext httpContext, HealthReport report) // TODO: rename public API { - if (report != null) - { - httpContext.Response.ContentType = DEFAULT_CONTENT_TYPE; + return WriteHealthCheckUIResponseAsync(httpContext, report, _options); + } - var uiReport = UIHealthReport.CreateFrom(report, _ => "Exception Occurred."); + public static Task WriteHealthCheckUIResponseNoExceptionDetails(HttpContext httpContext, HealthReport report) + { + return WriteHealthCheckUIResponseAsync(httpContext, report, _options, _ => "Exception Occurred."); + } - await JsonSerializer.SerializeAsync(httpContext.Response.Body, uiReport, _options.Value).ConfigureAwait(false); - } - else - { - await httpContext.Response.BodyWriter.WriteAsync(_emptyResponse).ConfigureAwait(false); - } + public static Func CreateResponseWriter(Lazy jsonOptions, Func? exceptionFormatter = null) + { + return (HttpContext httpContext, HealthReport report) => WriteHealthCheckUIResponseAsync(httpContext, report, jsonOptions, exceptionFormatter); } private static JsonSerializerOptions CreateJsonOptions() diff --git a/test/HealthChecks.UI.Client.Tests/HealthChecks.UI.Client.approved.txt b/test/HealthChecks.UI.Client.Tests/HealthChecks.UI.Client.approved.txt index 8f2134ada4..b83d5e6439 100644 --- a/test/HealthChecks.UI.Client.Tests/HealthChecks.UI.Client.approved.txt +++ b/test/HealthChecks.UI.Client.Tests/HealthChecks.UI.Client.approved.txt @@ -1,7 +1,8 @@ -namespace HealthChecks.UI.Client +namespace HealthChecks.UI.Client { public static class UIResponseWriter { + public static System.Func CreateResponseWriter(System.Lazy jsonOptions, System.Func? exceptionFormatter = null) { } public static System.Threading.Tasks.Task WriteHealthCheckUIResponse(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.Extensions.Diagnostics.HealthChecks.HealthReport report) { } public static System.Threading.Tasks.Task WriteHealthCheckUIResponseNoExceptionDetails(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.Extensions.Diagnostics.HealthChecks.HealthReport report) { } } diff --git a/test/HealthChecks.UI.Client.Tests/UIResponseWriterTests.cs b/test/HealthChecks.UI.Client.Tests/UIResponseWriterTests.cs index b3b5f7e42d..617c05980e 100644 --- a/test/HealthChecks.UI.Client.Tests/UIResponseWriterTests.cs +++ b/test/HealthChecks.UI.Client.Tests/UIResponseWriterTests.cs @@ -56,6 +56,38 @@ public async Task should_not_encode_unicode_characters(string healthReportKey) responseAsText.ShouldContain(healthReportKey); } + [Fact] + public async Task should_use_custom_jsonserializersettings_when_provided() + { + var healthReportKey = "Some key"; + var httpContext = new DefaultHttpContext(); + httpContext.Response.Body = new MemoryStream(); + var entries = new Dictionary + { + { healthReportKey, new HealthReportEntry(HealthStatus.Healthy, null, TimeSpan.FromSeconds(1), null, null) } + }; + var report = new HealthReport(entries, TimeSpan.FromSeconds(1)); + + var customJsonSettings = new Lazy(() => new JsonSerializerOptions + { + WriteIndented = true, + }); + + var customWriter = UIResponseWriter.CreateResponseWriter(customJsonSettings); + await customWriter(httpContext, report); + + httpContext.Response.ContentType.ShouldBe("application/json"); + + // reset pointer to the beginning + httpContext.Response.Body.Seek(0, SeekOrigin.Begin); + + var responseStreamReader = new StreamReader(httpContext.Response.Body); + var responseAsText = await responseStreamReader.ReadToEndAsync(); + + responseAsText.ShouldContain(healthReportKey); + responseAsText.Split('\n').Length.ShouldBeGreaterThan(1); + } + private static JsonSerializerOptions CreateJsonOptions() { var options = new JsonSerializerOptions