Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit c95d816

Browse files
committed
Updating unit test to probe _features cache field state
1 parent 35d7106 commit c95d816

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed

test/Microsoft.AspNet.Http.Tests/DefaultHttpContextTests.cs

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void SetItems_NewCollectionUsed()
149149
items["foo"] = item;
150150
Assert.Same(item, context.Items["foo"]);
151151
}
152-
152+
153153
[Fact]
154154
public void UpdateFeatures_ClearsCachedFeatures()
155155
{
@@ -158,75 +158,73 @@ public void UpdateFeatures_ClearsCachedFeatures()
158158
features.Set<IHttpResponseFeature>(new HttpResponseFeature());
159159
features.Set<IHttpWebSocketFeature>(new TestHttpWebSocketFeature());
160160

161+
// featurecollection is set. all cached interfaces are null.
161162
var context = new DefaultHttpContext(features);
162-
163+
TestAllCachedFeaturesAreNull(context, features);
163164
Assert.Equal(3, features.Count());
164165

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);
181168
Assert.NotEqual(3, features.Count());
182169

170+
// featurecollection is null. and all cached interfaces are null.
171+
// only top level is tested because child objects are inaccessible.
183172
context.Uninitialize();
173+
TestCachedFeaturesAreNull(context, null);
174+
175+
184176
var newFeatures = new FeatureCollection();
185177
newFeatures.Set<IHttpRequestFeature>(new HttpRequestFeature());
186178
newFeatures.Set<IHttpResponseFeature>(new HttpResponseFeature());
187179
newFeatures.Set<IHttpWebSocketFeature>(new TestHttpWebSocketFeature());
188-
context.Initialize(newFeatures);
189180

181+
// featurecollection is set to newFeatures. all cached interfaces are null.
182+
context.Initialize(newFeatures);
183+
TestAllCachedFeaturesAreNull(context, newFeatures);
190184
Assert.Equal(3, newFeatures.Count());
191185

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);
208188
Assert.NotEqual(3, newFeatures.Count());
209189
}
210190

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+
211201
void TestCachedFeaturesAreNull(object value, IFeatureCollection features)
212202
{
213203
var type = value.GetType();
214204

215-
var fields = type
205+
var field = type
216206
.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<>));
218210

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);
230228
}
231229

232230
void TestCachedFeaturesAreSet(object value, IFeatureCollection features)

0 commit comments

Comments
 (0)