public void DeserializeNoDataTest() { var data = new byte[] { }; var messages = FlashMessage.Deserialize(data); Assert.Equal(0, messages.Count); }
public void SerializeEmptyTest() { List <FlashMessageModel> messages = new List <FlashMessageModel>(); var data = FlashMessage.Serialize(messages); messages = FlashMessage.Deserialize(data); Assert.Equal(0, messages.Count); }
public void Queue(IList <FlashMessageModel> messages) { // Serialize the messages. var data = FlashMessage.Serialize(messages); // Set the data without doing any further securing or transformations. var context = new HttpContextWrapper(HttpContext.Current); context.Session[KeyName] = data; }
public void Queue() { var tempData = new Mock <ITempDataDictionaryFactory>(); tempData.Setup(x => x.GetTempData(It.IsAny <HttpContext>())).Returns(_tempDataMock.Object); var contextAccessor = new Mock <IHttpContextAccessor>(); var flashMessage = new Vereyon.Web.FlashMessage(tempData.Object, contextAccessor.Object); flashMessage.Info("Test"); }
/// <summary> /// Renders any queued flash messages to and returns the html code. /// </summary> /// <param name="html"></param> /// <returns></returns> public static IHtmlContent RenderFlashMessages(this IHtmlHelper html) { // Retrieve queued messages. var messages = FlashMessage.Retrieve(html.TempData); var output = ""; foreach (var message in messages) { output += RenderFlashMessage(message); } return(new HtmlString(output)); }
/// <summary> /// Renders any queued flash messages to and returns the html code. /// </summary> /// <param name="html"></param> /// <returns></returns> public static IHtmlString RenderFlashMessages(this HtmlHelper html) { // Retrieve queued messages. var messages = FlashMessage.Retrieve(html.ViewContext.HttpContext); var output = ""; foreach (var message in messages) { output += RenderFlashMessage(message); } return(html.Raw(output)); }
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 void Queue(IList <FlashMessageModel> messages) { // Serialize and digitally sign the data. // Encoding/signing is done using the configured encryption and key in the machineKey element. // TODO: Replace with MachineKey.Protect for .Net Framework >= 4.5 var serializedMessages = FlashMessage.Serialize(messages); var data = MachineKey.Encode(serializedMessages, MachineKeyProtection.All); // Serialize messages and enforce cookie size limit. if (data.Length > CookieSizeLimit && CookieSizeLimit > 0) { throw new InvalidOperationException("The flash messages cookie size limit exceeded the limit value. Queue less messages."); } // Set the cookie. var context = new HttpContextWrapper(HttpContext.Current); var cookie = new HttpCookie(CookieName, data); context.Response.SetCookie(cookie); }
public void SerializeSingleTest() { FlashMessageModel message = new FlashMessageModel { IsHtml = true, Message = "<p>test content</p> üñï€ode", Title = "Title", Type = FlashMessageType.Custom }; // Serialize and deserialize. FlashMessage.Queue(message); var messages = FlashMessage.GetQueued(); 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); }
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)); }
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); }