@@ -312,6 +312,73 @@ public async Task DoesNotModifyCacheHeaders_WhenNoExceptionIsThrown()
312
312
}
313
313
}
314
314
315
+ [ Fact ]
316
+ public async Task ExceptionHandlerSucceeded_IfExceptionHandlerResponseHasStarted ( )
317
+ {
318
+ using var host = new HostBuilder ( )
319
+ . ConfigureWebHost ( webHostBuilder =>
320
+ {
321
+ webHostBuilder
322
+ . UseTestServer ( )
323
+ . Configure ( app =>
324
+ {
325
+ app . Use ( async ( httpContext , next ) =>
326
+ {
327
+ Exception exception = null ;
328
+ try
329
+ {
330
+ await next ( httpContext ) ;
331
+ }
332
+ catch ( InvalidOperationException ex )
333
+ {
334
+ exception = ex ;
335
+ }
336
+
337
+ Assert . Null ( exception ) ;
338
+ } ) ;
339
+
340
+ app . UseExceptionHandler ( "/handle-errors" ) ;
341
+
342
+ app . Map ( "/handle-errors" , ( innerAppBuilder ) =>
343
+ {
344
+ innerAppBuilder . Run ( async ( httpContext ) =>
345
+ {
346
+ httpContext . Response . StatusCode = StatusCodes . Status404NotFound ;
347
+ await httpContext . Response . WriteAsync ( "Custom 404" ) ;
348
+ } ) ;
349
+ } ) ;
350
+
351
+ app . Run ( httpContext =>
352
+ {
353
+ httpContext . Response . Headers . Add ( "Cache-Control" , new [ ] { "max-age=3600" } ) ;
354
+ httpContext . Response . Headers . Add ( "Pragma" , new [ ] { "max-age=3600" } ) ;
355
+ httpContext . Response . Headers . Add ( "Expires" , new [ ] { DateTime . UtcNow . AddDays ( 10 ) . ToString ( "R" ) } ) ;
356
+ httpContext . Response . Headers . Add ( "ETag" , new [ ] { "abcdef" } ) ;
357
+
358
+ throw new InvalidOperationException ( "Something bad happened" ) ;
359
+ } ) ;
360
+ } ) ;
361
+ } ) . Build ( ) ;
362
+
363
+ await host . StartAsync ( ) ;
364
+
365
+ using ( var server = host . GetTestServer ( ) )
366
+ {
367
+ var client = server . CreateClient ( ) ;
368
+ var response = await client . GetAsync ( string . Empty ) ;
369
+ Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
370
+ Assert . Equal ( "Custom 404" , await response . Content . ReadAsStringAsync ( ) ) ;
371
+ IEnumerable < string > values ;
372
+ Assert . True ( response . Headers . CacheControl . NoCache ) ;
373
+ Assert . True ( response . Headers . CacheControl . NoStore ) ;
374
+ Assert . True ( response . Headers . TryGetValues ( "Pragma" , out values ) ) ;
375
+ Assert . Single ( values ) ;
376
+ Assert . Equal ( "no-cache" , values . First ( ) ) ;
377
+ Assert . False ( response . Headers . TryGetValues ( "Expires" , out _ ) ) ;
378
+ Assert . False ( response . Headers . TryGetValues ( "ETag" , out _ ) ) ;
379
+ }
380
+ }
381
+
315
382
[ Fact ]
316
383
public async Task DoesNotClearCacheHeaders_WhenResponseHasAlreadyStarted ( )
317
384
{
0 commit comments