/// <summary>
        /// <para>Get instance of the controller</para>
        /// </summary>
        protected override IController GetControllerInstance(
            RequestContext requestContext, Type controllerType)
        {
            var controller = base.GetControllerInstance(requestContext, controllerType);

            if (controller != null &&
                typeof(Controller).IsAssignableFrom(controller.GetType()))
            {
                // ITempDataProvider
                var tempDataProvider = new CookieTempDataProvider(requestContext.HttpContext);
                ((Controller)controller).TempDataProvider = tempDataProvider;
            }

            return(controller);
        }
        /// <summary> 
        /// <para>Get instance of the controller</para> 
        /// </summary> 
        protected override IController GetControllerInstance(
                RequestContext requestContext, Type controllerType)
        {
            var controller = base.GetControllerInstance(requestContext, controllerType);

            if (controller != null
                    && typeof(Controller).IsAssignableFrom(controller.GetType()))
            {
                // ITempDataProvider 
                var tempDataProvider = new CookieTempDataProvider(requestContext.HttpContext);
                ((Controller)controller).TempDataProvider = tempDataProvider;
            }

            return controller;
        }
        public void WhenLoadTempDataFromUnexistingCookie_ThenReturnsEmptyTempDataDictionary()
        {
            var cookies = new HttpCookieCollection();

            var requestMock = new Mock<HttpRequestBase>();
            requestMock.Setup(r => r.Cookies).Returns(cookies);

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(c => c.Request).Returns(requestMock.Object);

            var provider = new CookieTempDataProvider(httpContextMock.Object);

            var tempData = provider.LoadTempData(null);
            
            Assert.NotNull(tempData);
            Assert.Equal(0, tempData.Count);
        }
        public void WhenLoadingDataFromEmptyCookie_ThenReturnsEmptyDictionary()
        {
            var cookie = new HttpCookie("__ControllerTempData");

            cookie.Value = string.Empty;
            var cookies = new HttpCookieCollection();
            cookies.Add(cookie);

            var requestMock = new Mock<HttpRequestBase>();
            requestMock.Setup(r => r.Cookies).Returns(cookies);

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(c => c.Request).Returns(requestMock.Object);

            var provider = new CookieTempDataProvider(httpContextMock.Object);

            var tempData = provider.LoadTempData(null);
            Assert.NotNull(tempData);
            Assert.Equal(0, tempData.Count);
        }
        public void WhenLoadingTempDatFromValidCookie_ThenReturnsDictionaryWithEntries()
        {
            var cookie = new HttpCookie("__ControllerTempData");
            var initialTempData = new Dictionary<string, object>();
            initialTempData.Add("WhatIsInHere?", "Stuff");
            cookie.Value = CookieTempDataProvider.SerializeToBase64EncodedString(initialTempData);
            var cookies = new HttpCookieCollection();
            cookies.Add(cookie);

            var requestMock = new Mock<HttpRequestBase>();
            requestMock.Setup(r => r.Cookies).Returns(cookies);

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(c => c.Request).Returns(requestMock.Object);
            httpContextMock.Setup(c => c.Response).Returns((HttpResponseBase)null);

            var provider = new CookieTempDataProvider(httpContextMock.Object);

            var tempData = provider.LoadTempData(null);
            Assert.Equal("Stuff", tempData["WhatIsInHere?"]);
        }
        public void WhenSavingDictionary_ThenDataGetsSerializedInCookie()
        {
            var cookies = new HttpCookieCollection();
            var responseMock = new Mock<HttpResponseBase>();
            responseMock.Setup(r => r.Cookies).Returns(cookies);

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(c => c.Response).Returns(responseMock.Object);

            var provider = new CookieTempDataProvider(httpContextMock.Object);
            var tempData = new Dictionary<string, object>();
            
            tempData.Add("Testing", "Turn it up to 11");
            tempData.Add("Testing2", 1.23);

            provider.SaveTempData(null, tempData);
            var cookie = cookies["__ControllerTempData"];
            var serialized = cookie.Value;
            var deserializedTempData = CookieTempDataProvider.DeserializeTempData(serialized);
            
            Assert.Equal("Turn it up to 11", deserializedTempData["Testing"]);
            Assert.Equal(1.23, deserializedTempData["Testing2"]);
        }