React to Session api changes

This commit is contained in:
Kiran Challa 2015-05-27 13:13:43 -07:00
parent 88d0efc2db
commit f5a880ba32
4 changed files with 34 additions and 81 deletions

View File

@ -5,11 +5,8 @@ using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Session;
using Microsoft.Framework.Caching.Distributed;
using Microsoft.Framework.Caching.Memory;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging.Testing; using MusicStore.Controllers;
using MusicStore.Models; using MusicStore.Models;
using Xunit; using Xunit;
@ -42,11 +39,7 @@ namespace MusicStore.Components
// Session initialization // Session initialization
var cartId = "CartId_A"; var cartId = "CartId_A";
var sessionFeature = new SessionFeature() viewContext.HttpContext.SetFeature<ISessionFeature>(new TestSessionFeature());
{
Session = CreateTestSession(),
};
viewContext.HttpContext.SetFeature<ISessionFeature>(sessionFeature);
viewContext.HttpContext.Session.SetString("Session", cartId); viewContext.HttpContext.Session.SetString("Session", cartId);
// DbContext initialization // DbContext initialization
@ -71,17 +64,6 @@ namespace MusicStore.Components
Assert.Equal("AlbumA", cartSummaryComponent.ViewBag.CartSummary); Assert.Equal("AlbumA", cartSummaryComponent.ViewBag.CartSummary);
} }
private static ISession CreateTestSession()
{
return new DistributedSession(
new LocalCache(new MemoryCache(new MemoryCacheOptions())),
"sessionId_A",
idleTimeout: TimeSpan.MaxValue,
tryEstablishSession: () => true,
loggerFactory: new NullLoggerFactory(),
isNewSessionKey: true);
}
private static void PopulateData(MusicStoreContext context, string cartId, string albumTitle, int itemCount) private static void PopulateData(MusicStoreContext context, string cartId, string albumTitle, int itemCount)
{ {
var album = new Album() var album = new Album()

View File

@ -8,11 +8,7 @@ using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Session;
using Microsoft.Framework.Caching.Distributed;
using Microsoft.Framework.Caching.Memory;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging.Testing;
using MusicStore.Models; using MusicStore.Models;
using Xunit; using Xunit;
@ -62,11 +58,7 @@ namespace MusicStore.Controllers
// Session initialization // Session initialization
var cartId = "CartId_A"; var cartId = "CartId_A";
var sessionFeature = new SessionFeature() httpContext.SetFeature<ISessionFeature>(new TestSessionFeature());
{
Session = CreateTestSession(),
};
httpContext.SetFeature<ISessionFeature>(sessionFeature);
httpContext.Session.SetString("Session", cartId); httpContext.Session.SetString("Session", cartId);
// FormCollection initialization // FormCollection initialization
@ -241,17 +233,6 @@ namespace MusicStore.Controllers
Assert.Equal("Error", viewResult.ViewName); Assert.Equal("Error", viewResult.ViewName);
} }
private static ISession CreateTestSession()
{
return new DistributedSession(
new LocalCache(new MemoryCache(new MemoryCacheOptions())),
"sessionId_A",
idleTimeout: TimeSpan.MaxValue,
tryEstablishSession: () => true,
loggerFactory: new NullLoggerFactory(),
isNewSessionKey: true);
}
private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem) private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem)
{ {
var albums = Enumerable.Range(1, 10).Select(n => var albums = Enumerable.Range(1, 10).Select(n =>

View File

@ -41,13 +41,8 @@ namespace MusicStore.Controllers
public async Task Index_ReturnsNoCartItems_WhenSessionEmpty() public async Task Index_ReturnsNoCartItems_WhenSessionEmpty()
{ {
// Arrange // Arrange
var sessionFeature = new SessionFeature()
{
Session = CreateTestSession(),
};
var httpContext = new DefaultHttpContext(); var httpContext = new DefaultHttpContext();
httpContext.SetFeature<ISessionFeature>(sessionFeature); httpContext.SetFeature<ISessionFeature>(new TestSessionFeature());
var controller = new ShoppingCartController() var controller = new ShoppingCartController()
{ {
@ -72,13 +67,8 @@ namespace MusicStore.Controllers
public async Task Index_ReturnsNoCartItems_WhenNoItemsInCart() public async Task Index_ReturnsNoCartItems_WhenNoItemsInCart()
{ {
// Arrange // Arrange
var sessionFeature = new SessionFeature()
{
Session = CreateTestSession(),
};
var httpContext = new DefaultHttpContext(); var httpContext = new DefaultHttpContext();
httpContext.SetFeature<ISessionFeature>(sessionFeature); httpContext.SetFeature<ISessionFeature>(new TestSessionFeature());
httpContext.Session.SetString("Session", "CartId_A"); httpContext.Session.SetString("Session", "CartId_A");
var controller = new ShoppingCartController() var controller = new ShoppingCartController()
@ -105,13 +95,8 @@ namespace MusicStore.Controllers
{ {
// Arrange // Arrange
var cartId = "CartId_A"; var cartId = "CartId_A";
var sessionFeature = new SessionFeature()
{
Session = CreateTestSession(),
};
var httpContext = new DefaultHttpContext(); var httpContext = new DefaultHttpContext();
httpContext.SetFeature<ISessionFeature>(sessionFeature); httpContext.SetFeature<ISessionFeature>(new TestSessionFeature());
httpContext.Session.SetString("Session", cartId); httpContext.Session.SetString("Session", cartId);
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>(); var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
@ -147,13 +132,8 @@ namespace MusicStore.Controllers
{ {
// Arrange // Arrange
var albumId = 3; var albumId = 3;
var sessionFeature = new SessionFeature()
{
Session = CreateTestSession(),
};
var httpContext = new DefaultHttpContext(); var httpContext = new DefaultHttpContext();
httpContext.SetFeature<ISessionFeature>(sessionFeature); httpContext.SetFeature<ISessionFeature>(new TestSessionFeature());
httpContext.Session.SetString("Session", "CartId_A"); httpContext.Session.SetString("Session", "CartId_A");
// Creates the albums of AlbumId = 1 ~ 10. // Creates the albums of AlbumId = 1 ~ 10.
@ -192,11 +172,7 @@ namespace MusicStore.Controllers
var httpContext = new DefaultHttpContext(); var httpContext = new DefaultHttpContext();
// Session and cart initialization // Session and cart initialization
var sessionFeature = new SessionFeature() httpContext.SetFeature<ISessionFeature>(new TestSessionFeature());
{
Session = CreateTestSession(),
};
httpContext.SetFeature<ISessionFeature>(sessionFeature);
httpContext.Session.SetString("Session", cartId); httpContext.Session.SetString("Session", cartId);
// DbContext initialization // DbContext initialization
@ -243,18 +219,6 @@ namespace MusicStore.Controllers
Assert.False((await cart.GetCartItems()).Any(c => c.CartItemId == cartItemId)); Assert.False((await cart.GetCartItems()).Any(c => c.CartItemId == cartItemId));
} }
private static ISession CreateTestSession()
{
return new DistributedSession(
new LocalCache(new MemoryCache(new MemoryCacheOptions())),
"sessionId_A",
idleTimeout: TimeSpan.MaxValue,
tryEstablishSession: () => true,
loggerFactory: new NullLoggerFactory(),
isNewSessionKey: true);
}
private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem) private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem)
{ {
var albums = CreateTestAlbums(itemPrice); var albums = CreateTestAlbums(itemPrice);

View File

@ -0,0 +1,26 @@
using System;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Session;
using Microsoft.Framework.Caching.Distributed;
using Microsoft.Framework.Caching.Memory;
using Microsoft.Framework.Logging.Testing;
namespace MusicStore.Controllers
{
public class TestSessionFeature : ISessionFeature
{
public ISession Session
{
get
{
return new DistributedSession(
new LocalCache(new MemoryCache(new MemoryCacheOptions())),
"sessionId_A",
idleTimeout: TimeSpan.MaxValue,
tryEstablishSession: () => true,
loggerFactory: new NullLoggerFactory(),
isNewSessionKey: true);
}
}
}
}