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})");
        }
Exemple #2
0
        public void ParseWithSingleDigitTime()
        {
            Assert.Equal(_expectedTime, DateFormatter.ParseHttpDate("Sunday, 06 Nov 1994 8:49:37 GMT"));

            DateTime time080937 = _expectedTime - TimeSpan.FromMilliseconds(40 * 60 * 1000);

            Assert.Equal(time080937, DateFormatter.ParseHttpDate("Sunday, 06 Nov 1994 8:9:37 GMT"));
            Assert.Equal(time080937, DateFormatter.ParseHttpDate("Sunday, 06 Nov 1994 8:09:37 GMT"));

            DateTime time080907 = _expectedTime - TimeSpan.FromMilliseconds((40 * 60 + 30) * 1000);

            Assert.Equal(time080907, DateFormatter.ParseHttpDate("Sunday, 06 Nov 1994 8:9:7 GMT"));
            Assert.Equal(time080907, DateFormatter.ParseHttpDate("Sunday, 06 Nov 1994 8:9:07 GMT"));
        }
Exemple #3
0
 long MergeMaxAgeAndExpires()
 {
     // max age has precedence over expires
     if (_maxAge != long.MinValue)
     {
         return(_maxAge);
     }
     else if (IsValueDefined(_expiresStart, _expiresEnd))
     {
         DateTime?expiresDate = DateFormatter.ParseHttpDate(_header, _expiresStart, _expiresEnd);
         if (expiresDate is object)
         {
             return((expiresDate.Value.Ticks - DateTime.UtcNow.Ticks) / TimeSpan.TicksPerSecond);
         }
     }
     return(long.MinValue);
 }
Exemple #4
0
 public void ParseInvalidInput()
 {
     // missing field
     Assert.Null(DateFormatter.ParseHttpDate("Sun, Nov 1994 08:49:37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 1994 08:49:37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 Nov 08:49:37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 Nov 1994 :49:37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 Nov 1994 49:37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 Nov 1994 08::37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 Nov 1994 08:37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 Nov 1994 08:49: GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 Nov 1994 08:49 GMT"));
     //invalid value
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 FOO 1994 08:49:37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 36 Nov 1994 08:49:37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 Nov 1994 28:49:37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 Nov 1994 08:69:37 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sun, 06 Nov 1994 08:49:67 GMT"));
     //wrong number of digits in timestamp
     Assert.Null(DateFormatter.ParseHttpDate("Sunday, 06 Nov 1994 0:0:000 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sunday, 06 Nov 1994 0:000:0 GMT"));
     Assert.Null(DateFormatter.ParseHttpDate("Sunday, 06 Nov 1994 000:0:0 GMT"));
 }
 public DateTime?ParseHttpHeaderDateFormatter() => DateFormatter.ParseHttpDate(DateString);
Exemple #6
0
 public void ParseMidnight()
 {
     Assert.Equal(new DateTime(1994, 11, 6, 0, 0, 0, DateTimeKind.Utc), DateFormatter.ParseHttpDate("Sunday, 06 Nov 1994 00:00:00 GMT"));
 }
Exemple #7
0
 public void ParseWithSingleDigitHourMinutesAndSecond()
 {
     Assert.Equal(_expectedTime, DateFormatter.ParseHttpDate("Sunday, 06-Nov-94 8:49:37 GMT"));
 }
Exemple #8
0
 public void ParseWithFunkyTimezone()
 {
     Assert.Equal(_expectedTime, DateFormatter.ParseHttpDate("Sun Nov 06 08:49:37 1994 -0000"));
 }
Exemple #9
0
 public void ParseWithoutGmt()
 {
     Assert.Equal(_expectedTime, DateFormatter.ParseHttpDate("Sun Nov 06 08:49:37 1994"));
 }
Exemple #10
0
 public void ParseWithDashSeparatorDoubleDigitDay()
 {
     Assert.Equal(_expectedTime, DateFormatter.ParseHttpDate("Sunday, 06-Nov-94 08:49:37 GMT"));
 }
Exemple #11
0
 public void ParseWithDoubleDigitDay()
 {
     Assert.Equal(_expectedTime, DateFormatter.ParseHttpDate("Sun, 06 Nov 1994 08:49:37 GMT"));
 }
Exemple #12
0
 public void ParseWithSingleDoubleDigitDay()
 {
     Assert.Equal(this.expectedTime, DateFormatter.ParseHttpDate("Sunday, 6-Nov-94 08:49:37 GMT"));
 }