Exemplo n.º 1
0
        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 CanLoadTempDataFromCookie()
        {
            var tempData = new Dictionary <string, object>();

            tempData.Add("abc", "easy as 123");
            tempData.Add("price", 1.234);
            string serializedTempData = CookieTempDataProvider.DictionaryToBase64String(tempData);

            var cookies    = new HttpCookieCollection();
            var httpCookie = new HttpCookie("__ControllerTempData");

            httpCookie.Value = serializedTempData;
            cookies.Add(httpCookie);

            var requestMock = new Mock <HttpRequestBase>();

            requestMock.Setup(r => r.Cookies).Returns(cookies);

            var responseMock = new Mock <HttpResponseBase>();

            responseMock.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(responseMock.Object);

            ITempDataProvider            provider       = new CookieTempDataProvider(httpContextMock.Object);
            IDictionary <string, object> loadedTempData = provider.LoadTempData(null /* controllerContext */);

            Assert.Equal(2, loadedTempData.Count);
            Assert.Equal("easy as 123", loadedTempData["abc"]);
            Assert.Equal(1.234, loadedTempData["price"]);
        }
        public void LoadTempDataIgnoresNullResponseCookieDoesNotThrowException()
        {
            HttpCookie cookie          = new HttpCookie("__ControllerTempData");
            var        initialTempData = new Dictionary <string, object>();

            initialTempData.Add("WhatIsInHere?", "Stuff");
            cookie.Value = CookieTempDataProvider.DictionaryToBase64String(initialTempData);
            var cookies = new HttpCookieCollection();

            cookies.Add(cookie);

            var requestMock = new Mock <HttpRequestBase>();

            requestMock.Setup(r => r.Cookies).Returns(cookies);

            var responseMock = new Mock <HttpResponseBase>();

            responseMock.Setup(r => r.Cookies).Returns((HttpCookieCollection)null);

            var httpContextMock = new Mock <HttpContextBase>();

            httpContextMock.Setup(c => c.Request).Returns(requestMock.Object);
            httpContextMock.Setup(c => c.Response).Returns(responseMock.Object);

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);

            IDictionary <string, object> tempData = provider.LoadTempData(null /* controllerContext */);

            Assert.Equal("Stuff", tempData["WhatIsInHere?"]);
        }
        public void CtorSetsHttpContextProperty()
        {
            var httpContext = new Mock<HttpContextBase>().Object;
            var provider = new CookieTempDataProvider(httpContext);

            Assert.Equal(httpContext, provider.HttpContext);
        }
            public void StoresValuesInCookie()
            {
                var cookies     = new HttpCookieCollection();
                var httpContext = new Mock <HttpContextBase>();

                httpContext.Setup(c => c.Response.Cookies).Returns(cookies);
                ITempDataProvider provider = new CookieTempDataProvider(httpContext.Object);
                var controllerContext      = new ControllerContext();

                provider.SaveTempData(
                    controllerContext,
                    new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase)
                {
                    { "message", "Say hello to my little friend" },
                    { "key2", 123 },
                    { "key3", "dumb&dumber?:;,isit" }
                });

                Assert.Equal(1, cookies.Count);
                Assert.True(cookies[0].HttpOnly);
                Assert.Equal(3, cookies[0].Values.Count);
                Assert.Equal("Say hello to my little friend", cookies[0]["message"]);
                Assert.Equal("123", cookies[0]["key2"]);
                Assert.Equal("dumb&dumber?:;,isit", cookies[0]["key3"]);
            }
            public void WithInitialStateAndNoValuesClearsCookie()
            {
                // Arrange and Setup
                var cookies = new HttpCookieCollection();
                var cookie  = new HttpCookie("__Controller::TempData");

                cookie.HttpOnly = true;
                cookie.Secure   = true;
                cookies.Add(cookie);
                cookie["message"] = "clear";
                var httpContext = new Mock <HttpContextBase>();

                httpContext.Setup(c => c.Request.Cookies).Returns(cookies);
                ITempDataProvider provider = new CookieTempDataProvider(httpContext.Object);
                var controllerContext      = new ControllerContext();

                var tempData = provider.LoadTempData(controllerContext);

                // Validate
                provider.SaveTempData(controllerContext, new Dictionary <string, object>());
                Assert.Single(cookies);
                Assert.True(cookies[0].HttpOnly);
                Assert.True(cookies[0].Secure);
                Assert.Equal("", cookies[0].Value);
            }
        public void CtorSetsHttpContextProperty()
        {
            var httpContext = new Mock <HttpContextBase>().Object;
            var provider    = new CookieTempDataProvider(httpContext);

            Assert.Equal(httpContext, provider.HttpContext);
        }
        public void SaveTempDataStoresSerializedFormInCookie()
        {
            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);

            ITempDataProvider 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);
            HttpCookie cookie     = cookies["__ControllerTempData"];
            string     serialized = cookie.Value;
            IDictionary <string, object> deserializedTempData = CookieTempDataProvider.Base64StringToDictionary(serialized);

            Assert.Equal("Turn it up to 11", deserializedTempData["Testing"]);
            Assert.Equal(1.23, deserializedTempData["Testing2"]);
        }
            public void WithNoValuesDoesNotAddCookie()
            {
                var cookies = new HttpCookieCollection();
                var httpContext = new Mock<HttpContextBase>();
                httpContext.Setup(c => c.Response.Cookies).Returns(cookies);
                ITempDataProvider provider = new CookieTempDataProvider(httpContext.Object);
                var controllerContext = new ControllerContext();

                provider.SaveTempData(controllerContext, new Dictionary<string, object>());

                Assert.Equal(0, cookies.Count);
            }
            public void WithNullCookieReturnsEmptyDictionary()
            {
                var cookies = new HttpCookieCollection();
                var httpContext = new Mock<HttpContextBase>();
                httpContext.Setup(c => c.Request.Cookies).Returns(cookies);
                ITempDataProvider provider = new CookieTempDataProvider(httpContext.Object);
                var controllerContext = new ControllerContext();

                var tempData = provider.LoadTempData(controllerContext);

                Assert.Equal(0, tempData.Count);
            }
            public void WithNullCookieReturnsEmptyDictionary()
            {
                var cookies     = new HttpCookieCollection();
                var httpContext = new Mock <HttpContextBase>();

                httpContext.Setup(c => c.Request.Cookies).Returns(cookies);
                ITempDataProvider provider = new CookieTempDataProvider(httpContext.Object);
                var controllerContext      = new ControllerContext();

                var tempData = provider.LoadTempData(controllerContext);

                Assert.Equal(0, tempData.Count);
            }
            public void WithNoValuesDoesNotAddCookie()
            {
                var cookies     = new HttpCookieCollection();
                var httpContext = new Mock <HttpContextBase>();

                httpContext.Setup(c => c.Response.Cookies).Returns(cookies);
                ITempDataProvider provider = new CookieTempDataProvider(httpContext.Object);
                var controllerContext      = new ControllerContext();

                provider.SaveTempData(controllerContext, new Dictionary <string, object>());

                Assert.Equal(0, cookies.Count);
            }
        public void LoadTempDataWithNullCookieReturnsEmptyTempDataDictionary()
        {
            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);

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);

            IDictionary<string, object> tempData = provider.LoadTempData(null /* controllerContext */);
            Assert.NotNull(tempData);
            Assert.Equal(0, tempData.Count);
        }
Exemplo n.º 14
0
        private ITempDataProvider GetProvider(HttpContext httpContext)
        {
            if (httpContext.Items.ContainsKey("_multitenantCookieTempDataProvider"))
            {
                return(httpContext.Items["_multitenantCookieTempDataProvider"] as ITempDataProvider);
            }

            var options = httpContext.RequestServices.GetRequiredService <IOptions <CookieTempDataProviderOptions> >();

            var cookieTempDataProvider = new CookieTempDataProvider(_dataProtectionProvider, _loggerFactory, options, _tempDataSerializer);

            //.NET Core 2.2
            //var cookieTempDataProvider = new CookieTempDataProvider(_dataProtectionProvider, _loggerFactory, options);

            httpContext.Items["_multitenantCookieTempDataProvider"] = cookieTempDataProvider;

            return(cookieTempDataProvider);
        }
Exemplo n.º 15
0
        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 RetrievesValuesFromCookie()
            {
                var cookies = new HttpCookieCollection();
                var cookie = new HttpCookie("__Controller::TempData");
                cookies.Add(cookie);
                cookie["message"] = "Say hello to my little friend";
                cookie["question"] = "How am I funny?";
                var httpContext = new Mock<HttpContextBase>();
                httpContext.Setup(c => c.Request.Cookies).Returns(cookies);
                ITempDataProvider provider = new CookieTempDataProvider(httpContext.Object);
                var controllerContext = new ControllerContext();

                var tempData = provider.LoadTempData(controllerContext);

                Assert.Equal(2, tempData.Count);
                Assert.Equal("Say hello to my little friend", tempData["message"]);
                Assert.Equal("How am I funny?", tempData["question"]);
                Assert.Equal("How am I funny?", tempData["QUESTION"]);
            }
        public void LoadTempDataWithNullCookieReturnsEmptyTempDataDictionary()
        {
            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);

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);

            IDictionary <string, object> tempData = provider.LoadTempData(null /* controllerContext */);

            Assert.NotNull(tempData);
            Assert.Equal(0, tempData.Count);
        }
            public void RetrievesValuesFromCookie()
            {
                var cookies = new HttpCookieCollection();
                var cookie  = new HttpCookie("__Controller::TempData");

                cookies.Add(cookie);
                cookie["message"]  = "Say hello to my little friend";
                cookie["question"] = "How am I funny?";
                var httpContext = new Mock <HttpContextBase>();

                httpContext.Setup(c => c.Request.Cookies).Returns(cookies);
                ITempDataProvider provider = new CookieTempDataProvider(httpContext.Object);
                var controllerContext      = new ControllerContext();

                var tempData = provider.LoadTempData(controllerContext);

                Assert.Equal(2, tempData.Count);
                Assert.Equal("Say hello to my little friend", tempData["message"]);
                Assert.Equal("How am I funny?", tempData["question"]);
                Assert.Equal("How am I funny?", tempData["QUESTION"]);
            }
            public void StoresValuesInCookie()
            {
                var cookies = new HttpCookieCollection();
                var httpContext = new Mock<HttpContextBase>();
                httpContext.Setup(c => c.Response.Cookies).Returns(cookies);
                ITempDataProvider provider = new CookieTempDataProvider(httpContext.Object);
                var controllerContext = new ControllerContext();

                provider.SaveTempData(controllerContext, new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) { 
                    { "message", "Say hello to my little friend" },
                    { "key2", 123 },
                    { "key3", "dumb&dumber?:;,isit"} 
                });

                Assert.Equal(1, cookies.Count);
                Assert.True(cookies[0].HttpOnly);
                Assert.Equal(3, cookies[0].Values.Count);
                Assert.Equal("Say hello to my little friend", cookies[0]["message"]);
                Assert.Equal("123", cookies[0]["key2"]);
                Assert.Equal("dumb&dumber?:;,isit", cookies[0]["key3"]);
            }
            public void DoesNotThrowWhenKeyValuesFromCookieContainsNullKey()
            {
                var cookies = new HttpCookieCollection();
                var cookie  = new HttpCookie("__Controller::TempData");

                cookie.HttpOnly = true;
                cookies.Add(cookie);
                cookie["message"]  = "Say hello to my little friend";
                cookie["question"] = "How am I funny?";
                cookie[null]       = "This should be ignored.";
                var httpContext = new Mock <HttpContextBase>();

                httpContext.Setup(c => c.Request.Cookies).Returns(cookies);
                ITempDataProvider provider = new CookieTempDataProvider(httpContext.Object);
                var controllerContext      = new ControllerContext();

                var tempData = provider.LoadTempData(controllerContext);

                Assert.Equal(2, tempData.Count);
                Assert.Equal("Say hello to my little friend", tempData["message"]);
                Assert.Equal("How am I funny?", tempData["question"]);
                Assert.Equal("How am I funny?", tempData["QUESTION"]);
            }
Exemplo n.º 21
0
        public void LoadTempDataWithEmptyCookieReturnsEmptyDictionary()
        {
            HttpCookie 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);

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);

            IDictionary <string, object> tempData = provider.LoadTempData(null /* controllerContext */);

            Assert.IsNotNull(tempData);
            Assert.AreEqual(0, tempData.Count);
        }
Exemplo n.º 22
0
        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);
        }
Exemplo n.º 23
0
 protected override void Initialize(RequestContext requestContext)
 {
     TempDataProvider = new CookieTempDataProvider();
     base.Initialize(requestContext);
 }
        public void CanLoadTempDataFromCookie()
        {
            var tempData = new Dictionary<string, object>();
            tempData.Add("abc", "easy as 123");
            tempData.Add("price", 1.234);
            string serializedTempData = CookieTempDataProvider.DictionaryToBase64String(tempData);

            var cookies = new HttpCookieCollection();
            var httpCookie = new HttpCookie("__ControllerTempData");
            httpCookie.Value = serializedTempData;
            cookies.Add(httpCookie);

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

            var responseMock = new Mock<HttpResponseBase>();
            responseMock.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(responseMock.Object);

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);
            IDictionary<string, object> loadedTempData = provider.LoadTempData(null /* controllerContext */);
            Assert.Equal(2, loadedTempData.Count);
            Assert.Equal("easy as 123", loadedTempData["abc"]);
            Assert.Equal(1.234, loadedTempData["price"]);
        }
        public void SaveTempDataStoresSerializedFormInCookie()
        {
            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);

            ITempDataProvider 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);
            HttpCookie cookie = cookies["__ControllerTempData"];
            string serialized = cookie.Value;
            IDictionary<string, object> deserializedTempData = CookieTempDataProvider.Base64StringToDictionary(serialized);
            Assert.Equal("Turn it up to 11", deserializedTempData["Testing"]);
            Assert.Equal(1.23, deserializedTempData["Testing2"]);
        }
        public void LoadTempDataWithNullResponseDoesNotThrowException()
        {
            HttpCookie cookie = new HttpCookie("__ControllerTempData");
            var initialTempData = new Dictionary<string, object>();
            initialTempData.Add("WhatIsInHere?", "Stuff");
            cookie.Value = CookieTempDataProvider.DictionaryToBase64String(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);

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);

            IDictionary<string, object> tempData = provider.LoadTempData(null /* controllerContext */);
            Assert.Equal("Stuff", tempData["WhatIsInHere?"]);
        }