66using System . Diagnostics . Tracing ;
77using System . Net ;
88using System . Reflection ;
9+ using System . Text ;
910using Microsoft . AspNetCore . Builder ;
1011using Microsoft . AspNetCore . HostFiltering ;
1112using Microsoft . AspNetCore . Hosting ;
@@ -651,6 +652,75 @@ public async Task WebApplicationCanObserveConfigurationChangesMadeInBuild()
651652 Assert . Equal ( "F" , builder . Configuration [ "F" ] ) ;
652653 }
653654
655+ [ Fact ]
656+ public async Task WebApplicationCanObserveSourcesClearedInBuild ( )
657+ {
658+ // This mimics what WebApplicationFactory<T> does and runs configure
659+ // services callbacks
660+ using var listener = new HostingListener ( hostBuilder =>
661+ {
662+ hostBuilder . ConfigureHostConfiguration ( config =>
663+ {
664+ // Clearing here would not remove the app config added via builder.Configuration.
665+ config . AddInMemoryCollection ( new Dictionary < string , string > ( )
666+ {
667+ { "A" , "A" } ,
668+ } ) ;
669+ } ) ;
670+
671+ hostBuilder . ConfigureAppConfiguration ( config =>
672+ {
673+ // This clears both the chained host configuration and chained builder.Configuration.
674+ config . Sources . Clear ( ) ;
675+ config . AddInMemoryCollection ( new Dictionary < string , string > ( )
676+ {
677+ { "B" , "B" } ,
678+ } ) ;
679+ } ) ;
680+ } ) ;
681+
682+ var builder = WebApplication . CreateBuilder ( ) ;
683+
684+ builder . Configuration . AddInMemoryCollection ( new Dictionary < string , string > ( )
685+ {
686+ { "C" , "C" } ,
687+ } ) ;
688+
689+ await using var app = builder . Build ( ) ;
690+
691+ Assert . True ( string . IsNullOrEmpty ( app . Configuration [ "A" ] ) ) ;
692+ Assert . True ( string . IsNullOrEmpty ( app . Configuration [ "C" ] ) ) ;
693+
694+ Assert . Equal ( "B" , app . Configuration [ "B" ] ) ;
695+
696+ Assert . Same ( builder . Configuration , app . Configuration ) ;
697+ }
698+
699+ [ Fact ]
700+ public async Task WebApplicationCanHandleStreamBackedConfigurationAddedInBuild ( )
701+ {
702+ static Stream CreateStreamFromString ( string data ) => new MemoryStream ( Encoding . UTF8 . GetBytes ( data ) ) ;
703+
704+ using var jsonAStream = CreateStreamFromString ( @"{ ""A"": ""A"" }" ) ;
705+ using var jsonBStream = CreateStreamFromString ( @"{ ""B"": ""B"" }" ) ;
706+
707+ // This mimics what WebApplicationFactory<T> does and runs configure
708+ // services callbacks
709+ using var listener = new HostingListener ( hostBuilder =>
710+ {
711+ hostBuilder . ConfigureHostConfiguration ( config => config . AddJsonStream ( jsonAStream ) ) ;
712+ hostBuilder . ConfigureAppConfiguration ( config => config . AddJsonStream ( jsonBStream ) ) ;
713+ } ) ;
714+
715+ var builder = WebApplication . CreateBuilder ( ) ;
716+ await using var app = builder . Build ( ) ;
717+
718+ Assert . Equal ( "A" , app . Configuration [ "A" ] ) ;
719+ Assert . Equal ( "B" , app . Configuration [ "B" ] ) ;
720+
721+ Assert . Same ( builder . Configuration , app . Configuration ) ;
722+ }
723+
654724 [ Fact ]
655725 public void WebApplicationBuilderHostProperties_IsCaseSensitive ( )
656726 {
@@ -704,7 +774,6 @@ public void CanResolveIConfigurationBeforeBuildingApplication()
704774
705775 var app = builder . Build ( ) ;
706776
707- // These are different
708777 Assert . Same ( app . Configuration , builder . Configuration ) ;
709778 }
710779
@@ -721,7 +790,6 @@ public void ManuallyAddingConfigurationAsServiceWorks()
721790
722791 var app = builder . Build ( ) ;
723792
724- // These are different
725793 Assert . Same ( app . Configuration , builder . Configuration ) ;
726794 }
727795
0 commit comments