Skip to content

Commit

Permalink
UIResponseWriter - Do not encode unicode characters
Browse files Browse the repository at this point in the history
  • Loading branch information
cieciurm committed Aug 5, 2023
1 parent 1a9df92 commit 2449b12
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build/versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<HealthChecksUIPostgreSQLStorage>7.0.0</HealthChecksUIPostgreSQLStorage>
<HealthCheckSystem>7.0.0</HealthCheckSystem>
<HealthCheckUI>7.0.1</HealthCheckUI>
<HealthCheckUIClient>7.0.0</HealthCheckUIClient>
<HealthCheckUIClient>7.1.0</HealthCheckUIClient>
<HealthCheckUICore>7.0.0</HealthCheckUICore>
<HealthCheckUIData>7.0.0</HealthCheckUIData>
<HealthCheckUIInMemoryStorage>7.0.0</HealthCheckUIInMemoryStorage>
Expand Down
3 changes: 3 additions & 0 deletions src/HealthChecks.UI.Client/UIResponseWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using HealthChecks.UI.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Diagnostics.HealthChecks;
Expand Down Expand Up @@ -55,6 +57,7 @@ private static JsonSerializerOptions CreateJsonOptions()
{
AllowTrailingCommas = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};

Expand Down
24 changes: 24 additions & 0 deletions test/HealthChecks.UI.Client.Tests/UIResponseWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ public async Task should_response_with_obfuscated_exception_when_exception_messa
healthReport.ShouldNotBeNull().Entries[healthReportKey].Exception.ShouldBe(exceptionMessage);
}

[Theory]
[InlineData("你好")]
[InlineData("жарко")]
[InlineData("ąężćś")]
public async Task should_not_encode_unicode_characters(string healthReportKey)
{
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));

await UIResponseWriter.WriteHealthCheckUIResponse(httpContext, report).ConfigureAwait(false);

httpContext.Response.Body.Seek(0, SeekOrigin.Begin);

var responseStreamReader = new StreamReader(httpContext.Response.Body);
var responseAsText = await responseStreamReader.ReadToEndAsync().ConfigureAwait(false);

responseAsText.ShouldContain(healthReportKey);
}

private static JsonSerializerOptions CreateJsonOptions()
{
var options = new JsonSerializerOptions
Expand Down

0 comments on commit 2449b12

Please sign in to comment.