@@ -1729,6 +1729,7 @@ public void WebApplicationBuilder_ThrowsFromExtensionMethodsNotSupportedByHostAn
1729
1729
public async Task EndpointDataSourceOnlyAddsOnce ( CreateBuilderFunc createBuilder )
1730
1730
{
1731
1731
var builder = createBuilder ( ) ;
1732
+ builder . WebHost . UseTestServer ( ) ;
1732
1733
await using var app = builder . Build ( ) ;
1733
1734
1734
1735
app . UseRouting ( ) ;
@@ -1883,6 +1884,7 @@ public async Task BranchingPipelineHasOwnRoutes(CreateBuilderFunc createBuilder)
1883
1884
public async Task PropertiesPreservedFromInnerApplication ( CreateBuilderFunc createBuilder )
1884
1885
{
1885
1886
var builder = createBuilder ( ) ;
1887
+ builder . WebHost . UseTestServer ( ) ;
1886
1888
builder . Services . AddSingleton < IStartupFilter , PropertyFilter > ( ) ;
1887
1889
await using var app = builder . Build ( ) ;
1888
1890
@@ -2526,6 +2528,141 @@ public async Task UsingCreateSlimBuilderWorksIfRegexConstraintAddedViaAddRouting
2526
2528
Assert . Equal ( "RegexRoute" , chosenRoute ) ;
2527
2529
}
2528
2530
2531
+ private sealed class TestDebugger : IDebugger
2532
+ {
2533
+ private bool _isAttached ;
2534
+ public TestDebugger ( bool isAttached ) => _isAttached = isAttached ;
2535
+ public bool IsAttached => _isAttached ;
2536
+ }
2537
+
2538
+ [ Fact ]
2539
+ public void UseMiddleware_DebugView_HasMiddleware ( )
2540
+ {
2541
+ var builder = WebApplication . CreateBuilder ( ) ;
2542
+ builder . Services . AddSingleton < IDebugger > ( new TestDebugger ( true ) ) ;
2543
+
2544
+ var app = builder . Build ( ) ;
2545
+
2546
+ app . UseMiddleware < MiddlewareWithInterface > ( ) ;
2547
+ app . UseAuthentication ( ) ;
2548
+ app . Use ( next =>
2549
+ {
2550
+ return next ;
2551
+ } ) ;
2552
+
2553
+ var debugView = new WebApplication . WebApplicationDebugView ( app ) ;
2554
+
2555
+ // Contains three strings:
2556
+ // 1. Middleware that implements IMiddleware from app.UseMiddleware<T>()
2557
+ // 2. AuthenticationMiddleware type from app.UseAuthentication()
2558
+ // 3. Generated delegate name from app.Use(...)
2559
+ Assert . Collection ( debugView . Middleware ,
2560
+ m => Assert . Equal ( typeof ( MiddlewareWithInterface ) . FullName , m ) ,
2561
+ m => Assert . Equal ( "Microsoft.AspNetCore.Authentication.AuthenticationMiddleware" , m ) ,
2562
+ m =>
2563
+ {
2564
+ Assert . Contains ( nameof ( UseMiddleware_DebugView_HasMiddleware ) , m ) ;
2565
+ Assert . DoesNotContain ( nameof ( RequestDelegate ) , m ) ;
2566
+ } ) ;
2567
+ }
2568
+
2569
+ [ Fact ]
2570
+ public void NoDebugger_DebugView_NoMiddleware ( )
2571
+ {
2572
+ var builder = WebApplication . CreateBuilder ( ) ;
2573
+ builder . Services . AddSingleton < IDebugger > ( new TestDebugger ( false ) ) ;
2574
+
2575
+ var app = builder . Build ( ) ;
2576
+
2577
+ app . UseMiddleware < MiddlewareWithInterface > ( ) ;
2578
+ app . UseAuthentication ( ) ;
2579
+ app . Use ( next =>
2580
+ {
2581
+ return next ;
2582
+ } ) ;
2583
+
2584
+ var debugView = new WebApplication . WebApplicationDebugView ( app ) ;
2585
+
2586
+ Assert . Throws < NotSupportedException > ( ( ) => debugView . Middleware ) ;
2587
+ }
2588
+
2589
+ [ Fact ]
2590
+ public async Task UseMiddleware_HasEndpointsAndAuth_Run_DebugView_HasAutomaticMiddleware ( )
2591
+ {
2592
+ var builder = WebApplication . CreateBuilder ( ) ;
2593
+ builder . WebHost . UseTestServer ( ) ;
2594
+ builder . Services . AddAuthenticationCore ( ) ;
2595
+ builder . Services . AddAuthorization ( ) ;
2596
+ builder . Services . AddSingleton < IDebugger > ( new TestDebugger ( true ) ) ;
2597
+
2598
+ await using var app = builder . Build ( ) ;
2599
+
2600
+ app . UseMiddleware < MiddlewareWithInterface > ( ) ;
2601
+ app . MapGet ( "/hello" , ( ) => "hello world" ) ;
2602
+
2603
+ // Starting the app automatically adds middleware as needed.
2604
+ _ = app . RunAsync ( ) ;
2605
+
2606
+ var debugView = new WebApplication . WebApplicationDebugView ( app ) ;
2607
+
2608
+ Assert . Collection ( debugView . Middleware ,
2609
+ m => Assert . Equal ( "Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware" , m ) ,
2610
+ m => Assert . Equal ( "Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware" , m ) ,
2611
+ m => Assert . Equal ( "Microsoft.AspNetCore.Authentication.AuthenticationMiddleware" , m ) ,
2612
+ m => Assert . Equal ( "Microsoft.AspNetCore.Authorization.AuthorizationMiddleware" , m ) ,
2613
+ m => Assert . Equal ( typeof ( MiddlewareWithInterface ) . FullName , m ) ,
2614
+ m => Assert . Equal ( "Microsoft.AspNetCore.Routing.EndpointMiddleware" , m ) ) ;
2615
+ }
2616
+
2617
+ [ Fact ]
2618
+ public async Task NoMiddleware_Run_DebugView_HasAutomaticMiddleware ( )
2619
+ {
2620
+ var builder = WebApplication . CreateBuilder ( ) ;
2621
+ builder . WebHost . UseTestServer ( ) ;
2622
+ builder . Services . AddSingleton < IDebugger > ( new TestDebugger ( true ) ) ;
2623
+
2624
+ await using var app = builder . Build ( ) ;
2625
+
2626
+ // Starting the app automatically adds middleware as needed.
2627
+ _ = app . RunAsync ( ) ;
2628
+
2629
+ var debugView = new WebApplication . WebApplicationDebugView ( app ) ;
2630
+
2631
+ Assert . Collection ( debugView . Middleware ,
2632
+ m => Assert . Equal ( "Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware" , m ) ) ;
2633
+ }
2634
+
2635
+ [ Fact ]
2636
+ public void NestedMiddleware_DebugView_OnlyContainsTopLevelMiddleware ( )
2637
+ {
2638
+ var builder = WebApplication . CreateBuilder ( ) ;
2639
+ builder . Services . AddSingleton < IDebugger > ( new TestDebugger ( true ) ) ;
2640
+
2641
+ var app = builder . Build ( ) ;
2642
+
2643
+ app . MapWhen ( c => true , nested =>
2644
+ {
2645
+ nested . UseStatusCodePages ( ) ;
2646
+ } ) ;
2647
+ app . UseWhen ( c => false , nested =>
2648
+ {
2649
+ nested . UseDeveloperExceptionPage ( ) ;
2650
+ } ) ;
2651
+ app . UseExceptionHandler ( ) ;
2652
+
2653
+ var debugView = new WebApplication . WebApplicationDebugView ( app ) ;
2654
+
2655
+ Assert . Equal ( 3 , debugView . Middleware . Count ) ;
2656
+ }
2657
+
2658
+ private class MiddlewareWithInterface : IMiddleware
2659
+ {
2660
+ public Task InvokeAsync ( HttpContext context , RequestDelegate next )
2661
+ {
2662
+ throw new NotImplementedException ( ) ;
2663
+ }
2664
+ }
2665
+
2529
2666
private class UberHandler : AuthenticationHandler < AuthenticationSchemeOptions >
2530
2667
{
2531
2668
public UberHandler ( IOptionsMonitor < AuthenticationSchemeOptions > options , ILoggerFactory logger , UrlEncoder encoder ) : base ( options , logger , encoder ) { }
0 commit comments