@@ -149,7 +149,7 @@ public void SetItems_NewCollectionUsed()
149
149
items [ "foo" ] = item ;
150
150
Assert . Same ( item , context . Items [ "foo" ] ) ;
151
151
}
152
-
152
+
153
153
[ Fact ]
154
154
public void UpdateFeatures_ClearsCachedFeatures ( )
155
155
{
@@ -158,75 +158,73 @@ public void UpdateFeatures_ClearsCachedFeatures()
158
158
features . Set < IHttpResponseFeature > ( new HttpResponseFeature ( ) ) ;
159
159
features . Set < IHttpWebSocketFeature > ( new TestHttpWebSocketFeature ( ) ) ;
160
160
161
+ // featurecollection is set. all cached interfaces are null.
161
162
var context = new DefaultHttpContext ( features ) ;
162
-
163
+ TestAllCachedFeaturesAreNull ( context , features ) ;
163
164
Assert . Equal ( 3 , features . Count ( ) ) ;
164
165
165
- TestCachedFeaturesAreNull ( context , features ) ;
166
- TestCachedFeaturesAreNull ( context . Request , features ) ;
167
- TestCachedFeaturesAreNull ( context . Response , features ) ;
168
- TestCachedFeaturesAreNull ( context . Authentication , features ) ;
169
- TestCachedFeaturesAreNull ( context . Connection , features ) ;
170
- TestCachedFeaturesAreNull ( context . WebSockets , features ) ;
171
-
172
- context . Session = new TestSession ( ) ;
173
-
174
- TestCachedFeaturesAreSet ( context , features ) ;
175
- TestCachedFeaturesAreSet ( context . Request , features ) ;
176
- TestCachedFeaturesAreSet ( context . Response , features ) ;
177
- TestCachedFeaturesAreSet ( context . Authentication , features ) ;
178
- TestCachedFeaturesAreSet ( context . Connection , features ) ;
179
- TestCachedFeaturesAreSet ( context . WebSockets , features ) ;
180
-
166
+ // getting feature properties populates feature collection with defaults
167
+ TestAllCachedFeaturesAreSet ( context , features ) ;
181
168
Assert . NotEqual ( 3 , features . Count ( ) ) ;
182
169
170
+ // featurecollection is null. and all cached interfaces are null.
171
+ // only top level is tested because child objects are inaccessible.
183
172
context . Uninitialize ( ) ;
173
+ TestCachedFeaturesAreNull ( context , null ) ;
174
+
175
+
184
176
var newFeatures = new FeatureCollection ( ) ;
185
177
newFeatures . Set < IHttpRequestFeature > ( new HttpRequestFeature ( ) ) ;
186
178
newFeatures . Set < IHttpResponseFeature > ( new HttpResponseFeature ( ) ) ;
187
179
newFeatures . Set < IHttpWebSocketFeature > ( new TestHttpWebSocketFeature ( ) ) ;
188
- context . Initialize ( newFeatures ) ;
189
180
181
+ // featurecollection is set to newFeatures. all cached interfaces are null.
182
+ context . Initialize ( newFeatures ) ;
183
+ TestAllCachedFeaturesAreNull ( context , newFeatures ) ;
190
184
Assert . Equal ( 3 , newFeatures . Count ( ) ) ;
191
185
192
- TestCachedFeaturesAreNull ( context , newFeatures ) ;
193
- TestCachedFeaturesAreNull ( context . Request , newFeatures ) ;
194
- TestCachedFeaturesAreNull ( context . Response , newFeatures ) ;
195
- TestCachedFeaturesAreNull ( context . Authentication , newFeatures ) ;
196
- TestCachedFeaturesAreNull ( context . Connection , newFeatures ) ;
197
- TestCachedFeaturesAreNull ( context . WebSockets , newFeatures ) ;
198
-
199
- context . Session = new TestSession ( ) ;
200
-
201
- TestCachedFeaturesAreSet ( context , newFeatures ) ;
202
- TestCachedFeaturesAreSet ( context . Request , newFeatures ) ;
203
- TestCachedFeaturesAreSet ( context . Response , newFeatures ) ;
204
- TestCachedFeaturesAreSet ( context . Authentication , newFeatures ) ;
205
- TestCachedFeaturesAreSet ( context . Connection , newFeatures ) ;
206
- TestCachedFeaturesAreSet ( context . WebSockets , newFeatures ) ;
207
-
186
+ // getting feature properties populates new feature collection with defaults
187
+ TestAllCachedFeaturesAreSet ( context , newFeatures ) ;
208
188
Assert . NotEqual ( 3 , newFeatures . Count ( ) ) ;
209
189
}
210
190
191
+ void TestAllCachedFeaturesAreNull ( HttpContext context , IFeatureCollection features )
192
+ {
193
+ TestCachedFeaturesAreNull ( context , features ) ;
194
+ TestCachedFeaturesAreNull ( context . Request , features ) ;
195
+ TestCachedFeaturesAreNull ( context . Response , features ) ;
196
+ TestCachedFeaturesAreNull ( context . Authentication , features ) ;
197
+ TestCachedFeaturesAreNull ( context . Connection , features ) ;
198
+ TestCachedFeaturesAreNull ( context . WebSockets , features ) ;
199
+ }
200
+
211
201
void TestCachedFeaturesAreNull ( object value , IFeatureCollection features )
212
202
{
213
203
var type = value . GetType ( ) ;
214
204
215
- var fields = type
205
+ var field = type
216
206
. GetFields ( BindingFlags . NonPublic | BindingFlags . Instance )
217
- . Where ( f => f . FieldType . GetTypeInfo ( ) . IsInterface ) ;
207
+ . Single ( f =>
208
+ f . FieldType . GetTypeInfo ( ) . IsGenericType &&
209
+ f . FieldType . GetGenericTypeDefinition ( ) == typeof ( FeatureReferences < > ) ) ;
218
210
219
- foreach ( var field in fields )
220
- {
221
- if ( field . FieldType == typeof ( IFeatureCollection ) )
222
- {
223
- Assert . Same ( features , field . GetValue ( value ) ) ;
224
- }
225
- else
226
- {
227
- Assert . Null ( field . GetValue ( value ) ) ;
228
- }
229
- }
211
+ var boxedExpectedStruct = features == null ?
212
+ Activator . CreateInstance ( field . FieldType ) :
213
+ Activator . CreateInstance ( field . FieldType , features ) ;
214
+
215
+ var boxedActualStruct = field . GetValue ( value ) ;
216
+
217
+ Assert . Equal ( boxedExpectedStruct , boxedActualStruct ) ;
218
+ }
219
+
220
+ void TestAllCachedFeaturesAreSet ( HttpContext context , IFeatureCollection features )
221
+ {
222
+ TestCachedFeaturesAreSet ( context , features ) ;
223
+ TestCachedFeaturesAreSet ( context . Request , features ) ;
224
+ TestCachedFeaturesAreSet ( context . Response , features ) ;
225
+ TestCachedFeaturesAreSet ( context . Authentication , features ) ;
226
+ TestCachedFeaturesAreSet ( context . Connection , features ) ;
227
+ TestCachedFeaturesAreSet ( context . WebSockets , features ) ;
230
228
}
231
229
232
230
void TestCachedFeaturesAreSet ( object value , IFeatureCollection features )
0 commit comments