React to Session api review changes

This commit is contained in:
Kiran Challa 2015-06-02 17:17:02 -07:00
parent 6cd277e8aa
commit 0469103683
4 changed files with 44 additions and 56 deletions

View File

@ -100,7 +100,7 @@ namespace MvcSample.Web
#endif
app.UseRequestLocalization();
app.UseInMemorySession();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");

View File

@ -61,11 +61,16 @@ namespace Microsoft.AspNet.Mvc
return null;
}
var tempDataDictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
var session = context.Session;
if (session == null)
{
return null;
}
var tempDataDictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
byte[] value;
if (session != null && session.TryGetValue(TempDataSessionStateKey, out value))
if (session.TryGetValue(TempDataSessionStateKey, out value))
{
using (var memoryStream = new MemoryStream(value))
using (var writer = new BsonReader(memoryStream))
@ -139,7 +144,7 @@ namespace Microsoft.AspNet.Mvc
{
// Since we call Save() after the response has been sent, we need to initialize an empty session
// so that it is established before the headers are sent.
session[TempDataSessionStateKey] = new byte[] { };
session.Set(TempDataSessionStateKey, new byte[] { });
}
return tempDataDictionary;
@ -165,7 +170,7 @@ namespace Microsoft.AspNet.Mvc
using (var writer = new BsonWriter(memoryStream))
{
_jsonSerializer.Serialize(writer, values);
session[TempDataSessionStateKey] = memoryStream.ToArray();
session.Set(TempDataSessionStateKey, memoryStream.ToArray());
}
}
}

View File

@ -2,11 +2,12 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Internal;
using Moq;
using Xunit;
@ -25,7 +26,7 @@ namespace Microsoft.AspNet.Mvc
GetHttpContext(session: null, sessionEnabled: true));
// Assert
Assert.Empty(tempDataDictionary);
Assert.Null(tempDataDictionary);
}
[Fact]
@ -36,7 +37,7 @@ namespace Microsoft.AspNet.Mvc
// Act
var tempDataDictionary = testProvider.LoadTempData(
GetHttpContext(Mock.Of<ISessionCollection>()));
GetHttpContext(Mock.Of<ISession>()));
// Assert
Assert.Empty(tempDataDictionary);
@ -180,7 +181,7 @@ namespace Microsoft.AspNet.Mvc
{
{ "string", "value" }
};
var context = GetHttpContext(new TestSessionCollection(), true);
var context = GetHttpContext(new TestSession(), true);
// Act
testProvider.SaveTempData(context, input);
@ -200,7 +201,7 @@ namespace Microsoft.AspNet.Mvc
{
{ "int", 10 }
};
var context = GetHttpContext(new TestSessionCollection(), true);
var context = GetHttpContext(new TestSession(), true);
// Act
testProvider.SaveTempData(context, input);
@ -222,7 +223,7 @@ namespace Microsoft.AspNet.Mvc
{
{ "bool", value }
};
var context = GetHttpContext(new TestSessionCollection(), true);
var context = GetHttpContext(new TestSession(), true);
// Act
testProvider.SaveTempData(context, input);
@ -243,7 +244,7 @@ namespace Microsoft.AspNet.Mvc
{
{ "DateTime", inputDatetime }
};
var context = GetHttpContext(new TestSessionCollection(), true);
var context = GetHttpContext(new TestSession(), true);
// Act
testProvider.SaveTempData(context, input);
@ -264,7 +265,7 @@ namespace Microsoft.AspNet.Mvc
{
{ "Guid", inputGuid }
};
var context = GetHttpContext(new TestSessionCollection(), true);
var context = GetHttpContext(new TestSession(), true);
// Act
testProvider.SaveTempData(context, input);
@ -284,7 +285,7 @@ namespace Microsoft.AspNet.Mvc
{
{ "List`string", new List<string> { "one", "two" } }
};
var context = GetHttpContext(new TestSessionCollection(), true);
var context = GetHttpContext(new TestSession(), true);
// Act
testProvider.SaveTempData(context, input);
@ -310,7 +311,7 @@ namespace Microsoft.AspNet.Mvc
{
{ "Dictionary", inputDictionary }
};
var context = GetHttpContext(new TestSessionCollection(), true);
var context = GetHttpContext(new TestSession(), true);
// Act
testProvider.SaveTempData(context, input);
@ -330,7 +331,7 @@ namespace Microsoft.AspNet.Mvc
{
{ "EmptyDictionary", new Dictionary<string, int>() }
};
var context = GetHttpContext(new TestSessionCollection(), true);
var context = GetHttpContext(new TestSession(), true);
// Act
testProvider.SaveTempData(context, input);
@ -346,43 +347,35 @@ namespace Microsoft.AspNet.Mvc
public int DummyInt { get; set; }
}
private HttpContext GetHttpContext(ISessionCollection session, bool sessionEnabled=true)
private HttpContext GetHttpContext(ISession session, bool sessionEnabled = true)
{
var httpContext = new Mock<HttpContext>();
if (session != null)
var httpContext = new DefaultHttpContext();
if(sessionEnabled)
{
httpContext.Setup(h => h.Session).Returns(session);
httpContext.SetFeature<ISessionFeature>(new SessionFeature() { Session = session });
}
else if (!sessionEnabled)
{
httpContext.Setup(h => h.Session).Throws<InvalidOperationException>();
}
else
{
httpContext.Setup(h => h.Session[It.IsAny<string>()]);
}
if (sessionEnabled)
{
httpContext.Setup(h => h.GetFeature<ISessionFeature>()).Returns(Mock.Of<ISessionFeature>());
}
return httpContext.Object;
return httpContext;
}
private class TestSessionCollection : ISessionCollection
private class SessionFeature : ISessionFeature
{
public ISession Session { get; set; }
}
private class TestSession : ISession
{
private Dictionary<string, byte[]> _innerDictionary = new Dictionary<string, byte[]>();
public byte[] this[string key]
{
get
{
return _innerDictionary[key];
}
public IEnumerable<string> Keys { get { return _innerDictionary.Keys; } }
set
{
_innerDictionary[key] = value;
}
public Task LoadAsync()
{
return Task.FromResult(0);
}
public Task CommitAsync()
{
return Task.FromResult(0);
}
public void Clear()
@ -390,17 +383,12 @@ namespace Microsoft.AspNet.Mvc
_innerDictionary.Clear();
}
public IEnumerator<KeyValuePair<string, byte[]>> GetEnumerator()
{
return _innerDictionary.GetEnumerator();
}
public void Remove(string key)
{
_innerDictionary.Remove(key);
}
public void Set(string key, ArraySegment<byte> value)
public void Set(string key, byte[] value)
{
_innerDictionary[key] = value.ToArray();
}
@ -409,11 +397,6 @@ namespace Microsoft.AspNet.Mvc
{
return _innerDictionary.TryGetValue(key, out value);
}
IEnumerator IEnumerable.GetEnumerator()
{
return _innerDictionary.GetEnumerator();
}
}
}
}

View File

@ -20,7 +20,7 @@ namespace TempDataWebSite
{
app.UseCultureReplacer();
app.UseInMemorySession();
app.UseSession();
app.UseMvcWithDefaultRoute();
}
}