コード例 #1
0
        public async void SetAndReleaseItemExclusiveAsync_Should_Create_New_SessionItem_If_Session_Is_New()
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());

            var sessionCollection = new SessionStateItemCollection();
            var now = DateTime.UtcNow;

            sessionCollection["test1"] = "test1";
            sessionCollection["test2"] = now;
            var data = new SessionStateStoreData(sessionCollection, new HttpStaticObjectsCollection(), TestTimeout);

            byte[] buff;
            int    length;

            SqlSessionStateProviderAsync.SerializeStoreData(data, DefaultItemLength, out buff, out length, false);

            var repoMoq            = new Mock <ISqlSessionStateRepository>();
            var sessionItemCreated = false;

            repoMoq.Setup(repo => repo.CreateOrUpdateSessionStateItemAsync(true, SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId),
                                                                           buff, length, TestTimeout, 0, 0))
            .Returns(Task.CompletedTask)
            .Callback(() => sessionItemCreated = true);
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            await provider.SetAndReleaseItemExclusiveAsync(httpContext, TestSessionId, data, null, true, CancellationToken.None);

            Assert.True(sessionItemCreated);
        }
コード例 #2
0
        public async void InitializeRequest_Should_Reset_OrigStreamLen()
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());
            Assert.Equal(0, provider.OrigStreamLen);

            var sessionCollection = new SessionStateItemCollection();
            var now = DateTime.UtcNow;

            sessionCollection["test1"] = "test1";
            sessionCollection["test2"] = now;
            var data = new SessionStateStoreData(sessionCollection, new HttpStaticObjectsCollection(), TestTimeout);

            byte[] buff;
            int    length;

            SqlSessionStateProviderAsync.SerializeStoreData(data, DefaultItemLength, out buff, out length, false);

            var repoMoq = new Mock <ISqlSessionStateRepository>();
            var ssItem  = new SessionItem(buff, true, TimeSpan.Zero, 1, SessionStateActions.None);

            repoMoq.Setup(repo => repo.GetSessionStateItemAsync(SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId), false)).Returns(Task.FromResult(ssItem));
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            var getItemResult = await provider.GetItemAsync(httpContext, TestSessionId, CancellationToken.None);

            Assert.NotEqual(0, provider.OrigStreamLen);

            provider.InitializeRequest(httpContext);
            Assert.Equal(0, provider.OrigStreamLen);
        }
コード例 #3
0
        public void Serialize_And_Deserialized_SessionStateStoreData_RoundTrip_Should_Work(bool enableCompression)
        {
            var sessionCollection = new SessionStateItemCollection();
            var now = DateTime.UtcNow;

            sessionCollection["test1"] = "test1";
            sessionCollection["test2"] = now;
            var data = new SessionStateStoreData(sessionCollection, new HttpStaticObjectsCollection(), TestTimeout);

            byte[] buff;
            int    length;
            SessionStateStoreData deserializedData;

            SqlSessionStateProviderAsync.SerializeStoreData(data, DefaultItemLength, out buff, out length, enableCompression);
            using (var stream = new MemoryStream(buff))
            {
                var httpContext = CreateMoqHttpContextBase();
                deserializedData = SqlSessionStateProviderAsync.DeserializeStoreData(httpContext, stream, enableCompression);
            }

            Assert.Equal(data.Items.Count, deserializedData.Items.Count);
            Assert.Equal(data.Items["test1"], deserializedData.Items["test1"]);
            Assert.Equal(now, (DateTime)deserializedData.Items["test2"]);
            Assert.NotNull(deserializedData.StaticObjects);
            Assert.Equal(data.Timeout, deserializedData.Timeout);
        }
コード例 #4
0
        public async void SetAndReleaseItemExclusiveAsync_Should_Release_NonExclsive_SessionItem()
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());

            var sessionCollection = new SessionStateItemCollection();
            var now = DateTime.UtcNow;

            sessionCollection["test1"] = "test1";
            sessionCollection["test2"] = now;
            var data = new SessionStateStoreData(sessionCollection, new HttpStaticObjectsCollection(), TestTimeout);

            byte[] buff;
            int    length;

            SqlSessionStateProviderAsync.SerializeStoreData(data, DefaultItemLength, out buff, out length, false);

            var repoMoq = new Mock <IPostgresSessionStateRepository>();
            var ssItem  = new SessionItem(buff, false, TimeSpan.Zero, null, SessionStateActions.None);

            repoMoq.Setup(repo => repo.GetSessionStateItemAsync(SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId), false))
            .Returns(Task.FromResult(ssItem));
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            var getItemResult = await provider.GetItemAsync(httpContext, TestSessionId, CancellationToken.None);

            var sessionReleased = false;

            repoMoq.Setup(repo => repo.CreateOrUpdateSessionStateItemAsync(false, SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId),
                                                                           buff, length, TestTimeout, 0, provider.OrigStreamLen))
            .Returns(Task.CompletedTask)
            .Callback(() => sessionReleased = true);

            await provider.SetAndReleaseItemExclusiveAsync(httpContext, TestSessionId, getItemResult.Item, null, false, CancellationToken.None);

            Assert.True(sessionReleased);
        }
コード例 #5
0
        public async void GetItemAsync_Should_Return_SessionItem_If_SessionItem_Is_Unlocked(SessionStateActions action)
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());

            var sessionCollection = new SessionStateItemCollection();
            var now = DateTime.UtcNow;

            sessionCollection["test1"] = "test1";
            sessionCollection["test2"] = now;
            var data = new SessionStateStoreData(sessionCollection, new HttpStaticObjectsCollection(), TestTimeout);

            byte[] buff;
            int    length;

            SqlSessionStateProviderAsync.SerializeStoreData(data, DefaultItemLength, out buff, out length, false);

            var repoMoq = new Mock <ISqlSessionStateRepository>();
            var ssItem  = new SessionItem(buff, false, TimeSpan.Zero, null, action);

            repoMoq.Setup(repo => repo.GetSessionStateItemAsync(SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId), false)).Returns(Task.FromResult(ssItem));
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            var getItemResult = await provider.GetItemAsync(httpContext, TestSessionId, CancellationToken.None);

            Assert.Equal(data.Items.Count, getItemResult.Item.Items.Count);
            Assert.Equal(data.Items["test1"], getItemResult.Item.Items["test1"]);
            Assert.Equal(now, (DateTime)getItemResult.Item.Items["test2"]);
            Assert.NotNull(getItemResult.Item.StaticObjects);
            Assert.Equal(data.Timeout, getItemResult.Item.Timeout);
            Assert.Equal(ssItem.Actions, getItemResult.Actions);
            Assert.Equal(ssItem.LockAge, getItemResult.LockAge);
            Assert.Equal(ssItem.Locked, getItemResult.Locked);
            Assert.Equal(ssItem.LockId, getItemResult.LockId);
            Assert.NotEqual(0, provider.OrigStreamLen);
        }