public void TestSaveEmptyCookieStore()
        {
            var cookieStore = new CookieStore(GetCookiesDirectory().FullName);

            Assert.AreEqual(0, cookieStore.Count);
            cookieStore.Save();

            cookieStore = new CookieStore(GetCookiesDirectory().FullName);
            Assert.AreEqual(0, cookieStore.Count);
        }
Esempio n. 2
0
        public void TestSaveEmptyCookieStore()
        {
            var cookieStore = new CookieStore(database, "cookie_store_unit_test");

            Assert.AreEqual(0, cookieStore.Count);
            cookieStore.Save();

            cookieStore = new CookieStore(database, "cookie_store_unit_test");
            Assert.AreEqual(0, cookieStore.Count);
        }
        protected override HttpResponseMessage ProcessResponse(HttpResponseMessage response, CancellationToken cancellationToken)
        {
            int retryCount;

            do
            {
                if (Authenticator != null && response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    retryCount = _retryMessages.GetOrAdd(response, 0);
                    if (retryCount >= 5)
                    {
                        // Multiple concurrent requests means that the Nc can sometimes get out of order
                        // so try again, but within reason.
                        break;
                    }

                    _retryMessages.TryUpdate(response, retryCount + 1, retryCount);
                    var newRequest = new HttpRequestMessage(response.RequestMessage.Method, response.RequestMessage.RequestUri);
                    foreach (var header in response.RequestMessage.Headers)
                    {
                        if (header.Key != "Authorization")
                        {
                            newRequest.Headers.Add(header.Key, header.Value);
                        }
                    }

                    newRequest.Content = response.RequestMessage.Content;
                    var challengeResponse = Authenticator.ResponseFromChallenge(response);
                    if (challengeResponse != null)
                    {
                        newRequest.Headers.Add("Authorization", challengeResponse);
                        return(ProcessResponse(SendAsync(newRequest, cancellationToken).Result, cancellationToken));
                    }
                }
            }  while(false);

            var hasSetCookie = response.Headers.Contains("Set-Cookie");

            if (hasSetCookie)
            {
                var cookie = default(Cookie);
                if (CookieParser.TryParse(response.Headers.GetValues("Set-Cookie").ElementAt(0), response.RequestMessage.RequestUri.Host,
                                          out cookie))
                {
                    lock (_locker) {
                        _cookieStore.Add(cookie);
                        _cookieStore.Save();
                    }
                }
            }

            _retryMessages.TryRemove(response, out retryCount);
            return(response);
        }
        public void TestSaveCookieStore()
        {
            var cookieStore = new CookieStore(GetCookiesDirectory().FullName);

            Assert.AreEqual(0, cookieStore.Count);

            var name     = "foo";
            var value    = "bar";
            var uri      = new Uri("http://foo.com/baz");
            var domain   = uri.Host;
            var path     = uri.PathAndQuery;
            var httpOnly = false;
            var isSecure = false;
            var expires  = DateTime.Now.Add(TimeSpan.FromDays(1));

            var cookie = new Cookie(name, value);

            cookie.Path     = path;
            cookie.Domain   = domain;
            cookie.HttpOnly = httpOnly;
            cookie.Secure   = isSecure;
            cookie.Expires  = expires;

            cookieStore.Add(cookie);
            cookieStore.Save();

            cookieStore = new CookieStore(GetCookiesDirectory().FullName);
            Assert.AreEqual(1, cookieStore.Count);

            var cookies = cookieStore.GetCookies(uri);

            Assert.AreEqual(1, cookies.Count);
            Assert.AreEqual(name, cookies[0].Name);
            Assert.AreEqual(value, cookies[0].Value);
            Assert.AreEqual(path, cookies[0].Path);
            Assert.AreEqual(domain, cookies[0].Domain);
            Assert.AreEqual(expires, cookies[0].Expires);
        }