Ejemplo n.º 1
0
        public void EncodingMultipleClientCookies()
        {
            const string C1      = "myCookie=myValue";
            const string C2      = "myCookie2=myValue2";
            const string C3      = "myCookie3=myValue3";
            ICookie      cookie1 = new DefaultCookie("myCookie", "myValue")
            {
                Domain   = ".adomainsomewhere",
                MaxAge   = 50,
                Path     = "/apathsomewhere",
                IsSecure = true
            };
            ICookie cookie2 = new DefaultCookie("myCookie2", "myValue2")
            {
                Domain   = ".anotherdomainsomewhere",
                Path     = "/anotherpathsomewhere",
                IsSecure = false
            };
            ICookie cookie3       = new DefaultCookie("myCookie3", "myValue3");
            string  encodedCookie = ClientCookieEncoder.StrictEncoder.Encode(cookie1, cookie2, cookie3);

            // Cookies should be sorted into decreasing order of path length, as per RFC6265.
            // When no path is provided, we assume maximum path length (so cookie3 comes first).
            Assert.Equal(C3 + "; " + C2 + "; " + C1, encodedCookie);
        }
        public void EncodingSingleCookieV0()
        {
            const int MaxAge = 50;
            const string Result = "myCookie=myValue; Max-Age=50; Expires=(.+?); Path=/apathsomewhere; Domain=.adomainsomewhere; Secure";

            var cookie = new DefaultCookie("myCookie", "myValue")
            {
                Domain = ".adomainsomewhere",
                MaxAge = MaxAge,
                Path = "/apathsomewhere",
                IsSecure = true
            };
            string encodedCookie = ServerCookieEncoder.StrictEncoder.Encode(cookie);

            var regex = new Regex(Result, RegexOptions.Compiled);
            MatchCollection matches = regex.Matches(encodedCookie);
            Assert.Single(matches);

            Match match = matches[0];
            Assert.NotNull(match);
             
            DateTime? expiresDate = DateFormatter.ParseHttpDate(match.Groups[1].Value);
            Assert.True(expiresDate.HasValue, $"Parse http date failed : {match.Groups[1].Value}");
            long diff = (expiresDate.Value.Ticks - DateTime.UtcNow.Ticks) / TimeSpan.TicksPerSecond;
            // 2 secs should be fine
            Assert.True(Math.Abs(diff - MaxAge) <= 2, $"Expecting difference of MaxAge < 2s, but was {diff}s (MaxAge={MaxAge})");
        }
Ejemplo n.º 3
0
        public void ComparatorForSamePathLength()
        {
            ICookie cookie = new DefaultCookie("test", "value");

            cookie.Path = "1";

            ICookie cookie2 = new DefaultCookie("test", "value");

            cookie2.Path = "2";

            Assert.Equal(0, ClientCookieEncoder.Comparer.Compare(cookie, cookie2));
            Assert.Equal(0, ClientCookieEncoder.Comparer.Compare(cookie2, cookie));
        }
        public void EncodingMultipleCookiesStrict()
        {
            var result = new List<string>
            {
                "cookie2=value2",
                "cookie1=value3"
            };
            ICookie cookie1 = new DefaultCookie("cookie1", "value1");
            ICookie cookie2 = new DefaultCookie("cookie2", "value2");
            ICookie cookie3 = new DefaultCookie("cookie1", "value3");

            IList<string> encodedCookies = ServerCookieEncoder.StrictEncoder.Encode(cookie1, cookie2, cookie3);
            Assert.Equal(result, encodedCookies);
        }