From ec5b6d00ce42924c45d422f18e575691099a6ba6 Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Mon, 28 Sep 2015 14:47:56 -0700 Subject: [PATCH] [Fixes #3068] TempData fails silently without sessions middleware --- .../SessionStateTempDataProvider.cs | 22 +++------- .../ViewFeatures/TempDataDictionary.cs | 7 +++ .../SessionStateTempDataProviderTest.cs | 44 +++++++++++-------- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/SessionStateTempDataProvider.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/SessionStateTempDataProvider.cs index aec95be4da..00b7fda01a 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/SessionStateTempDataProvider.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/SessionStateTempDataProvider.cs @@ -59,16 +59,10 @@ public virtual IDictionary LoadTempData(HttpContext context) throw new ArgumentNullException(nameof(context)); } - if (!IsSessionEnabled(context)) - { - // Session middleware is not enabled. No-op - return null; - } - var session = context.Session; if (session == null) { - return null; + throw new InvalidOperationException("Session cannot be null."); } var tempDataDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -162,6 +156,9 @@ public virtual void SaveTempData(HttpContext context, IDictionary 0); if (hasValues) { @@ -171,9 +168,6 @@ public virtual void SaveTempData(HttpContext context, IDictionary() != null; - } - internal void EnsureObjectCanBeSerialized(object item) { var itemType = item.GetType(); diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/TempDataDictionary.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/TempDataDictionary.cs index 694e90f976..d7e1c2809d 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/TempDataDictionary.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/TempDataDictionary.cs @@ -101,6 +101,13 @@ public object this[string key] /// public void Keep() { + // if the data is not loaded, we can assume none of it has been read + // and so silently return. + if (!_loaded) + { + return; + } + Load(); _retainedKeys.Clear(); _retainedKeys.UnionWith(_data.Keys); diff --git a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewFeatures/SessionStateTempDataProviderTest.cs b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewFeatures/SessionStateTempDataProviderTest.cs index 7cae2e63f4..91b7454571 100644 --- a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewFeatures/SessionStateTempDataProviderTest.cs +++ b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewFeatures/SessionStateTempDataProviderTest.cs @@ -16,52 +16,58 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures public class SessionStateTempDataProviderTest { [Fact] - public void Load_NullSession_ReturnsEmptyDictionary() + public void Load_ThrowsException_WhenSessionIsNotBeingEnabled() { // Arrange var testProvider = new SessionStateTempDataProvider(); - // Act - var tempDataDictionary = testProvider.LoadTempData( - GetHttpContext(session: null, sessionEnabled: true)); + // Act & Assert + var exception = Assert.Throws(() => testProvider.LoadTempData( + GetHttpContext(session: null, sessionEnabled: false))); - // Assert - Assert.Null(tempDataDictionary); + Assert.Equal("Session has not been configured for this application or request.", exception.Message); } [Fact] - public void Load_NonNullSession_NoSessionData_ReturnsEmptyDictionary() + public void Save_ThrowsException_WhenSessionIsNotBeingEnabled() { // Arrange var testProvider = new SessionStateTempDataProvider(); + var values = new Dictionary(); + values.Add("key1", "value1"); - // Act - var tempDataDictionary = testProvider.LoadTempData( - GetHttpContext(Mock.Of())); + // Act & Assert + var exception = Assert.Throws(() => testProvider.SaveTempData( + GetHttpContext(session: null, sessionEnabled: false), values)); - // Assert - Assert.Empty(tempDataDictionary); + Assert.Equal("Session has not been configured for this application or request.", exception.Message); } [Fact] - public void Save_NullSession_NullDictionary_DoesNotThrow() + public void Load_NullSession_ReturnsEmptyDictionary() { // Arrange var testProvider = new SessionStateTempDataProvider(); - // Act & Assert (does not throw) - testProvider.SaveTempData(GetHttpContext(session: null, sessionEnabled: false), null); + // Act & Assert + var exception = Assert.Throws(() => testProvider.LoadTempData( + GetHttpContext(session: null, sessionEnabled: true))); + + Assert.Equal("Session cannot be null.", exception.Message); } [Fact] - public void Save_NullSession_EmptyDictionary_DoesNotThrow() + public void Load_NonNullSession_NoSessionData_ReturnsEmptyDictionary() { // Arrange var testProvider = new SessionStateTempDataProvider(); - // Act & Assert (does not throw) - testProvider.SaveTempData( - GetHttpContext(session: null, sessionEnabled: false), new Dictionary()); + // Act + var tempDataDictionary = testProvider.LoadTempData( + GetHttpContext(Mock.Of())); + + // Assert + Assert.Empty(tempDataDictionary); } [Fact]