Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2178 Added a new method CreateResponseWriter to UIResponseWriter #2307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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