24
24
using Microsoft . AspNetCore . Tests ;
25
25
using Microsoft . DotNet . RemoteExecutor ;
26
26
using Microsoft . Extensions . Configuration ;
27
+ using Microsoft . Extensions . Configuration . Json ;
28
+ using Microsoft . Extensions . Configuration . UserSecrets ;
27
29
using Microsoft . Extensions . DependencyInjection ;
28
30
using Microsoft . Extensions . DependencyInjection . Extensions ;
29
31
using Microsoft . Extensions . Hosting ;
30
32
using Microsoft . Extensions . Logging ;
31
33
using Microsoft . Extensions . Options ;
32
34
33
35
[ assembly: HostingStartup ( typeof ( WebApplicationTests . TestHostingStartup ) ) ]
36
+ [ assembly: UserSecretsId ( "UserSecret-TestId" ) ]
34
37
35
38
namespace Microsoft . AspNetCore . Tests ;
36
39
@@ -609,6 +612,41 @@ public void ContentRootIsBaseDirectoryWhenCurrentIsSpecialFolderSystem()
609
612
} , options ) ;
610
613
}
611
614
615
+ public static IEnumerable < object [ ] > EnablesAppSettingsConfigurationData
616
+ {
617
+ get
618
+ {
619
+ yield return new object [ ] { ( CreateBuilderOptionsFunc ) CreateBuilderOptions , true } ;
620
+ yield return new object [ ] { ( CreateBuilderOptionsFunc ) CreateBuilderOptions , false } ;
621
+ yield return new object [ ] { ( CreateBuilderOptionsFunc ) CreateSlimBuilderOptions , true } ;
622
+ yield return new object [ ] { ( CreateBuilderOptionsFunc ) CreateSlimBuilderOptions , false } ;
623
+ }
624
+ }
625
+
626
+ [ Theory ]
627
+ [ MemberData ( nameof ( EnablesAppSettingsConfigurationData ) ) ]
628
+ public void WebApplicationBuilderEnablesAppSettingsConfiguration ( CreateBuilderOptionsFunc createBuilder , bool isDevelopment )
629
+ {
630
+ var options = new WebApplicationOptions
631
+ {
632
+ EnvironmentName = isDevelopment ? Environments . Development : Environments . Production
633
+ } ;
634
+
635
+ var webApplication = createBuilder ( options ) . Build ( ) ;
636
+
637
+ var config = Assert . IsType < ConfigurationManager > ( webApplication . Configuration ) ;
638
+ Assert . Contains ( config . Sources , source => source is JsonConfigurationSource jsonSource && jsonSource . Path == "appsettings.json" ) ;
639
+
640
+ if ( isDevelopment )
641
+ {
642
+ Assert . Contains ( config . Sources , source => source is JsonConfigurationSource jsonSource && jsonSource . Path == "appsettings.Development.json" ) ;
643
+ }
644
+ else
645
+ {
646
+ Assert . DoesNotContain ( config . Sources , source => source is JsonConfigurationSource jsonSource && jsonSource . Path == "appsettings.Development.json" ) ;
647
+ }
648
+ }
649
+
612
650
[ Theory ]
613
651
[ MemberData ( nameof ( CreateBuilderOptionsFuncs ) ) ]
614
652
public void WebApplicationBuilderSettingInvalidApplicationDoesNotThrowWhenAssemblyLoadForUserSecretsFail ( CreateBuilderOptionsFunc createBuilder )
@@ -626,6 +664,22 @@ public void WebApplicationBuilderSettingInvalidApplicationDoesNotThrowWhenAssemb
626
664
Assert . Equal ( Environments . Development , webApplication . Environment . EnvironmentName ) ;
627
665
}
628
666
667
+ [ Theory ]
668
+ [ MemberData ( nameof ( CreateBuilderOptionsFuncs ) ) ]
669
+ public void WebApplicationBuilderEnablesUserSecretsInDevelopment ( CreateBuilderOptionsFunc createBuilder )
670
+ {
671
+ var options = new WebApplicationOptions
672
+ {
673
+ ApplicationName = typeof ( WebApplicationTests ) . Assembly . GetName ( ) . Name ,
674
+ EnvironmentName = Environments . Development
675
+ } ;
676
+
677
+ var webApplication = createBuilder ( options ) . Build ( ) ;
678
+
679
+ var config = Assert . IsType < ConfigurationManager > ( webApplication . Configuration ) ;
680
+ Assert . Contains ( config . Sources , source => source is JsonConfigurationSource jsonSource && jsonSource . Path == "secrets.json" ) ;
681
+ }
682
+
629
683
[ Theory ]
630
684
[ MemberData ( nameof ( WebApplicationBuilderConstructorFuncs ) ) ]
631
685
public void WebApplicationBuilderCanConfigureHostSettingsUsingWebApplicationOptions ( WebApplicationBuilderConstructorFunc createBuilder )
@@ -1470,7 +1524,7 @@ public async Task WebApplication_CanCallUseEndpointsWithoutUseRoutingFails(Creat
1470
1524
[ Fact ]
1471
1525
public void WebApplicationCreate_RegistersEventSourceLogger ( )
1472
1526
{
1473
- var listener = new TestEventListener ( ) ;
1527
+ using var listener = new TestEventListener ( ) ;
1474
1528
var app = WebApplication . Create ( ) ;
1475
1529
1476
1530
var logger = app . Services . GetRequiredService < ILogger < WebApplicationTests > > ( ) ;
@@ -1487,7 +1541,7 @@ public void WebApplicationCreate_RegistersEventSourceLogger()
1487
1541
[ MemberData ( nameof ( CreateBuilderFuncs ) ) ]
1488
1542
public void WebApplicationBuilder_CanClearDefaultLoggers ( CreateBuilderFunc createBuilder )
1489
1543
{
1490
- var listener = new TestEventListener ( ) ;
1544
+ using var listener = new TestEventListener ( ) ;
1491
1545
var builder = createBuilder ( ) ;
1492
1546
builder . Logging . ClearProviders ( ) ;
1493
1547
@@ -1590,50 +1644,22 @@ public async Task CanAddMiddlewareBeforeUseRouting(CreateBuilderFunc createBuild
1590
1644
Assert . Equal ( "One" , chosenEndpoint ) ;
1591
1645
}
1592
1646
1593
- public static IEnumerable < object [ ] > OnlyAddsDefaultServicesOnceData
1594
- {
1595
- get
1596
- {
1597
- // The slim builder doesn't add logging services by default
1598
- yield return new object [ ] { ( CreateBuilderFunc ) CreateBuilder , true } ;
1599
- yield return new object [ ] { ( CreateBuilderFunc ) CreateSlimBuilder , false } ;
1600
- }
1601
- }
1602
-
1603
1647
[ Theory ]
1604
- [ MemberData ( nameof ( OnlyAddsDefaultServicesOnceData ) ) ]
1605
- public async Task WebApplicationBuilder_OnlyAddsDefaultServicesOnce ( CreateBuilderFunc createBuilder , bool hasLogging )
1648
+ [ MemberData ( nameof ( CreateBuilderFuncs ) ) ]
1649
+ public async Task WebApplicationBuilder_OnlyAddsDefaultServicesOnce ( CreateBuilderFunc createBuilder )
1606
1650
{
1607
1651
var builder = createBuilder ( ) ;
1608
1652
1609
1653
// IWebHostEnvironment is added by ConfigureDefaults
1610
- var loggingDescriptors = builder . Services . Where ( descriptor => descriptor . ServiceType == typeof ( IConfigureOptions < LoggerFactoryOptions > ) ) ;
1611
- if ( hasLogging )
1612
- {
1613
- Assert . Single ( loggingDescriptors ) ;
1614
- }
1615
- else
1616
- {
1617
- Assert . Empty ( loggingDescriptors ) ;
1618
- }
1619
-
1654
+ Assert . Single ( builder . Services . Where ( descriptor => descriptor . ServiceType == typeof ( IConfigureOptions < LoggerFactoryOptions > ) ) ) ;
1620
1655
// IWebHostEnvironment is added by ConfigureWebHostDefaults
1621
1656
Assert . Single ( builder . Services . Where ( descriptor => descriptor . ServiceType == typeof ( IWebHostEnvironment ) ) ) ;
1622
1657
Assert . Single ( builder . Services . Where ( descriptor => descriptor . ServiceType == typeof ( IOptionsChangeTokenSource < HostFilteringOptions > ) ) ) ;
1623
1658
Assert . Single ( builder . Services . Where ( descriptor => descriptor . ServiceType == typeof ( IServer ) ) ) ;
1624
1659
1625
1660
await using var app = builder . Build ( ) ;
1626
1661
1627
- var loggingServices = app . Services . GetRequiredService < IEnumerable < IConfigureOptions < LoggerFactoryOptions > > > ( ) ;
1628
- if ( hasLogging )
1629
- {
1630
- Assert . Single ( loggingServices ) ;
1631
- }
1632
- else
1633
- {
1634
- Assert . Empty ( loggingServices ) ;
1635
- }
1636
-
1662
+ Assert . Single ( app . Services . GetRequiredService < IEnumerable < IConfigureOptions < LoggerFactoryOptions > > > ( ) ) ;
1637
1663
Assert . Single ( app . Services . GetRequiredService < IEnumerable < IWebHostEnvironment > > ( ) ) ;
1638
1664
Assert . Single ( app . Services . GetRequiredService < IEnumerable < IOptionsChangeTokenSource < HostFilteringOptions > > > ( ) ) ;
1639
1665
Assert . Single ( app . Services . GetRequiredService < IEnumerable < IServer > > ( ) ) ;
@@ -1867,7 +1893,7 @@ public async Task PropertiesPreservedFromInnerApplication(CreateBuilderFunc crea
1867
1893
1868
1894
[ Theory ]
1869
1895
[ MemberData ( nameof ( CreateBuilderOptionsFuncs ) ) ]
1870
- public async Task DeveloperExceptionPageIsOnByDefaltInDevelopment ( CreateBuilderOptionsFunc createBuilder )
1896
+ public async Task DeveloperExceptionPageIsOnByDefaultInDevelopment ( CreateBuilderOptionsFunc createBuilder )
1871
1897
{
1872
1898
var builder = createBuilder ( new WebApplicationOptions ( ) { EnvironmentName = Environments . Development } ) ;
1873
1899
builder . WebHost . UseTestServer ( ) ;
@@ -2331,7 +2357,7 @@ public async Task SupportsDisablingMiddlewareAutoRegistration(CreateBuilderFunc
2331
2357
2332
2358
app . Properties [ "__AuthenticationMiddlewareSet" ] = true ;
2333
2359
2334
- app . MapGet ( "/hello" , ( ClaimsPrincipal user ) => { } ) . AllowAnonymous ( ) ;
2360
+ app . MapGet ( "/hello" , ( ClaimsPrincipal user ) => { } ) . AllowAnonymous ( ) ;
2335
2361
2336
2362
Assert . True ( app . Properties . ContainsKey ( "__AuthenticationMiddlewareSet" ) ) ;
2337
2363
Assert . False ( app . Properties . ContainsKey ( "__AuthorizationMiddlewareSet" ) ) ;
0 commit comments