Exemplo n.º 1
0
        public void LoadTempDataWithNullCookieReturnsEmptyTempDataDictionary() {
            var cookies = new HttpCookieCollection();

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

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Expect(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);
        }
        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.º 3
0
        public void LoadTempDataWithNullResponseDoesNotThrowException() {
            HttpCookie cookie = new HttpCookie("__ControllerTempData");
            TempDataDictionary initialTempData = new TempDataDictionary();
            initialTempData.Add("WhatIsInHere?", "Stuff");
            cookie.Value = CookieTempDataProvider.SerializeToBase64EncodedString(initialTempData);
            var cookies = new HttpCookieCollection();
            cookies.Add(cookie);

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

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

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);

            IDictionary<string, object> tempData = provider.LoadTempData(null /* controllerContext */);
            Assert.AreEqual("Stuff", tempData["WhatIsInHere?"]);
        }
Exemplo n.º 4
0
        public void CanLoadTempDataFromCookie() {
            var tempData = new TempDataDictionary();
            tempData.Add("abc", "easy as 123");
            tempData.Add("price", 1.234);
            string serializedTempData = CookieTempDataProvider.SerializeToBase64EncodedString(tempData);
            
            var cookies = new HttpCookieCollection();
            var httpCookie = new HttpCookie("__ControllerTempData");
            httpCookie.Value = serializedTempData;
            cookies.Add(httpCookie);

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

            var responseMock = new Mock<HttpResponseBase>();
            responseMock.Expect(r => r.Cookies).Returns(cookies);

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

            ITempDataProvider provider = new CookieTempDataProvider(httpContextMock.Object);
            IDictionary<string, object> loadedTempData = provider.LoadTempData(null /* controllerContext */);
            Assert.AreEqual(2, loadedTempData.Count);
            Assert.AreEqual("easy as 123", loadedTempData["abc"]);
            Assert.AreEqual(1.234, loadedTempData["price"]);
        }