1+ using System ;
2+ using FluentAssertions ;
3+ using Microsoft . AspNetCore . Builder ;
4+ using Microsoft . Extensions . DependencyInjection ;
5+ using NetEscapades . AspNetCore . SecurityHeaders . Infrastructure ;
6+
7+ namespace NetEscapades . AspNetCore . SecurityHeaders . Test ;
8+
9+ public class SecurityHeadersMiddlewareExtensionsTests
10+ {
11+ [ Test ]
12+ public void AddSecurityHeaderPolicies_NullPolicyByDefault ( )
13+ {
14+ var serviceCollection = new ServiceCollection ( ) ;
15+ var provider = serviceCollection . BuildServiceProvider ( ) ;
16+ var opts = SecurityHeadersMiddlewareExtensions . GetOptions ( provider ) ;
17+
18+ opts . Should ( ) . BeNull ( ) ;
19+ }
20+
21+ [ Test ]
22+ [ MatrixDataSource ]
23+ public void AddSecurityHeaderPolicies_OverwritesPreviousDefault (
24+ [ Matrix ] RegistrationType first ,
25+ [ Matrix ] RegistrationType second )
26+ {
27+ var serviceCollection = new ServiceCollection ( ) ;
28+ HeaderPolicyCollection ? policy1 = null ;
29+ switch ( first )
30+ {
31+ case RegistrationType . Direct :
32+ serviceCollection . AddSecurityHeaderPolicies ( ) . SetDefaultPolicy ( p => { policy1 = p ; } ) ;
33+ break ;
34+ case RegistrationType . Func :
35+ serviceCollection . AddSecurityHeaderPolicies ( o => o . SetDefaultPolicy ( p => { policy1 = p ; } ) ) ;
36+ break ;
37+ case RegistrationType . FuncWithProvider :
38+ serviceCollection . AddSecurityHeaderPolicies ( ( o , _ ) => o . SetDefaultPolicy ( p => { policy1 = p ; } ) ) ;
39+ break ;
40+ }
41+
42+ HeaderPolicyCollection ? policy2 = null ;
43+ switch ( first )
44+ {
45+ case RegistrationType . Direct :
46+ serviceCollection . AddSecurityHeaderPolicies ( ) . SetDefaultPolicy ( p => { policy2 = p ; } ) ;
47+ break ;
48+ case RegistrationType . Func :
49+ serviceCollection . AddSecurityHeaderPolicies ( o => o . SetDefaultPolicy ( p => { policy2 = p ; } ) ) ;
50+ break ;
51+ case RegistrationType . FuncWithProvider :
52+ serviceCollection . AddSecurityHeaderPolicies ( ( o , _ ) => o . SetDefaultPolicy ( p => { policy2 = p ; } ) ) ;
53+ break ;
54+ }
55+
56+ var provider = serviceCollection . BuildServiceProvider ( ) ;
57+ var opts = SecurityHeadersMiddlewareExtensions . GetOptions ( provider ) ;
58+
59+ policy1 . Should ( ) . NotBeNull ( ) ;
60+ policy2 . Should ( ) . NotBeNull ( ) ;
61+ opts . Should ( ) . NotBeNull ( ) ;
62+ opts . DefaultPolicy . Should ( ) . BeSameAs ( policy2 ) ;
63+ }
64+
65+ [ Test ]
66+ public void AddSecurityHeaderPolicies_OverwritesMultiplePreviousDefault ( )
67+ {
68+ var serviceCollection = new ServiceCollection ( ) ;
69+ HeaderPolicyCollection ? policy1 = null ;
70+ serviceCollection . AddSecurityHeaderPolicies ( ) . SetDefaultPolicy ( p => { policy1 = p ; } ) ;
71+ HeaderPolicyCollection ? policy2 = null ;
72+ serviceCollection . AddSecurityHeaderPolicies ( o => o . SetDefaultPolicy ( p => { policy2 = p ; } ) ) ;
73+ HeaderPolicyCollection ? policy3 = null ;
74+ serviceCollection . AddSecurityHeaderPolicies ( ( o , _ ) => o . SetDefaultPolicy ( p => { policy3 = p ; } ) ) ;
75+
76+ var provider = serviceCollection . BuildServiceProvider ( ) ;
77+ var opts = SecurityHeadersMiddlewareExtensions . GetOptions ( provider ) ;
78+
79+ policy1 . Should ( ) . NotBeNull ( ) ;
80+ policy2 . Should ( ) . NotBeNull ( ) ;
81+ policy3 . Should ( ) . NotBeNull ( ) ;
82+ opts . Should ( ) . NotBeNull ( ) ;
83+ opts . DefaultPolicy . Should ( ) . BeSameAs ( policy3 ) ;
84+ }
85+
86+ [ Test ]
87+ [ MatrixDataSource ]
88+ public void AddSecurityHeaderPolicies_OverwritesPreviousPolicySelector (
89+ [ Matrix ] RegistrationType first ,
90+ [ Matrix ] RegistrationType second )
91+ {
92+ var serviceCollection = new ServiceCollection ( ) ;
93+ Func < PolicySelectorContext , IReadOnlyHeaderPolicyCollection > selector1 = x => x . DefaultPolicy ;
94+ Func < PolicySelectorContext , IReadOnlyHeaderPolicyCollection > selector2 = x => x . DefaultPolicy ;
95+ SetSelector ( serviceCollection , first , selector1 ) ;
96+ SetSelector ( serviceCollection , second , selector2 ) ;
97+ var provider = serviceCollection . BuildServiceProvider ( ) ;
98+ var opts = SecurityHeadersMiddlewareExtensions . GetOptions ( provider ) ;
99+
100+ opts . Should ( ) . NotBeNull ( ) ;
101+ opts . PolicySelector . Should ( ) . BeSameAs ( selector2 ) ;
102+
103+ static void SetSelector ( ServiceCollection services , RegistrationType type ,
104+ Func < PolicySelectorContext , IReadOnlyHeaderPolicyCollection > selector )
105+ {
106+ switch ( type )
107+ {
108+ case RegistrationType . Direct :
109+ services . AddSecurityHeaderPolicies ( ) . SetPolicySelector ( selector ) ;
110+ break ;
111+ case RegistrationType . Func :
112+ services . AddSecurityHeaderPolicies ( o => o . SetPolicySelector ( selector ) ) ;
113+ break ;
114+ case RegistrationType . FuncWithProvider :
115+ services . AddSecurityHeaderPolicies ( ( o , _ ) => o . SetPolicySelector ( selector ) ) ;
116+ break ;
117+ default :
118+ throw new ArgumentOutOfRangeException ( nameof ( type ) , type , null ) ;
119+ }
120+ }
121+ }
122+
123+ [ Test ]
124+ public void AddSecurityHeaderPolicies_OverwritesAndIncludesPreviousNamedPolicies ( )
125+ {
126+ var serviceCollection = new ServiceCollection ( ) ;
127+
128+ var name1 = "name1" ;
129+ var name2 = "name2" ;
130+ var name3 = "name3" ;
131+ var replacement = new HeaderPolicyCollection ( ) ;
132+ serviceCollection . AddSecurityHeaderPolicies ( ) . AddPolicy ( name1 , new HeaderPolicyCollection ( ) ) ;
133+ serviceCollection . AddSecurityHeaderPolicies ( o => o . AddPolicy ( name2 , new HeaderPolicyCollection ( ) ) ) ;
134+ serviceCollection . AddSecurityHeaderPolicies ( ( o , _ ) => o . AddPolicy ( name3 , new HeaderPolicyCollection ( ) ) ) ;
135+ serviceCollection . AddSecurityHeaderPolicies ( ) . AddPolicy ( name3 , replacement ) ;
136+
137+ var provider = serviceCollection . BuildServiceProvider ( ) ;
138+ var opts = SecurityHeadersMiddlewareExtensions . GetOptions ( provider ) ;
139+
140+ opts . Should ( ) . NotBeNull ( ) ;
141+ opts . NamedPolicyCollections . Should ( ) . ContainKeys ( name1 , name2 , name3 ) . And . HaveCount ( 3 ) ;
142+ opts . NamedPolicyCollections [ name3 ] . Should ( ) . BeSameAs ( replacement ) ;
143+ }
144+
145+ public enum RegistrationType
146+ {
147+ Direct ,
148+ Func ,
149+ FuncWithProvider
150+ }
151+ }
0 commit comments