Skip to content

Commit 76bd3b4

Browse files
committed
text/plain; charset=utf-8 and pr feedback
1 parent 3a3f4cc commit 76bd3b4

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/Middleware/Diagnostics/src/DeveloperExceptionPage/DeveloperExceptionPageMiddleware.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private Task DisplayException(ErrorContext errorContext)
145145
// If the client does not ask for HTML just format the exception as plain text
146146
if (acceptHeader == null || !acceptHeader.Any(h => h.IsSubsetOf(_textHtmlMediaType)))
147147
{
148-
httpContext.Response.ContentType = "text/plain";
148+
httpContext.Response.ContentType = "text/plain; charset=utf-8";
149149

150150
var sb = new StringBuilder();
151151
sb.AppendLine(errorContext.Exception.ToString());

src/Middleware/Diagnostics/test/UnitTests/DeveloperExceptionPageMiddlewareTest.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,16 @@ public async Task ErrorPageWithoutAcceptHeaderForHtmlReturnsPlainText()
119119

120120
var responseText = await response.Content.ReadAsStringAsync();
121121
Assert.Equal("text/plain", response.Content.Headers.ContentType.MediaType);
122+
Assert.Equal("utf-8", response.Content.Headers.ContentType.CharSet);
122123
Assert.Contains("Test exception", responseText);
123124
Assert.DoesNotContain("<html", responseText);
124125
}
125126

126127
[Fact]
127128
public async Task StatusCodeFromBadHttpRequestExceptionIsPreserved()
128129
{
130+
const int statusCode = 418;
131+
129132
// Arrange
130133
using var host = new HostBuilder()
131134
.ConfigureWebHost(webHostBuilder =>
@@ -137,7 +140,7 @@ public async Task StatusCodeFromBadHttpRequestExceptionIsPreserved()
137140
app.UseDeveloperExceptionPage();
138141
app.Run(context =>
139142
{
140-
throw new BadHttpRequestException("Not found!", 404);
143+
throw new BadHttpRequestException("Not found!", statusCode);
141144
});
142145
});
143146
}).Build();
@@ -150,7 +153,7 @@ public async Task StatusCodeFromBadHttpRequestExceptionIsPreserved()
150153
var response = await server.CreateClient().GetAsync("/path");
151154

152155
// Assert
153-
Assert.Equal(StatusCodes.Status404NotFound, (int)response.StatusCode);
156+
Assert.Equal(statusCode, (int)response.StatusCode);
154157

155158
var responseText = await response.Content.ReadAsStringAsync();
156159
Assert.Contains("Not found!", responseText);

0 commit comments

Comments
 (0)