public void TestDeleteCookie() { var cookieStore = new CookieStore(database, "cookie_store_unit_test"); Assert.AreEqual(0, cookieStore.Count); var cookie1 = new Cookie("whitechoco", "sweet", "/", "mycookie.com") { Expires = DateTime.Now.AddSeconds(3600), }; cookieStore.Add(cookie1); var cookie2 = new Cookie("oatmeal_raisin", "sweet", "/", "mycookie.com") { Expires = DateTime.Now.AddSeconds(3600), }; cookieStore.Add(cookie2); var cookie3 = new Cookie("darkchoco", "sweet", "/", "mycookie.com") { Expires = DateTime.Now.AddSeconds(3600), }; cookieStore.Add(cookie3); Assert.AreEqual(3, cookieStore.Count); var cookies = cookieStore.GetCookies(new Uri("http://mycookie.com")); CollectionAssert.Contains(cookies, cookie1); CollectionAssert.Contains(cookies, cookie2); CollectionAssert.Contains(cookies, cookie3); cookieStore.Delete(new Uri("http://mycookie.com"), cookie2.Name); Assert.AreEqual(2, cookieStore.Count); cookies = cookieStore.GetCookies(new Uri("http://mycookie.com")); CollectionAssert.Contains(cookies, cookie1); CollectionAssert.Contains(cookies, cookie3); cookieStore = new CookieStore(database, "cookie_store_unit_test"); Assert.AreEqual(2, cookieStore.Count); cookies = cookieStore.GetCookies(new Uri("http://mycookie.com")); CollectionAssert.Contains(cookies, cookie1); CollectionAssert.Contains(cookies, cookie3); cookieStore.Delete(new Uri("http://mycookie.com"), cookie1.Name); cookieStore.Delete(new Uri("http://mycookie.com"), cookie3.Name); Assert.AreEqual(0, cookieStore.Count); cookieStore = new CookieStore(database, "cookie_store_unit_test"); Assert.AreEqual(0, cookieStore.Count); }
public void TestSetCookiePersistent() { var cookieStore = new CookieStore(database, "cookie_store_unit_test"); Assert.AreEqual(0, cookieStore.Count); var cookie1 = new Cookie("whitechoco", "sweet", "/", "mycookie.com") { Comment = "yummy", CommentUri = new Uri("http://www.mycookie.com"), Expires = DateTime.Now.AddSeconds(60), }; cookieStore.Add(cookie1); var cookie2 = new Cookie("darkchoco", "sweet", "/", "mycookie.com") { Comment = "yummy", CommentUri = new Uri("http://www.mycookie.com"), Expires = DateTime.Now.AddSeconds(60) }; cookieStore.Add(cookie2); var cookies = cookieStore.GetCookies(new Uri("http://mycookie.com")); Assert.AreEqual(2, cookieStore.Count); CollectionAssert.AreEquivalent(new[] { cookie1, cookie2 }, cookies); // Set cookie with same name, domain, and path // with one of the previously set cookies: var cookie3 = new Cookie("darkchoco", "bitter sweet", "/", "mycookie.com") { Comment = "yummy", CommentUri = new Uri("http://www.mycookie.com"), Expires = DateTime.Now.AddSeconds(60) }; cookieStore.Add(cookie3); Assert.AreEqual(2, cookieStore.Count); cookies = cookieStore.GetCookies(new Uri("http://mycookie.com")); CollectionAssert.AreEquivalent(new[] { cookie1, cookie3 }, cookies); cookieStore = new CookieStore(database, "cookie_store_unit_test"); cookies = cookieStore.GetCookies(new Uri("http://mycookie.com")); Assert.AreEqual(2, cookieStore.Count); CollectionAssert.AreEquivalent(new[] { cookie1, cookie3 }, cookies); }
public virtual void DeleteCookie(string name) { // since CookieStore does not have a way to delete an individual cookie, do workaround: // 1. get all cookies // 2. filter list to strip out the one we want to delete // 3. clear cookie store // 4. re-add all cookies except the one we want to delete if (cookieStore == null) { return; } IList <Apache.Http.Cookie.Cookie> cookies = cookieStore.GetCookies(); IList <Apache.Http.Cookie.Cookie> retainedCookies = new AList <Apache.Http.Cookie.Cookie >(); foreach (Apache.Http.Cookie.Cookie cookie in cookies) { if (!cookie.GetName().Equals(name)) { retainedCookies.AddItem(cookie); } } cookieStore.Clear(); foreach (Apache.Http.Cookie.Cookie retainedCookie in retainedCookies) { cookieStore.AddCookie(retainedCookie); } }
public void TestMigration() { var cookie1 = new Cookie { Name = "whitechoco", Domain = ".mycookie.com", Path = "/", Value = "sweet", Expires = DateTime.Now.AddSeconds(3600) }; var cookie2 = new Cookie { Name = "oatmeal_raisin", Domain = ".mycookie.com", Path = "/", Value = "sweet", Expires = DateTime.Now.AddSeconds(3600), Version = 1 }; const string LocalCheckpointCookiesKey = "cbl_cookie_storage_xxxxx"; database.PutLocalCheckpointDoc(LocalCheckpointCookiesKey, new[] { cookie1, cookie2 }); var cookies = database.GetLocalCheckpointDocValue(LocalCheckpointCookiesKey).AsList <Cookie>(); Assert.AreEqual(2, cookies.Count); var cookieStore = new CookieStore(database, null); var newCookies = cookieStore.GetCookies(new Uri("http://mycookie.com")); Assert.AreEqual(2, cookieStore.Count); Assert.AreEqual(cookie1, newCookies[0]); Assert.AreEqual(cookie2, newCookies[1]); Assert.IsNull(database.GetLocalCheckpointDocValue(LocalCheckpointCookiesKey)); }
public void TestSetCookieSessionOnly() { var cookieStore = new CookieStore(database, "cookie_store_unit_test"); Assert.AreEqual(0, cookieStore.Count); // No expires date specified for a cookie v0: var cookie1 = new Cookie("whitechoco", "sweet", "/", "mycookie.com"); Assert.AreEqual(DateTime.MinValue, cookie1.Expires); cookieStore.Add(cookie1); // No max age specified for a cookie v1: var cookie2 = new Cookie("oatmeal_raisin", "sweet", "/", ".mycookie.com") { Version = 1 }; Assert.AreEqual(DateTime.MinValue, cookie2.Expires); cookieStore.Add(cookie2); Assert.AreEqual(2, cookieStore.Count); var cookies = cookieStore.GetCookies(new Uri("http://mycookie.com")); CollectionAssert.AreEquivalent(new[] { cookie1, cookie2 }, cookies); cookieStore = new CookieStore(database, "cookie_store_unit_test"); Assert.AreEqual(0, cookieStore.Count); }
public void TestSetCookieNameDomainPath() { var cookieStore = new CookieStore(database, "cookie_store_unit_test"); Assert.AreEqual(0, cookieStore.Count); var cookie1 = new Cookie("cookie1", "sweet", "/", "mycookie.com"); cookieStore.Add(cookie1); var cookie2 = new Cookie("cookie1", "sweet", "", "mycookie.com"); cookieStore.Add(cookie2); var cookie3 = new Cookie("cookie1", "sweet", "/path", "mycookie.com"); cookieStore.Add(cookie3); var cookie4 = new Cookie("cookie1", "sweet", "/path/", "mycookie.com"); cookieStore.Add(cookie4); var cookie5 = new Cookie("cookie1", "sweet", "/", "www.mycookie.com"); cookieStore.Add(cookie5); var cookie6 = new Cookie("cookie7", "sweet", "/", "www.mycookie.com"); cookieStore.Add(cookie6); Assert.AreEqual(6, cookieStore.Count); var cookies = cookieStore.GetCookies(new Uri("http://www.mycookie.com/path/")); Assert.AreEqual(6, cookies.Count); CollectionAssert.Contains(cookies, cookie1); CollectionAssert.Contains(cookies, cookie2); CollectionAssert.Contains(cookies, cookie3); CollectionAssert.Contains(cookies, cookie4); CollectionAssert.Contains(cookies, cookie5); CollectionAssert.Contains(cookies, cookie6); var cookie8 = new Cookie("cookie1", "bitter. sweet", "/", "mycookie.com"); cookieStore.Add(cookie8); Assert.AreEqual(6, cookieStore.Count); cookies = cookieStore.GetCookies(new Uri("http://www.mycookie.com/path/")); Assert.AreEqual(6, cookies.Count); CollectionAssert.DoesNotContain(cookies, cookie1); CollectionAssert.Contains(cookies, cookie8); }
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); }
public void TestCookieExpires() { var cookieStore = new CookieStore(database, "cookie_store_unit_test"); Assert.AreEqual(0, cookieStore.Count); var cookie1 = new Cookie("whitechoco", "sweet", "/", ".mycookie.com") { Expires = DateTime.Now.AddSeconds(1), Version = 1 }; cookieStore.Add(cookie1); Assert.AreEqual(1, cookieStore.Count); var cookies = cookieStore.GetCookies(new Uri("http://mycookie.com")); Assert.AreEqual(1, cookies.Count); Assert.AreEqual(cookie1, cookies[0]); Sleep(1500); Assert.AreEqual(0, cookieStore.Count); }
/// <exception cref="Apache.Http.HttpException"></exception> /// <exception cref="System.IO.IOException"></exception> public virtual void Process(IHttpRequest request, HttpContext context) { Args.NotNull(request, "HTTP request"); Args.NotNull(context, "HTTP context"); string method = request.GetRequestLine().GetMethod(); if (Sharpen.Runtime.EqualsIgnoreCase(method, "CONNECT")) { return; } HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); // Obtain cookie store CookieStore cookieStore = clientContext.GetCookieStore(); if (cookieStore == null) { this.log.Debug("Cookie store not specified in HTTP context"); return; } // Obtain the registry of cookie specs Lookup <CookieSpecProvider> registry = clientContext.GetCookieSpecRegistry(); if (registry == null) { this.log.Debug("CookieSpec registry not specified in HTTP context"); return; } // Obtain the target host, possibly virtual (required) HttpHost targetHost = clientContext.GetTargetHost(); if (targetHost == null) { this.log.Debug("Target host not set in the context"); return; } // Obtain the route (required) RouteInfo route = clientContext.GetHttpRoute(); if (route == null) { this.log.Debug("Connection route not set in the context"); return; } RequestConfig config = clientContext.GetRequestConfig(); string policy = config.GetCookieSpec(); if (policy == null) { policy = CookieSpecs.BestMatch; } if (this.log.IsDebugEnabled()) { this.log.Debug("CookieSpec selected: " + policy); } URI requestURI = null; if (request is IHttpUriRequest) { requestURI = ((IHttpUriRequest)request).GetURI(); } else { try { requestURI = new URI(request.GetRequestLine().GetUri()); } catch (URISyntaxException) { } } string path = requestURI != null?requestURI.GetPath() : null; string hostName = targetHost.GetHostName(); int port = targetHost.GetPort(); if (port < 0) { port = route.GetTargetHost().GetPort(); } CookieOrigin cookieOrigin = new CookieOrigin(hostName, port >= 0 ? port : 0, !TextUtils .IsEmpty(path) ? path : "/", route.IsSecure()); // Get an instance of the selected cookie policy CookieSpecProvider provider = registry.Lookup(policy); if (provider == null) { throw new HttpException("Unsupported cookie policy: " + policy); } CookieSpec cookieSpec = provider.Create(clientContext); // Get all cookies available in the HTTP state IList <Apache.Http.Cookie.Cookie> cookies = new AList <Apache.Http.Cookie.Cookie>(cookieStore .GetCookies()); // Find cookies matching the given origin IList <Apache.Http.Cookie.Cookie> matchedCookies = new AList <Apache.Http.Cookie.Cookie >(); DateTime now = new DateTime(); foreach (Apache.Http.Cookie.Cookie cookie in cookies) { if (!cookie.IsExpired(now)) { if (cookieSpec.Match(cookie, cookieOrigin)) { if (this.log.IsDebugEnabled()) { this.log.Debug("Cookie " + cookie + " match " + cookieOrigin); } matchedCookies.AddItem(cookie); } } else { if (this.log.IsDebugEnabled()) { this.log.Debug("Cookie " + cookie + " expired"); } } } // Generate Cookie request headers if (!matchedCookies.IsEmpty()) { IList <Header> headers = cookieSpec.FormatCookies(matchedCookies); foreach (Header header in headers) { request.AddHeader(header); } } int ver = cookieSpec.GetVersion(); if (ver > 0) { bool needVersionHeader = false; foreach (Apache.Http.Cookie.Cookie cookie_1 in matchedCookies) { if (ver != cookie_1.GetVersion() || !(cookie_1 is SetCookie2)) { needVersionHeader = true; } } if (needVersionHeader) { Header header = cookieSpec.GetVersionHeader(); if (header != null) { // Advertise cookie version support request.AddHeader(header); } } } // Stick the CookieSpec and CookieOrigin instances to the HTTP context // so they could be obtained by the response interceptor context.SetAttribute(HttpClientContext.CookieSpec, cookieSpec); context.SetAttribute(HttpClientContext.CookieOrigin, cookieOrigin); }