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

Commit 16c16e8

Browse files
committed
updated tests and some cleanup
1 parent cfa3700 commit 16c16e8

File tree

2 files changed

+68
-61
lines changed

2 files changed

+68
-61
lines changed

src/Microsoft.AspNet.Mvc.Core/SessionStateTempDataProvider.cs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Reflection;
78
using Microsoft.AspNet.Http;
89
using Microsoft.AspNet.Mvc.Core;
910
using Microsoft.Framework.Internal;
@@ -64,8 +65,11 @@ public virtual void SaveTempData([NotNull] HttpContext context, IDictionary<stri
6465
var hasValues = (values != null && values.Count > 0);
6566
if (hasValues)
6667
{
67-
// We want to allow only simple types to be serialized in session.
68-
EnsureObjectCanBeSerialized(values);
68+
foreach (var item in values.Values)
69+
{
70+
// We want to allow only simple types to be serialized in session.
71+
EnsureObjectCanBeSerialized(item);
72+
}
6973

7074
// Accessing Session property will throw if the session middleware is not enabled.
7175
var session = context.Session;
@@ -84,42 +88,39 @@ public virtual void SaveTempData([NotNull] HttpContext context, IDictionary<stri
8488
}
8589
}
8690

87-
private bool IsSessionEnabled(HttpContext context)
91+
private static bool IsSessionEnabled(HttpContext context)
8892
{
8993
return context.GetFeature<ISessionFeature>() != null;
9094
}
9195

92-
internal void EnsureObjectCanBeSerialized(IDictionary<string, object> values)
96+
internal void EnsureObjectCanBeSerialized(object item)
9397
{
94-
foreach (var item in values.Values)
95-
{
96-
var itemType = item.GetType();
97-
Type[] actualTypes = null;
98+
var itemType = item.GetType();
99+
Type[] actualTypes = null;
98100

99-
if (itemType.IsArray)
100-
{
101-
itemType = itemType.GetElementType();
102-
}
103-
else if (itemType.IsGenericType)
101+
if (itemType.IsArray)
102+
{
103+
itemType = itemType.GetElementType();
104+
}
105+
else if (itemType.GetTypeInfo().IsGenericType)
106+
{
107+
if (itemType.ExtractGenericInterface(typeof(IList<>)) != null ||
108+
itemType.ExtractGenericInterface(typeof(IDictionary<,>)) != null)
104109
{
105-
if (itemType.ExtractGenericInterface(typeof(IList<>)) != null ||
106-
itemType.ExtractGenericInterface(typeof(IDictionary<,>)) != null)
107-
{
108-
actualTypes = itemType.GetGenericArguments();
109-
}
110+
actualTypes = itemType.GetGenericArguments();
110111
}
112+
}
111113

112-
actualTypes = actualTypes ?? new Type[] { itemType };
114+
actualTypes = actualTypes ?? new Type[] { itemType };
113115

114-
foreach (var actualType in actualTypes)
116+
foreach (var actualType in actualTypes)
117+
{
118+
var underlyingType = Nullable.GetUnderlyingType(actualType) ?? actualType;
119+
if (!TypeHelper.IsSimpleType(actualType))
115120
{
116-
var underlyingType = Nullable.GetUnderlyingType(actualType) ?? actualType;
117-
if (!TypeHelper.IsSimpleType(actualType))
118-
{
119-
var message = Resources.FormatTempData_CannotSerializeToSession(underlyingType,
120-
typeof(SessionStateTempDataProvider).FullName);
121-
throw new InvalidOperationException(message);
122-
}
121+
var message = Resources.FormatTempData_CannotSerializeToSession(underlyingType,
122+
typeof(SessionStateTempDataProvider).FullName);
123+
throw new InvalidOperationException(message);
123124
}
124125
}
125126
}

test/Microsoft.AspNet.Mvc.Core.Test/SessionStateTempDataProviderTest.cs

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -76,63 +76,69 @@ public void Save_NullSession_NonEmptyDictionary_Throws()
7676
});
7777
}
7878

79+
public static TheoryData<object, Type> InvalidTypes
80+
{
81+
get
82+
{
83+
return new TheoryData<object, Type>
84+
{
85+
{ new object(), typeof(object) },
86+
{ new object[3], typeof(object) },
87+
{ new TestItem(), typeof(TestItem) },
88+
{ new List<TestItem>(), typeof(TestItem) },
89+
{ new Dictionary<string, TestItem>(), typeof(TestItem) },
90+
{ new Dictionary<object, string>(), typeof(object) },
91+
{ new Dictionary<TestItem, TestItem>(), typeof(TestItem) }
92+
};
93+
}
94+
}
95+
7996
[Theory]
8097
[MemberData(nameof(InvalidTypes))]
81-
public void EnsureObjectCanBeSerialized_InvalidType_Throws(string key, object value, Type type)
98+
public void EnsureObjectCanBeSerialized_InvalidType_Throws(object value, Type type)
8299
{
83100
// Arrange
84101
var testProvider = new SessionStateTempDataProvider();
85102

86103
// Act & Assert
87104
var exception = Assert.Throws<InvalidOperationException>(() =>
88105
{
89-
testProvider.EnsureObjectCanBeSerialized(new Dictionary<string, object> { { key, value } });
106+
testProvider.EnsureObjectCanBeSerialized(value);
90107
});
91108
Assert.Equal($"The type {type} cannot be serialized to Session by '{typeof(SessionStateTempDataProvider).FullName}'.",
92109
exception.Message);
93110
}
94111

95-
[Theory]
96-
[MemberData(nameof(ValidTypes))]
97-
public void EnsureObjectCanBeSerialized_ValidType_DoesNotThrow(string key, object value)
98-
{
99-
// Arrange
100-
var testProvider = new SessionStateTempDataProvider();
101-
102-
// Act & Assert (Does not throw)
103-
testProvider.EnsureObjectCanBeSerialized(new Dictionary<string, object> { { key, value } });
104-
}
105-
106-
public static TheoryData<string, object, Type> InvalidTypes
112+
public static TheoryData<object> ValidTypes
107113
{
108114
get
109115
{
110-
return new TheoryData<string, object, Type>
116+
return new TheoryData<object>
111117
{
112-
{ "Object", new object(), typeof(object) },
113-
{ "ObjectArray", new object[3], typeof(object) },
114-
{ "TestItem", new TestItem(), typeof(TestItem) },
115-
{ "ListTestItem", new List<TestItem>(), typeof(TestItem) },
116-
{ "DictTestItem", new Dictionary<string, TestItem>(), typeof(TestItem) }
118+
{ 10 },
119+
{ new int[]{ 10, 20 } },
120+
{ "FooValue" },
121+
{ new Dictionary<string, int>() },
122+
{ new Uri("http://Foo") },
123+
{ Guid.NewGuid() },
124+
{ new List<string> { "foo", "bar" } },
125+
{ new DateTimeOffset() },
126+
{ 100.1m },
127+
{ new Dictionary<Uri, Guid>() },
128+
{ new Uri[] { new Uri("http://Foo"), new Uri("http://Bar") } }
117129
};
118130
}
119131
}
120132

121-
public static TheoryData<string, object> ValidTypes
133+
[Theory]
134+
[MemberData(nameof(ValidTypes))]
135+
public void EnsureObjectCanBeSerialized_ValidType_DoesNotThrow(object value)
122136
{
123-
get
124-
{
125-
return new TheoryData<string, object>
126-
{
127-
{ "int", 10 },
128-
{ "IntArray", new int[]{ 10, 20 } },
129-
{ "string", "FooValue" },
130-
{ "SimpleDict", new Dictionary<string, int>() },
131-
{ "Uri", new Uri("http://Foo") },
132-
{ "Guid", Guid.NewGuid() },
133-
{ "SimpleList", new List<string> { "foo", "bar" } }
134-
};
135-
}
137+
// Arrange
138+
var testProvider = new SessionStateTempDataProvider();
139+
140+
// Act & Assert (Does not throw)
141+
testProvider.EnsureObjectCanBeSerialized(value);
136142
}
137143

138144
private class TestItem

0 commit comments

Comments
 (0)