Skip to content
代码片段 群组 项目
未验证 提交 c1a234d7 编辑于 作者: John Luo's avatar John Luo 提交者: GitHub
浏览文件

Check for HasStarted after running ExceptionHandler (#32198)

上级 e92aeff4
No related branches found
No related tags found
无相关合并请求
...@@ -132,7 +132,8 @@ namespace Microsoft.AspNetCore.Diagnostics ...@@ -132,7 +132,8 @@ namespace Microsoft.AspNetCore.Diagnostics
await _options.ExceptionHandler!(context); await _options.ExceptionHandler!(context);
if (context.Response.StatusCode != StatusCodes.Status404NotFound || _options.AllowStatusCode404Response) // If the response has already started, assume exception handler was successful.
if (context.Response.HasStarted || context.Response.StatusCode != StatusCodes.Status404NotFound || _options.AllowStatusCode404Response)
{ {
if (_diagnosticListener.IsEnabled() && _diagnosticListener.IsEnabled("Microsoft.AspNetCore.Diagnostics.HandledException")) if (_diagnosticListener.IsEnabled() && _diagnosticListener.IsEnabled("Microsoft.AspNetCore.Diagnostics.HandledException"))
{ {
......
...@@ -312,6 +312,73 @@ namespace Microsoft.AspNetCore.Diagnostics ...@@ -312,6 +312,73 @@ namespace Microsoft.AspNetCore.Diagnostics
} }
} }
[Fact]
public async Task ExceptionHandlerSucceeded_IfExceptionHandlerResponseHasStarted()
{
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseTestServer()
.Configure(app =>
{
app.Use(async (httpContext, next) =>
{
Exception exception = null;
try
{
await next(httpContext);
}
catch (InvalidOperationException ex)
{
exception = ex;
}
Assert.Null(exception);
});
app.UseExceptionHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
{
httpContext.Response.StatusCode = StatusCodes.Status404NotFound;
await httpContext.Response.WriteAsync("Custom 404");
});
});
app.Run(httpContext =>
{
httpContext.Response.Headers.Add("Cache-Control", new[] { "max-age=3600" });
httpContext.Response.Headers.Add("Pragma", new[] { "max-age=3600" });
httpContext.Response.Headers.Add("Expires", new[] { DateTime.UtcNow.AddDays(10).ToString("R") });
httpContext.Response.Headers.Add("ETag", new[] { "abcdef" });
throw new InvalidOperationException("Something bad happened");
});
});
}).Build();
await host.StartAsync();
using (var server = host.GetTestServer())
{
var client = server.CreateClient();
var response = await client.GetAsync(string.Empty);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.Equal("Custom 404", await response.Content.ReadAsStringAsync());
IEnumerable<string> values;
Assert.True(response.Headers.CacheControl.NoCache);
Assert.True(response.Headers.CacheControl.NoStore);
Assert.True(response.Headers.TryGetValues("Pragma", out values));
Assert.Single(values);
Assert.Equal("no-cache", values.First());
Assert.False(response.Headers.TryGetValues("Expires", out _));
Assert.False(response.Headers.TryGetValues("ETag", out _));
}
}
[Fact] [Fact]
public async Task DoesNotClearCacheHeaders_WhenResponseHasAlreadyStarted() public async Task DoesNotClearCacheHeaders_WhenResponseHasAlreadyStarted()
{ {
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册