Inheritance: ICloneable
 public void CookieState_Ctor1InitializesCorrectly(string name, string value)
 {
     CookieState cookie = new CookieState(name, value);
     Assert.Equal(name, cookie.Name);
     Assert.Equal(value, cookie.Values.AllKeys[0]);
     Assert.Equal(value, cookie.Value);
 }
        public void CookieHeaderValue_Ctor1_InitializesCorrectly()
        {
            CookieHeaderValue header = new CookieHeaderValue("cookie", "value");
            CookieState       cookie = Assert.Single(header.Cookies);

            Assert.Equal("cookie", cookie.Name);
            Assert.Equal("value", cookie.Values.AllKeys[0]);
        }
        public void CookieState_Ctor1InitializesCorrectly(string name, string value)
        {
            CookieState cookie = new CookieState(name, value);

            Assert.Equal(name, cookie.Name);
            Assert.Equal(value, cookie.Values.AllKeys[0]);
            Assert.Equal(value, cookie.Value);
        }
Beispiel #4
0
        public void CookieHeaderValue_GetItem_CreatesEmptyCookieIfNotAlreadyPresent()
        {
            CookieHeaderValue header = new CookieHeaderValue("cookie", "value");

            CookieState state = header["newstate"];

            Assert.Equal(2, header.Cookies.Count);
            Assert.Equal(String.Empty, state.Value);
        }
Beispiel #5
0
        public void CookieHeaderValue_GetItem_ReturnsCookie()
        {
            CookieHeaderValue header = new CookieHeaderValue("cookie", "value");

            CookieState state = header["cookie"];

            Assert.Equal(1, header.Cookies.Count);
            Assert.Equal("value", state.Value);
        }
        private CookieState(CookieState source)
        {
            Contract.Requires(source != null);

            _name = source._name;
            if (source._values != null)
            {
                Values.Add(source._values);
            }
        }
Beispiel #7
0
        private CookieState(CookieState source)
        {
            Contract.Requires(source != null);

            _name = source._name;
            if (source._values != null)
            {
                Values.Add(source._values);
            }
        }
        public void CookieState_ItemTreatsNullNameAsEmpty()
        {
            // Arrange
            CookieState state = new CookieState("name", "value");

            // Act
            state[null] = "v1";

            // Assert
            Assert.Equal("name=value&=v1", state.ToString());
        }
        public void CookieState_Value()
        {
            CookieState cookie = new CookieState("name");
            Assert.Equal(String.Empty, cookie.Value);

            cookie.Value = "value1";
            Assert.Equal("value1", cookie.Value);

            cookie.Values.AllKeys[0] = "value2";
            Assert.Equal("value2", cookie.Value);
        }
Beispiel #10
0
        public void CookieState_ToStringWithSingleValue()
        {
            // Arrange
            CookieState cookie = new CookieState("name", "value");

            // Act
            string actualValue = cookie.ToString();

            // Assert
            Assert.Equal("name=value", actualValue);
        }
        public void CookieState_Value()
        {
            CookieState cookie = new CookieState("name");

            Assert.Equal(String.Empty, cookie.Value);

            cookie.Value = "value1";
            Assert.Equal("value1", cookie.Value);

            cookie.Values.AllKeys[0] = "value2";
            Assert.Equal("value2", cookie.Value);
        }
        public void CookieHeaderValue_Ctor2_InitializesCorrectly()
        {
            NameValueCollection nvc = new NameValueCollection();

            nvc.Add("name", "value");
            CookieHeaderValue header = new CookieHeaderValue("cookie", nvc);
            CookieState       cookie = Assert.Single(header.Cookies);

            Assert.Equal("cookie", cookie.Name);
            Assert.Equal("name", cookie.Values.AllKeys[0]);
            Assert.Equal("value", cookie.Values["name"]);
        }
        public void CookieState_ItemEncodesValue(string subvalue, string encodedSubvalue)
        {
            // Arrange
            CookieState state = new CookieState("name", "value");

            // Act
            state["n1"] = subvalue;

            // Assert
            string value = String.Format("name=value&n1={0}", encodedSubvalue);

            Assert.Equal(value, state.ToString());
        }
        public void CookieState_ItemEncodesName(string subname, string encodedSubname)
        {
            // Arrange
            CookieState state = new CookieState("name", "value");

            // Act
            state[subname] = "v1";

            // Assert
            string value = String.Format("name=value&{0}=v1", encodedSubname);

            Assert.Equal(value, state.ToString());
        }
        public void CookieState_ToStringWithSingleValue(string subValue, string encodedSubvalue)
        {
            // Arrange
            CookieState cookie = new CookieState("name", subValue);

            // Act
            string actualValue = cookie.ToString();

            // Assert
            string expectedValue = String.Format("name={0}", encodedSubvalue);

            Assert.Equal(expectedValue, actualValue);
        }
Beispiel #16
0
        public void CookieState_ToStringWithNameValueCollection()
        {
            // Arrange
            NameValueCollection nvc = new NameValueCollection();

            nvc.Add("n1", "v1");
            nvc.Add("n2", "v2");
            nvc.Add("n3", "v3");
            CookieState cookie = new CookieState("name", nvc);

            // Act
            string actualValue = cookie.ToString();

            // Assert
            Assert.Equal("name=n1=v1&n2=v2&n3=v3", actualValue);
        }
        public void CookieState_Ctor2InitializesCorrectly()
        {
            // Arrange
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("n1", "v1");

            // Act
            CookieState cookie = new CookieState("name", nvc);

            // Assert
            Assert.Equal("name", cookie.Name);
            Assert.Equal(1, cookie.Values.Count);
            Assert.Equal("n1", cookie.Values.AllKeys[0]);
            Assert.Equal("v1", cookie.Values["n1"]);
            Assert.Equal("n1=v1", cookie.Value);
        }
        public void CookieState_Ctor2InitializesCorrectly()
        {
            // Arrange
            NameValueCollection nvc = new NameValueCollection();

            nvc.Add("n1", "v1");

            // Act
            CookieState cookie = new CookieState("name", nvc);

            // Assert
            Assert.Equal("name", cookie.Name);
            Assert.Equal(1, cookie.Values.Count);
            Assert.Equal("n1", cookie.Values.AllKeys[0]);
            Assert.Equal("v1", cookie.Values["n1"]);
            Assert.Equal("n1", cookie.Value);
        }
        public void CookieState_Clone()
        {
            // Arrange
            NameValueCollection nvc = new NameValueCollection();

            nvc.Add("n1", "v1");
            CookieState expectedValue = new CookieState("name", nvc);

            // Act
            CookieState actualValue = expectedValue.Clone() as CookieState;

            // Assert
            Assert.Equal("name", actualValue.Name);
            Assert.Equal(1, actualValue.Values.Count);
            Assert.Equal("n1", actualValue.Values.AllKeys[0]);
            Assert.Equal("v1", actualValue.Values["n1"]);
        }
        public void CookieState_ToStringWithNameValueCollection(string subValue, string encodedSubvalue)
        {
            // Arrange
            NameValueCollection nvc = new NameValueCollection();

            nvc.Add("n1", subValue);
            nvc.Add("n2", subValue);
            nvc.Add("n3", subValue);
            CookieState cookie = new CookieState("name", nvc);

            // Act
            string actualValue = cookie.ToString();

            // Assert
            string expectedValue = String.Format("name=n1={0}&n2={0}&n3={0}", encodedSubvalue);

            Assert.Equal(expectedValue, actualValue);
        }
Beispiel #21
0
        public CookieState this[string name]
        {
            get
            {
                if (String.IsNullOrEmpty(name))
                {
                    return(null);
                }

                CookieState cookie = Cookies.FirstOrDefault(c => String.Equals(c.Name, name, StringComparison.OrdinalIgnoreCase));
                if (cookie == null)
                {
                    cookie = new CookieState(name, String.Empty);
                    Cookies.Add(cookie);
                }
                return(cookie);
            }
        }
 public CookieHeaderValue(string name, NameValueCollection values)
 {
     CookieState cookie = new CookieState(name, values);
     Cookies.Add(cookie);
 }
        public void CookieState_ItemTreatsNullNameAsEmpty()
        {
            // Arrange
            CookieState state = new CookieState("name", "value");

            // Act
            state[null] = "v1";

            // Assert
            Assert.Equal("name=value&=v1", state.ToString());
        }
        public void CookieState_ToStringWithNameValueCollection()
        {
            // Arrange
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("n1", "v1");
            nvc.Add("n2", "v2");
            nvc.Add("n3", "v3");
            CookieState cookie = new CookieState("name", nvc);

            // Act
            string actualValue = cookie.ToString();

            // Assert
            Assert.Equal("name=n1=v1&n2=v2&n3=v3", actualValue);
        }
        public void CookieState_ToStringWithSingleValue()
        {
            // Arrange
            CookieState cookie = new CookieState("name", "value");

            // Act
            string actualValue = cookie.ToString();

            // Assert
            Assert.Equal("name=value", actualValue);
        }
Beispiel #26
0
        public CookieHeaderValue(string name, NameValueCollection values)
        {
            CookieState cookie = new CookieState(name, values);

            Cookies.Add(cookie);
        }
Beispiel #27
0
        public CookieHeaderValue(string name, string value)
        {
            CookieState cookie = new CookieState(name, value);

            Cookies.Add(cookie);
        }
Beispiel #28
0
        private static bool ParseCookieSegment(CookieHeaderValue instance, string segment)
        {
            if (String.IsNullOrWhiteSpace(segment))
            {
                return(true);
            }

            string[] nameValue = segment.Split(nameValueSeparator, 2);
            if (nameValue.Length < 1 || String.IsNullOrWhiteSpace(nameValue[0]))
            {
                return(false);
            }

            string name = nameValue[0].Trim();

            if (String.Equals(name, ExpiresToken, StringComparison.OrdinalIgnoreCase))
            {
                string         value = GetSegmentValue(nameValue, null);
                DateTimeOffset expires;
                if (FormattingUtilities.TryParseDate(value, out expires))
                {
                    instance.Expires = expires;
                    return(true);
                }
                return(false);
            }
            else if (String.Equals(name, MaxAgeToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                int    maxAge;
                if (FormattingUtilities.TryParseInt32(value, out maxAge))
                {
                    instance.MaxAge = new TimeSpan(0, 0, maxAge);
                    return(true);
                }
                return(false);
            }
            else if (String.Equals(name, DomainToken, StringComparison.OrdinalIgnoreCase))
            {
                instance.Domain = GetSegmentValue(nameValue, null);
                return(true);
            }
            else if (String.Equals(name, PathToken, StringComparison.OrdinalIgnoreCase))
            {
                instance.Path = GetSegmentValue(nameValue, DefaultPath);
                return(true);
            }
            else if (String.Equals(name, SecureToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                if (!String.IsNullOrWhiteSpace(value))
                {
                    return(false);
                }
                instance.Secure = true;
                return(true);
            }
            else if (String.Equals(name, HttpOnlyToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                if (!String.IsNullOrWhiteSpace(value))
                {
                    return(false);
                }
                instance.HttpOnly = true;
                return(true);
            }
            else
            {
                string value = GetSegmentValue(nameValue, null);

                // We read the cookie segment as form data
                try
                {
                    FormDataCollection  formData = new FormDataCollection(value);
                    NameValueCollection values   = formData.ReadAsNameValueCollection();
                    CookieState         cookie   = new CookieState(name, values);
                    instance.Cookies.Add(cookie);
                    return(true);
                }
                catch
                {
                    return(false);
                }
            }
        }
        public CookieState this[string name]
        {
            get
            {
                if (String.IsNullOrEmpty(name))
                {
                    return null;
                }

                CookieState cookie = Cookies.FirstOrDefault(c => String.Equals(c.Name, name, StringComparison.OrdinalIgnoreCase));
                if (cookie == null)
                {
                    cookie = new CookieState(name, String.Empty);
                    Cookies.Add(cookie);
                }
                return cookie;
            }
        }
 public CookieHeaderValue(string name, string value)
 {
     CookieState cookie = new CookieState(name, value);
     Cookies.Add(cookie);
 }
        public void CookieState_ToStringWithNameValueCollection(string subValue, string encodedSubvalue)
        {
            // Arrange
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("n1", subValue);
            nvc.Add("n2", subValue);
            nvc.Add("n3", subValue);
            CookieState cookie = new CookieState("name", nvc);

            // Act
            string actualValue = cookie.ToString();

            // Assert
            string expectedValue = String.Format("name=n1={0}&n2={0}&n3={0}", encodedSubvalue);
            Assert.Equal(expectedValue, actualValue);
        }
        private static bool ParseCookieSegment(CookieHeaderValue instance, string segment)
        {
            if (String.IsNullOrWhiteSpace(segment))
            {
                return true;
            }

            string[] nameValue = segment.Split(nameValueSeparator, 2);
            if (nameValue.Length < 1 || String.IsNullOrWhiteSpace(nameValue[0]))
            {
                return false;
            }

            string name = nameValue[0].Trim();
            if (String.Equals(name, ExpiresToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                DateTimeOffset expires;
                if (FormattingUtilities.TryParseDate(value, out expires))
                {
                    instance.Expires = expires;
                    return true;
                }
                return false;
            }
            else if (String.Equals(name, MaxAgeToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                int maxAge;
                if (FormattingUtilities.TryParseInt32(value, out maxAge))
                {
                    instance.MaxAge = new TimeSpan(0, 0, maxAge);
                    return true;
                }
                return false;
            }
            else if (String.Equals(name, DomainToken, StringComparison.OrdinalIgnoreCase))
            {
                instance.Domain = GetSegmentValue(nameValue, null);
                return true;
            }
            else if (String.Equals(name, PathToken, StringComparison.OrdinalIgnoreCase))
            {
                instance.Path = GetSegmentValue(nameValue, DefaultPath);
                return true;
            }
            else if (String.Equals(name, SecureToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                if (!String.IsNullOrWhiteSpace(value))
                {
                    return false;
                }
                instance.Secure = true;
                return true;
            }
            else if (String.Equals(name, HttpOnlyToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                if (!String.IsNullOrWhiteSpace(value))
                {
                    return false;
                }
                instance.HttpOnly = true;
                return true;
            }
            else
            {
                string value = GetSegmentValue(nameValue, null);

                // We read the cookie segment as form data
                try
                {
                    FormDataCollection formData = new FormDataCollection(value);
                    NameValueCollection values = formData.ReadAsNameValueCollection();
                    CookieState cookie = new CookieState(name, values);
                    instance.Cookies.Add(cookie);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
Beispiel #33
0
 public bool ValidateCookieTicket(CookieState cookie, out CookieTicket ticket, out CookieHeaderValue renewCookie)
 {
     ticket = new CookieTicket(_userId.ToString());
     renewCookie = null;
     return true;
 }
        public void CookieState_ItemEncodesName(string subname, string encodedSubname)
        {
            // Arrange
            CookieState state = new CookieState("name", "value");

            // Act
            state[subname] = "v1";

            // Assert
            string value = String.Format("name=value&{0}=v1", encodedSubname);
            Assert.Equal(value, state.ToString());
        }
        public void CookieState_ItemEncodesValue(string subvalue, string encodedSubvalue)
        {
            // Arrange
            CookieState state = new CookieState("name", "value");

            // Act
            state["n1"] = subvalue;

            // Assert
            string value = String.Format("name=value&n1={0}", encodedSubvalue);
            Assert.Equal(value, state.ToString());
        }
        public void CookieState_ToStringWithSingleValue(string subValue, string encodedSubvalue)
        {
            // Arrange
            CookieState cookie = new CookieState("name", subValue);

            // Act
            string actualValue = cookie.ToString();

            // Assert
            string expectedValue = String.Format("name={0}", encodedSubvalue);
            Assert.Equal(expectedValue, actualValue);
        }
        public void CookieState_Clone()
        {
            // Arrange
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("n1", "v1");
            CookieState expectedValue = new CookieState("name", nvc);

            // Act
            CookieState actualValue = expectedValue.Clone() as CookieState;

            // Assert
            Assert.Equal("name", actualValue.Name);
            Assert.Equal(1, actualValue.Values.Count);
            Assert.Equal("n1", actualValue.Values.AllKeys[0]);
            Assert.Equal("v1", actualValue.Values["n1"]);
        }
        private static bool ParseCookieSegment(CookieHeaderValue instance, string segment)
        {
            var nameValueSeparator = new char[] { '=' };

            if (string.IsNullOrWhiteSpace(segment))
            {
                return true;
            }
            string[] nameValuePair = segment.Split(nameValueSeparator, 2);
            if ((nameValuePair.Length < 1) || string.IsNullOrWhiteSpace(nameValuePair[0]))
            {
                return false;
            }
            string a = nameValuePair[0].Trim();
            if (string.Equals(a, "expires", StringComparison.OrdinalIgnoreCase))
            {
                DateTimeOffset offset;
                if (TryParseDate(GetSegmentValue(nameValuePair, null), out offset))
                {
                    instance.Expires = new DateTimeOffset?(offset);
                    return true;
                }
                return false;
            }
            if (string.Equals(a, "max-age", StringComparison.OrdinalIgnoreCase))
            {
                int num;
                if (TryParseInt32(GetSegmentValue(nameValuePair, null), out num))
                {
                    instance.MaxAge = new TimeSpan(0, 0, num);
                    return true;
                }
                return false;
            }
            if (string.Equals(a, "domain", StringComparison.OrdinalIgnoreCase))
            {
                instance.Domain = GetSegmentValue(nameValuePair, null);
                return true;
            }
            if (string.Equals(a, "path", StringComparison.OrdinalIgnoreCase))
            {
                instance.Path = GetSegmentValue(nameValuePair, "/");
                return true;
            }
            if (string.Equals(a, "secure", StringComparison.OrdinalIgnoreCase))
            {
                if (!string.IsNullOrWhiteSpace(GetSegmentValue(nameValuePair, null)))
                {
                    return false;
                }
                instance.Secure = true;
                return true;
            }
            if (string.Equals(a, "httponly", StringComparison.OrdinalIgnoreCase))
            {
                if (!string.IsNullOrWhiteSpace(GetSegmentValue(nameValuePair, null)))
                {
                    return false;
                }
                instance.HttpOnly = true;
                return true;
            }
            string segmentValue = GetSegmentValue(nameValuePair, null);
            try
            {
                NameValueCollection values = new FormDataCollection(segmentValue).ReadAsNameValueCollection();
                CookieState item = new CookieState(a, values);
                instance.Cookies.Add(item);
                return true;
            }
            catch
            {
                return false;
            }
        }