Skip to content

Commit

Permalink
#2178 Added a new method CreateResponseWriter to UIResponseWriter cla…
Browse files Browse the repository at this point in the history
…ss. Cleanup some duplicated code.
  • Loading branch information
Terje Wiesener committed Oct 22, 2024
1 parent 116a4ea commit 37ddd44
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
32 changes: 13 additions & 19 deletions src/HealthChecks.UI.Client/UIResponseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,34 @@ public static class UIResponseWriter
private static readonly byte[] _emptyResponse = new byte[] { (byte)'{', (byte)'}' };
private static readonly Lazy<JsonSerializerOptions> _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<JsonSerializerOptions> jsonOptions, Func<Exception, string>? 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<HttpContext, HealthReport, Task> CreateResponseWriter(Lazy<JsonSerializerOptions> jsonOptions, Func<Exception, string>? exceptionFormatter = null)
{
return (HttpContext httpContext, HealthReport report) => WriteHealthCheckUIResponseAsync(httpContext, report, jsonOptions, exceptionFormatter);
}

private static JsonSerializerOptions CreateJsonOptions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace HealthChecks.UI.Client
namespace HealthChecks.UI.Client
{
public static class UIResponseWriter
{
public static System.Func<Microsoft.AspNetCore.Http.HttpContext, Microsoft.Extensions.Diagnostics.HealthChecks.HealthReport, System.Threading.Tasks.Task> CreateResponseWriter(System.Lazy<System.Text.Json.JsonSerializerOptions> jsonOptions, System.Func<System.Exception, string>? 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) { }
}
Expand Down
32 changes: 32 additions & 0 deletions test/HealthChecks.UI.Client.Tests/UIResponseWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, HealthReportEntry>
{
{ healthReportKey, new HealthReportEntry(HealthStatus.Healthy, null, TimeSpan.FromSeconds(1), null, null) }
};
var report = new HealthReport(entries, TimeSpan.FromSeconds(1));

var customJsonSettings = new Lazy<JsonSerializerOptions>(() => 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
Expand Down

0 comments on commit 37ddd44

Please sign in to comment.