示例#1
0
        public void DeserializeNoDataTest()
        {
            var data     = new byte[] { };
            var messages = FlashMessage.Deserialize(data);

            Assert.Equal(0, messages.Count);
        }
示例#2
0
        public void SerializeEmptyTest()
        {
            List <FlashMessageModel> messages = new List <FlashMessageModel>();
            var data = FlashMessage.Serialize(messages);

            messages = FlashMessage.Deserialize(data);

            Assert.Equal(0, messages.Count);
        }
示例#3
0
        public List <FlashMessageModel> Peek()
        {
            // Retrieve the data from the session store, guard for cases where it does not exist.
            var context = new HttpContextWrapper(HttpContext.Current);
            var data    = context.Session[KeyName];

            if (data == null)
            {
                return(new List <FlashMessageModel>());
            }

            // Deserialize messages and return them.
            return(FlashMessage.Deserialize((byte[])data));
        }
        public List <FlashMessageModel> Peek()
        {
            // Attempt to retrieve cookie. If the cookie is non existent, return an empty message list.
            var context = new HttpContextWrapper(HttpContext.Current);
            var cookie  = context.Request.Cookies[CookieName];

            if (cookie == null)
            {
                return(new List <FlashMessageModel>());
            }

            // Decode and deserialize the data.
            var serializedMessages = MachineKey.Decode(cookie.Value, MachineKeyProtection.All);

            return(FlashMessage.Deserialize(serializedMessages));
        }
        public List <FlashMessageModel> GetQueued()
        {
            // Attempt to retrieve cookie.
            var context = new HttpContextWrapper(HttpContext.Current);
            var cookie  = context.Response.Cookies[CookieName];

            // If the cookie is non existent, return an empty message list.
            // If the cookie value is null, also return an empty list, as MachineKey.Decode does not like empty strings.
            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            {
                return(new List <FlashMessageModel>());
            }

            // Decode and deserialize the data.
            var serializedMessages = MachineKey.Decode(cookie.Value, MachineKeyProtection.All);

            return(FlashMessage.Deserialize(serializedMessages));
        }
        public List <FlashMessageModel> Retrieve()
        {
            // Attempt to retrieve cookie. If the cookie is non existent, return an empty message list.
            var context = new HttpContextWrapper(HttpContext.Current);
            var cookie  = context.Request.Cookies[CookieName];

            if (cookie == null)
            {
                return(new List <FlashMessageModel>());
            }

            var data = cookie.Value;

            // Clear the cookie by setting it to expired.
            cookie.Value   = null;
            cookie.Expires = DateTime.Now.AddDays(-1);
            context.Response.SetCookie(cookie);

            // Decode and deserialize the data.
            var serializedMessages = MachineKey.Decode(data, MachineKeyProtection.All);

            return(FlashMessage.Deserialize(serializedMessages));
        }
示例#7
0
        public void SerializeSingleTest()
        {
            List <FlashMessageModel> messages = new List <FlashMessageModel>();
            FlashMessageModel        message  = new FlashMessageModel
            {
                IsHtml  = true,
                Message = "<p>test content</p> üñï€ode",
                Title   = "Title",
                Type    = FlashMessageType.Custom
            };

            // Serialize and deserialize.
            messages.Add(message);
            var data = FlashMessage.Serialize(messages);

            messages = FlashMessage.Deserialize(data);
            var deserialized = messages[0];

            // Compare the results.
            Assert.Equal(message.Type, deserialized.Type);
            Assert.Equal(message.Title, deserialized.Title);
            Assert.Equal(message.Message, deserialized.Message);
            Assert.Equal(message.IsHtml, message.IsHtml);
        }