// Authors:
// Lawrence Pit (loz@cable.a2000.nl)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
+// Sebastien Pouliot <sebastien@ximian.com>
//
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
// (c) Copyright 2004 Ximian, Inc. (http://www.ximian.com)
-//
+// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
namespace System.Net
{
[Serializable]
- [MonoTODO ("Need to remove older/unused cookies if it reaches the maximum capacity")]
#if NET_2_1
public sealed class CookieContainer {
#else
public const int DefaultCookieLimit = 300;
public const int DefaultPerDomainCookieLimit = 20;
- int count;
int capacity = DefaultCookieLimit;
int perDomainCapacity = DefaultPerDomainCookieLimit;
int maxCookieSize = DefaultCookieLengthLimit;
// properties
public int Count {
- get { return count; }
+ get { return (cookies == null) ? 0 : cookies.Count; }
}
public int Capacity {
if (cookie == null)
throw new ArgumentNullException ("cookie");
- if (cookie.Domain == "")
+ if (cookie.Domain.Length == 0)
#if NET_2_0
throw new ArgumentException ("Cookie domain not set.", "cookie.Domain");
#else
if (cookies == null)
cookies = new CookieCollection ();
- if (count + 1 > capacity)
- throw new CookieException ("Capacity exceeded");
+ if (cookies.Count >= capacity)
+ RemoveOldest (null);
+
+ // try to avoid counting per-domain
+ if (cookies.Count >= perDomainCapacity) {
+ if (CountDomain (cookie.Domain) >= perDomainCapacity)
+ RemoveOldest (cookie.Domain);
+ }
- cookies.Add (cookie);
- count = cookies.Count;
+ // clone the important parts of the cookie
+ Cookie c = new Cookie (cookie.Name, cookie.Value);
+ c.Path = (cookie.Path.Length == 0) ? "/" : cookie.Path;
+ c.Domain = cookie.Domain;
+ c.ExactDomain = cookie.ExactDomain;
+ c.Version = cookie.Version;
+
+ cookies.Add (c);
CheckExpiration ();
}
+ int CountDomain (string domain)
+ {
+ int count = 0;
+ foreach (Cookie c in cookies) {
+ if (CheckDomain (domain, c.Domain, true))
+ count++;
+ }
+ return count;
+ }
+
+ void RemoveOldest (string domain)
+ {
+ int n = 0;
+ DateTime oldest = DateTime.MaxValue;
+ for (int i = 0; i < cookies.Count; i++) {
+ Cookie c = cookies [i];
+ if ((c.TimeStamp < oldest) && ((domain == null) || (domain == c.Domain))) {
+ oldest = c.TimeStamp;
+ n = i;
+ }
+ }
+ cookies.List.RemoveAt (n);
+ }
+
// Only needs to be called from AddCookie (Cookie) and GetCookies (Uri)
void CheckExpiration ()
{
if (cookies == null)
return;
- ArrayList removed = null;
for (int i = cookies.Count - 1; i >= 0; i--) {
Cookie cookie = cookies [i];
- if (cookie.Expired) {
- if (removed == null)
- removed = new ArrayList ();
- removed.Add (i);
- }
- }
-
- if (removed != null) {
- // We went backwards above, so this works.
- ArrayList list = cookies.List;
- foreach (int n in removed) {
- list.RemoveAt (n);
- }
+ if (cookie.Expired)
+ cookies.List.RemoveAt (i);
}
}
void Cook (Uri uri, Cookie cookie)
{
- if (cookie.Name == null || cookie.Name == "")
+ if (String.IsNullOrEmpty (cookie.Name))
throw new CookieException ("Invalid cookie: name");
if (cookie.Value == null)
throw new CookieException ("Invalid cookie: value");
- if (uri != null && cookie.Domain == "")
+ if (uri != null && cookie.Domain.Length == 0)
cookie.Domain = uri.Host;
- if (cookie.Version == 0 && (cookie.Path == null || cookie.Path == "")) {
+ if (cookie.Version == 0 && String.IsNullOrEmpty (cookie.Path)) {
if (uri != null) {
cookie.Path = uri.AbsolutePath;
} else {
}
}
- if (cookie.Port == "" && uri != null && !uri.IsDefaultPort) {
+ if (cookie.Port.Length == 0 && uri != null && !uri.IsDefaultPort) {
cookie.Port = "\"" + uri.Port.ToString () + "\"";
}
}
if (cookie == null)
throw new ArgumentNullException ("cookie");
- Cook (uri, cookie);
- AddCookie (cookie);
+ if (!cookie.Expired) {
+ Cook (uri, cookie);
+ AddCookie (cookie);
+ }
}
public void Add (Uri uri, CookieCollection cookies)
if (cookies == null)
throw new ArgumentNullException ("cookies");
- foreach (Cookie c in cookies) {
- Cook (uri, c);
- AddCookie (c);
+ foreach (Cookie cookie in cookies) {
+ if (!cookie.Expired) {
+ Cook (uri, cookie);
+ AddCookie (cookie);
+ }
}
}
StringBuilder result = new StringBuilder ();
foreach (Cookie cookie in coll) {
- result.Append (cookie.ToString ());
+ // don't include the domain since it can be infered from the URI
+ // include empty path as '/'
+ result.Append (cookie.ToString (uri));
result.Append ("; ");
}
return result.ToString ();
}
- static bool CheckDomain (string domain, string host)
+ static bool CheckDomain (string domain, string host, bool exact)
{
- if (domain == String.Empty)
- return false;
-
- int hlen = host.Length;
- int dlen = domain.Length;
- if (hlen < dlen)
+ if (domain.Length == 0)
return false;
- if (hlen == dlen)
- return (String.Compare (domain, host, true, CultureInfo.InvariantCulture) == 0);
+ if (exact)
+ return (String.Compare (host, domain, StringComparison.InvariantCultureIgnoreCase) == 0);
- if (domain [0] != '.') {
- domain = "." + domain;
- dlen++;
- }
-
- string subdomain = host.Substring (hlen - dlen);
- return (String.Compare (subdomain, domain, true, CultureInfo.InvariantCulture) == 0);
+ // check for allowed sub-domains - without string allocations
+ if (!host.EndsWith (domain, StringComparison.InvariantCultureIgnoreCase))
+ return false;
+ // mono.com -> www.mono.com is OK but supermono.com NOT OK
+ if (domain [0] == '.')
+ return true;
+ int p = host.Length - domain.Length - 1;
+ if (p < 0)
+ return false;
+ return (host [p] == '.');
}
public CookieCollection GetCookies (Uri uri)
foreach (Cookie cookie in cookies) {
string domain = cookie.Domain;
- if (!CheckDomain (domain, uri.Host))
+ if (!CheckDomain (domain, uri.Host, cookie.ExactDomain))
continue;
- if (cookie.Port != "" && cookie.Ports != null && uri.Port != -1) {
+ if (cookie.Port.Length > 0 && cookie.Ports != null && uri.Port != -1) {
if (Array.IndexOf (cookie.Ports, uri.Port) == -1)
continue;
}
coll.Add (cookie);
}
- coll.SortByPath ();
+ coll.Sort ();
return coll;
}
throw new ArgumentNullException ("uri");
if (cookieHeader == null)
- throw new ArgumentNullException ("cookieHeader");
+ throw new ArgumentNullException ("cookieHeader");
- ParseAndAddCookies (uri, cookieHeader);
- }
-
- // GetCookieValue, GetCookieName and ParseAndAddCookies copied from HttpRequest.cs
- static string GetCookieValue (string str, int length, ref int i)
- {
- if (i >= length)
- return null;
-
- int k = i;
- while (k < length && Char.IsWhiteSpace (str [k]))
- k++;
-
- int begin = k;
- while (k < length && str [k] != ';')
- k++;
-
- i = k;
- return str.Substring (begin, i - begin).Trim ();
- }
-
- static string GetCookieName (string str, int length, ref int i)
- {
- if (i >= length)
- return null;
-
- int k = i;
- while (k < length && Char.IsWhiteSpace (str [k]))
- k++;
+ if (cookieHeader.Length == 0)
+ return;
+
+ // Cookies must be separated by ',' (like documented on MSDN)
+ string [] jar = cookieHeader.Split (',');
+ foreach (string cookie in jar) {
+ try {
+ Cookie c = Parse (cookie);
+
+ // add default values from URI if missing from the string
+ if (c.Path.Length == 0) {
+ c.Path = uri.AbsolutePath;
+ } else if (!uri.AbsolutePath.StartsWith (c.Path)) {
+ string msg = String.Format ("'Path'='{0}' is invalid with URI", c.Path);
+ throw new CookieException (msg);
+ }
- int begin = k;
- while (k < length && str [k] != ';' && str [k] != '=')
- k++;
+ if (c.Domain.Length == 0) {
+ c.Domain = uri.Host;
+ // don't consider domain "a.b.com" as ".a.b.com"
+ c.ExactDomain = true;
+ }
- i = k + 1;
- return str.Substring (begin, k - begin).Trim ();
+ Add (c);
+ }
+ catch (Exception e) {
+ string msg = String.Format ("Could not parse cookies for '{0}'.", uri);
+ throw new CookieException (msg, e);
+ }
+ }
}
- static string GetDir (string path)
+ static Cookie Parse (string s)
{
- if (path == null || path == "")
- return "/";
-
- int last = path.LastIndexOf ('/');
- if (last == -1)
- return "/" + path;
-
- return path.Substring (0, last + 1);
- }
-
- void ParseAndAddCookies (Uri uri, string header)
- {
- if (header.Length == 0)
- return;
+ string [] parts = s.Split (';');
+ Cookie c = new Cookie ();
+ for (int i = 0; i < parts.Length; i++) {
+ string key, value;
+ int sep = parts[i].IndexOf ('=');
+ if (sep == -1) {
+ key = parts [i].Trim ();
+ value = String.Empty;
+ } else {
+ key = parts [i].Substring (0, sep).Trim ();
+ value = parts [i].Substring (sep + 1).Trim ();
+ }
- string [] name_values = header.Trim ().Split (';');
- int length = name_values.Length;
- Cookie cookie = null;
- int pos;
- CultureInfo inv = CultureInfo.InvariantCulture;
- bool havePath = false;
- bool haveDomain = false;
-
- for (int i = 0; i < length; i++) {
- pos = 0;
- string name_value = name_values [i].Trim ();
- string name = GetCookieName (name_value, name_value.Length, ref pos);
- if (name == null || name == "")
- throw new CookieException ("Name is empty.");
-
- string value = GetCookieValue (name_value, name_value.Length, ref pos);
- if (cookie != null) {
- if (!havePath && String.Compare (name, "$Path", true, inv) == 0 ||
- String.Compare (name, "path", true, inv) == 0) {
- havePath = true;
- cookie.Path = value;
- continue;
+ switch (key.ToLowerInvariant ()) {
+ case "path":
+ case "$path":
+ if (c.Path.Length == 0)
+ c.Path = value;
+ break;
+ case "domain":
+ case "$domain":
+ if (c.Domain.Length == 0) {
+ c.Domain = value;
+ // here mono.com means "*.mono.com"
+ c.ExactDomain = false;
}
-
- if (!haveDomain && String.Compare (name, "$Domain", true, inv) == 0 ||
- String.Compare (name, "domain", true, inv) == 0) {
- cookie.Domain = value;
- haveDomain = true;
- continue;
+ break;
+ default:
+ if (c.Name.Length == 0) {
+ c.Name = key;
+ c.Value = value;
}
-
- if (!havePath)
- cookie.Path = GetDir (uri.AbsolutePath);
-
- if (!haveDomain)
- cookie.Domain = uri.Host;
-
- havePath = false;
- haveDomain = false;
- Add (cookie);
- cookie = null;
+ break;
}
- cookie = new Cookie (name, value);
- }
-
- if (cookie != null) {
- if (!havePath)
- cookie.Path = GetDir (uri.AbsolutePath);
-
- if (!haveDomain)
- cookie.Domain = uri.Host;
-
- Add (cookie);
}
+ return c;
}
-
- } // CookieContainer
-
-} // System.Net
+ }
+}
// Authors:
// Gonzalo Paniagua Javier (gonzalo@novell.com)
// Daniel Nauck (dna(at)mono-project(dot)de)
+// Sebastien Pouliot <sebastien@ximian.com>
//
-// (c) Copyright 2004 Novell, Inc. (http://www.novell.com)
+// Copyright (C) 2004,2009 Novell, Inc (http://www.novell.com)
//
using System;
using NUnit.Framework;
-namespace MonoTests.System.Net
-{
+namespace MonoTests.System.Net {
[TestFixture]
- public class CookieContainerTest
- {
+ public class CookieContainerTest {
[Test] // .ctor ()
public void Constructor1 ()
{
try {
new CookieContainer (0);
Assert.Fail ("#A1");
- } catch (ArgumentException ex) {
+ }
+ catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
#if NET_2_0
try {
new CookieContainer (-10);
Assert.Fail ("#B1");
- } catch (ArgumentException ex) {
+ }
+ catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
#if NET_2_0
public void Constructor3 ()
{
CookieContainer c;
-
+
c = new CookieContainer (100, 50, 1000);
Assert.AreEqual (100, c.Capacity, "#A1");
Assert.AreEqual (50, c.PerDomainCapacity, "#A2");
try {
new CookieContainer (0, 0, 100);
Assert.Fail ("#A1");
- } catch (ArgumentException ex) {
+ }
+ catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
#if NET_2_0
try {
new CookieContainer (-10, 0, 100);
Assert.Fail ("#B1");
- } catch (ArgumentException ex) {
+ }
+ catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
#if NET_2_0
try {
new CookieContainer (100, 50, 0);
Assert.Fail ("#A1");
- } catch (ArgumentException ex) {
+ }
+ catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
#if NET_2_0
try {
new CookieContainer (100, 50, -4);
Assert.Fail ("#B1");
- } catch (ArgumentException ex) {
+ }
+ catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
#if NET_2_0
new CookieContainer (432, 0, 1000);
Assert.Fail ("#B1");
#if NET_2_0
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
// 'PerDomainCapacity' has to be greater than
// '0' and less than '432'
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
new CookieContainer (432, -1, 1000);
Assert.Fail ("#C1");
#if NET_2_0
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
// 'PerDomainCapacity' has to be greater than
// '0' and less than '432'
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
new CookieContainer (432, 433, 1000);
Assert.Fail ("#C1");
#if NET_2_0
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
// 'PerDomainCapacity' has to be greater than
// '0' and less than '432'
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
try {
c.Capacity = -5;
Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
// 'Capacity' has to be greater than '0' and
// less than '20'
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
try {
c.Capacity = 0;
Assert.Fail ("#B1");
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
// 'Capacity' has to be greater than '0' and
// less than '20'
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
try {
c.Capacity = 5;
Assert.Fail ("#C1");
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
// 'Capacity' has to be greater than '0' and
// less than '20'
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
try {
c.MaxCookieSize = -5;
Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
try {
c.MaxCookieSize = -1;
Assert.Fail ("#B1");
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
try {
c.MaxCookieSize = 0;
Assert.Fail ("#C1");
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
Assert.IsNull (ex.InnerException, "#C3");
Assert.IsNotNull (ex.Message, "#C4");
try {
c.PerDomainCapacity = -5;
Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
try {
c.PerDomainCapacity = 0;
Assert.Fail ("#B1");
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
try {
c.PerDomainCapacity = (c.Capacity + 1);
Assert.Fail ("#C1");
- } catch (ArgumentOutOfRangeException ex) {
+ }
+ catch (ArgumentOutOfRangeException ex) {
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
Assert.IsNull (ex.InnerException, "#C3");
Assert.IsNotNull (ex.Message, "#C4");
[Test] // Add (Cookie)
public void Add1 ()
{
- Cookie cookie;
- CookieCollection cookies;
-
CookieContainer cc = new CookieContainer ();
- cookie = new Cookie ("Age", "28", string.Empty, "localhost");
+ Cookie cookie = new Cookie ("Age", "28", string.Empty, "localhost");
+ Assert.AreEqual ("Age", cookie.Name, "Name");
+ Assert.AreEqual ("28", cookie.Value, "Value");
+ Assert.AreEqual (String.Empty, cookie.Path, "Path");
+ Assert.AreEqual ("localhost", cookie.Domain, "Domain");
+ // does not survive the addition "cloning"
+ cookie.Comment = "comment";
+ cookie.CommentUri = new Uri ("http://localhost");
+ cookie.Discard = true;
+ cookie.Expires = DateTime.MaxValue;
+ cookie.HttpOnly = true;
+ cookie.Secure = true;
+ // except version
+ cookie.Version = 1;
+
cc.Add (cookie);
Assert.AreEqual (1, cc.Count, "#A1");
- cookies = cc.GetCookies (new Uri ("http://localhost/Whatever"));
+
+ CookieCollection cookies = cc.GetCookies (new Uri ("http://localhost/Whatever"));
Assert.AreEqual (1, cookies.Count, "#A2");
+ Assert.AreNotSame (cookie, cookies [0], "!same");
+
cookie = cookies [0];
- Assert.AreEqual ("Age", cookie.Name, "#A3");
- Assert.AreEqual ("28", cookie.Value, "#A4");
-#if false
- Assert.AreEqual ("/", cookie.Path, "#A5");
-#endif
- Assert.AreEqual ("localhost", cookie.Domain, "#A6");
+ Assert.AreEqual ("Age", cookie.Name, "Clone-Name");
+ Assert.AreEqual ("28", cookie.Value, "Clone-Value");
+ // Path is not the same, nor default
+ Assert.AreEqual ("/", cookie.Path, "Clone-Path");
+ Assert.AreEqual ("localhost", cookie.Domain, "Clone-Domain");
+ // other non-core properties have default values
+ Assert.AreEqual (String.Empty, cookie.Comment, "Clone-Comment");
+ Assert.IsNull (cookie.CommentUri, "Clone-CommentUri");
+ Assert.IsFalse (cookie.Discard, "Clone-Discard");
+ Assert.AreEqual (DateTime.MinValue, cookie.Expires, "Clone-Expires");
+ Assert.IsFalse (cookie.HttpOnly, "Clone-HttpOnly");
+ Assert.IsFalse (cookie.Secure, "Clone-Secure");
+ // except version
+ Assert.AreEqual (1, cookie.Version, "Clone-Version");
+
+ cookies = cc.GetCookies (new Uri ("http://localhost/Whatever"));
+ // the same Cookie instance returned for a second query
+ Assert.AreSame (cookie, cookies [0], "!same-2");
}
[Test] // Add (Cookie)
try {
cc.Add (cookie);
Assert.Fail ("#1");
- } catch (ArgumentException ex) {
+ }
+ catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
#if NET_2_0
try {
cc.Add ((CookieCollection) null);
Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
+ }
+ catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
try {
cc.Add ((Uri) null, cookie);
Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
+ }
+ catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
try {
cc.Add (uri, (Cookie) null);
Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
+ }
+ catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
try {
cc.Add ((Uri) null, cookies);
Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
+ }
+ catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
try {
cc.Add (uri, (CookieCollection) null);
Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
+ }
+ catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
}
[Test]
- [Category ("NotWorking")]
+ // [Category ("NotWorking")]
public void TestAddExpired_Cookie ()
{
CookieContainer cc = new CookieContainer ();
Assert.IsFalse (cookie.Secure, "#C11");
Assert.AreEqual (2, cc.Count, "#D1");
- coll = cc.GetCookies (new Uri("http://contoso.com"));
+ coll = cc.GetCookies (new Uri ("http://contoso.com"));
Assert.AreEqual (0, coll.Count, "#D1.1");
//not expired cookie
}
[Test]
- [Category ("NotWorking")]
public void GetCookieHeader1 ()
{
CookieContainer cc;
}
[Test]
- [Category ("NotWorking")]
- public void GetCookieHeader2 ()
+ public void GetCookieHeader2a ()
{
- CookieContainer cc;
- Cookie cookie;
-
- cc = new CookieContainer ();
- cookie = new Cookie ("Country", "Belgium", "/path", "mono.com");
+ CookieContainer cc = new CookieContainer ();
+ Cookie cookie = new Cookie ("Country", "Belgium", "/path", "mono.com");
cc.Add (cookie);
cookie = new Cookie ("Age", "26", "/path", "dev.mono.com");
cc.Add (cookie);
Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://mono.com/path")), "#A2");
Assert.AreEqual ("", cc.GetCookieHeader (new Uri ("http://test.mono.com/path")), "#A3");
Assert.AreEqual ("", cc.GetCookieHeader (new Uri ("http://us.dev.mono.com/path")), "#A4");
+ }
- cc = new CookieContainer ();
- cookie = new Cookie ("Country", "Belgium", "/path", ".mono.com");
+ [Test]
+ public void GetCookieHeader2b ()
+ {
+ CookieContainer cc = new CookieContainer ();
+ Cookie cookie = new Cookie ("Country", "Belgium", "/path", ".mono.com");
cc.Add (cookie);
cookie = new Cookie ("Age", "26", "/path", ".dev.mono.com");
cc.Add (cookie);
}
[Test]
- [Category ("NotWorking")]
public void GetCookieHeader3 ()
{
- CookieContainer cc;
-
- cc = new CookieContainer ();
+ CookieContainer cc = new CookieContainer ();
cc.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do"),
"Country=Belgium; path=/Whatever; domain=mono.com;" +
"Age=26; path=/Whatever; domain=test.mono.com," +
}
[Test]
- [Category ("NotWorking")]
public void GetCookieHeader4 ()
{
- CookieContainer cc;
- Cookie cookie;
-
- cc = new CookieContainer ();
- cookie = new Cookie ("Height", "178", "/Whatever", "mono.com");
+ CookieContainer cc = new CookieContainer ();
+ Cookie cookie = new Cookie ("Height", "178", "/Whatever", "mono.com");
cc.Add (cookie);
cookie = new Cookie ("Town", "Brussels", "/Whatever", ".mono.com");
cc.Add (cookie);
}
[Test]
- [Category ("NotWorking")]
- public void GetCookieHeader5 ()
+ public void GetCookieHeader5a ()
{
- CookieContainer cc;
- Cookie cookie;
+ CookieContainer cc = new CookieContainer ();
+ Cookie cookie = new Cookie ("name1", "value1", "", "localhost");
+ cookie.Comment = "Short name";
+ cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
+ cookie.Version = 1;
+ cc.Add (cookie);
+ Assert.AreEqual ("$Version=1; name1=value1; $Domain=localhost", cookie.ToString (), "#E0");
+ Assert.AreEqual ("$Version=1; name1=value1; $Path=/",
+ cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#E1");
+ }
- cc = new CookieContainer ();
- cookie = new Cookie ("name1", "value1", "", "localhost");
+ [Test]
+ public void GetCookieHeader5b ()
+ {
+ CookieContainer cc = new CookieContainer ();
+ Cookie cookie = new Cookie ("name1", "value1");
+ cookie.Domain = "localhost";
cookie.Comment = "Short name";
cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
cookie.Version = 1;
cc.Add (cookie);
+ Assert.AreEqual ("$Version=1; name1=value1; $Domain=localhost", cookie.ToString (), "#E0");
Assert.AreEqual ("$Version=1; name1=value1; $Path=/",
cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#E1");
}
[Test]
public void GetCookieHeader6 ()
{
- CookieContainer cc;
- Cookie cookie;
-
- cc = new CookieContainer ();
- cookie = new Cookie ("name1", "value1", "", "localhost");
+ CookieContainer cc = new CookieContainer ();
+ Cookie cookie = new Cookie ("name1", "value1", "", "localhost");
cookie.Comment = "Short name";
cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
cookie.Version = 0;
}
[Test]
- [Category ("NotWorking")]
- public void GetCookieHeader7 ()
+ public void GetCookieHeader7a ()
{
- CookieContainer cc;
- Cookie cookie;
-
- cc = new CookieContainer ();
- cookie = new Cookie ("name1", "value1", "/path", ".mono.com");
+ CookieContainer cc = new CookieContainer ();
+ Cookie cookie = new Cookie ("name1", "value1", "/path", ".mono.com");
cookie.Comment = "Short name";
cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
cookie.Version = 0;
Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://live.mono.com/whatever")), "#A3");
Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://gomono.com/path/sub")), "#A4");
Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://mono.com/path/sub")), "#A5");
+ }
- cc = new CookieContainer ();
- cookie = new Cookie ("name1", "value1", "/path", "live.mono.com");
+ [Test]
+ public void GetCookieHeader7b ()
+ {
+ CookieContainer cc = new CookieContainer ();
+ Cookie cookie = new Cookie ("name1", "value1", "/path", "live.mono.com");
cookie.Comment = "Short name";
cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
cookie.Version = 0;
try {
cc.GetCookieHeader ((Uri) null);
Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
+ }
+ catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
}
[Test]
- [Category ("NotWorking")]
- public void GetCookies2 ()
+ public void GetCookies2a ()
{
- CookieContainer container;
- CookieCollection cookies;
- Cookie cookie;
-
- container = new CookieContainer ();
+ CookieContainer container = new CookieContainer ();
container.Add (new Cookie ("Country", "Belgium", "/path", "mono.com"));
container.Add (new Cookie ("Age", "26", "/path", "dev.mono.com"));
- cookies = container.GetCookies (new Uri ("http://dev.mono.com/path/ok"));
+ CookieCollection cookies = container.GetCookies (new Uri ("http://dev.mono.com/path/ok"));
Assert.IsNotNull (cookies, "#G1");
Assert.AreEqual (1, cookies.Count, "#G2");
- cookie = cookies [0];
+ Cookie cookie = cookies [0];
Assert.AreEqual ("Age", cookie.Name, "#H1");
Assert.AreEqual ("26", cookie.Value, "#H2");
Assert.AreEqual ("/path", cookie.Path, "#H3");
cookies = container.GetCookies (new Uri ("http://us.dev.mono.com/path"));
Assert.IsNotNull (cookies, "#L1");
Assert.AreEqual (0, cookies.Count, "#L2");
+ }
- container = new CookieContainer ();
+ [Test]
+ public void GetCookies2b ()
+ {
+ CookieContainer container = new CookieContainer ();
container.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do"),
"Country=Belgium; path=/Whatever; domain=mono.com," +
"Age=26; path=/Whatever; domain=test.mono.com," +
"Weight=87; path=/Whatever/Do; domain=.mono.com;");
- cookies = container.GetCookies (new Uri ("http://dev.mono.com/Whatever/Do"));
+ CookieCollection cookies = container.GetCookies (new Uri ("http://dev.mono.com/Whatever/Do"));
Assert.IsNotNull (cookies, "#M1");
Assert.AreEqual (2, cookies.Count, "#M2");
- cookie = cookies [0];
+ Cookie cookie = cookies [0];
Assert.AreEqual ("Weight", cookie.Name, "#N1");
Assert.AreEqual ("87", cookie.Value, "#N2");
Assert.AreEqual ("/Whatever/Do", cookie.Path, "#N3");
Assert.AreEqual ("Belgium", cookie.Value, "#S10");
Assert.AreEqual ("/Whatever", cookie.Path, "#S11");
Assert.AreEqual ("mono.com", cookie.Domain, "#S12");
+ }
- container = new CookieContainer ();
+ [Test]
+ public void GetCookies2c ()
+ {
+ CookieContainer container = new CookieContainer ();
container.Add (new Cookie ("Height", "178", "/Whatever", "mono.com"));
container.Add (new Cookie ("Town", "Brussels", "/Whatever", ".mono.com"));
container.Add (new Cookie ("Income", "34445", "/Whatever/", ".test.mono.com"));
"Age=26; path=/Whatever/Do; domain=test.mono.com," +
"Weight=87; path=/");
- cookies = container.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do/Ok"));
+ CookieCollection cookies = container.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do/Ok"));
Assert.IsNotNull (cookies, "#T1");
Assert.AreEqual (3, cookies.Count, "#T2");
- cookie = cookies [0];
+ Cookie cookie = cookies [0];
Assert.AreEqual ("Age", cookie.Name, "#U1");
Assert.AreEqual ("26", cookie.Value, "#U2");
Assert.AreEqual ("/Whatever/Do", cookie.Path, "#U3");
try {
cc.GetCookies (null);
Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
+ }
+ catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
}
[Test]
- [Category ("NotWorking")]
+ // [Category ("NotWorking")]
public void SetCookies ()
{
- CookieContainer cc;
- CookieCollection cookies;
- Cookie cookie;
-
Uri uri = new Uri ("http://dev.test.mono.com/Whatever/Do/You");
DateTime now = DateTime.Now;
- cc = new CookieContainer ();
+ CookieContainer cc = new CookieContainer ();
cc.SetCookies (uri, "Country=Belgium," +
"Age=26; ; path=/Whatever/Do; domain=test.mono.com," +
"Weight=87; path=/; ");
Assert.AreEqual (3, cc.Count, "#A");
- cookies = cc.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do/Ok"));
+ CookieCollection cookies = cc.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do/Ok"));
Assert.IsNotNull (cookies, "#B1");
Assert.AreEqual (1, cookies.Count, "#B2");
- cookie = cookies [0];
+ Cookie cookie = cookies [0];
Assert.AreEqual (string.Empty, cookie.Comment, "#C:Comment");
Assert.IsNull (cookie.CommentUri, "#C:CommentUri");
Assert.IsFalse (cookie.Discard, "#C:Discard");
Assert.IsNotNull (cookies, "#D1");
Assert.AreEqual (2, cookies.Count, "#D2");
- cookie = cookies [0];
- Assert.AreEqual (string.Empty, cookie.Comment, "#E:Comment");
- Assert.IsNull (cookie.CommentUri, "#E:CommentUri");
- Assert.IsFalse (cookie.Discard, "#E:Discard");
- Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#E:Domain");
- Assert.IsFalse (cookie.Expired, "#E:Expired");
- Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#E:Expires");
+ // our sorting is not 100% identical to MS implementation
+ for (int i = 0; i < cookies.Count; i++) {
+ cookie = cookies [i];
+ switch (cookie.Name) {
+ case "Weight":
+ Assert.AreEqual (string.Empty, cookie.Comment, "#E:Comment");
+ Assert.IsNull (cookie.CommentUri, "#E:CommentUri");
+ Assert.IsFalse (cookie.Discard, "#E:Discard");
+ Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#E:Domain");
+ Assert.IsFalse (cookie.Expired, "#E:Expired");
+ Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#E:Expires");
#if NET_2_0
- Assert.IsFalse (cookie.HttpOnly, "#E:HttpOnly");
+ Assert.IsFalse (cookie.HttpOnly, "#E:HttpOnly");
#endif
- Assert.AreEqual ("Weight", cookie.Name, "#E:Name");
- Assert.AreEqual ("/", cookie.Path, "#E:Path");
- Assert.IsFalse (cookie.Secure, "#E:Secure");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#E:TimeStamp1");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#E:TimeStamp2");
- Assert.AreEqual ("87", cookie.Value, "#E:Value");
- Assert.AreEqual (0, cookie.Version, "#E:Version");
-
- cookie = cookies [1];
- Assert.AreEqual (string.Empty, cookie.Comment, "#F:Comment");
- Assert.IsNull (cookie.CommentUri, "#F:CommentUri");
- Assert.IsFalse (cookie.Discard, "#F:Discard");
- Assert.AreEqual ("test.mono.com", cookie.Domain, "#F:Domain");
- Assert.IsFalse (cookie.Expired, "#F:Expired");
- Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#F:Expires");
+ Assert.AreEqual ("Weight", cookie.Name, "#E:Name");
+ Assert.AreEqual ("/", cookie.Path, "#E:Path");
+ Assert.IsFalse (cookie.Secure, "#E:Secure");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#E:TimeStamp1");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#E:TimeStamp2");
+ Assert.AreEqual ("87", cookie.Value, "#E:Value");
+ Assert.AreEqual (0, cookie.Version, "#E:Version");
+ break;
+ case "Age":
+ Assert.AreEqual (string.Empty, cookie.Comment, "#F:Comment");
+ Assert.IsNull (cookie.CommentUri, "#F:CommentUri");
+ Assert.IsFalse (cookie.Discard, "#F:Discard");
+ Assert.AreEqual ("test.mono.com", cookie.Domain, "#F:Domain");
+ Assert.IsFalse (cookie.Expired, "#F:Expired");
+ Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#F:Expires");
#if NET_2_0
- Assert.IsFalse (cookie.HttpOnly, "#F:HttpOnly");
+ Assert.IsFalse (cookie.HttpOnly, "#F:HttpOnly");
#endif
- Assert.AreEqual ("Age", cookie.Name, "#F:Name");
- Assert.AreEqual ("/Whatever/Do", cookie.Path, "#F:Path");
- Assert.IsFalse (cookie.Secure, "#F:Secure");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#F:TimeStamp1");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#F:TimeStamp2");
- Assert.AreEqual ("26", cookie.Value, "#F:Value");
- Assert.AreEqual (0, cookie.Version, "#F:Version");
+ Assert.AreEqual ("Age", cookie.Name, "#F:Name");
+ Assert.AreEqual ("/Whatever/Do", cookie.Path, "#F:Path");
+ Assert.IsFalse (cookie.Secure, "#F:Secure");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#F:TimeStamp1");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#F:TimeStamp2");
+ Assert.AreEqual ("26", cookie.Value, "#F:Value");
+ Assert.AreEqual (0, cookie.Version, "#F:Version");
+ break;
+ default:
+ Assert.Fail (cookie.Name);
+ break;
+ }
+ }
cookies = cc.GetCookies (uri);
Assert.IsNotNull (cookies, "#G1");
Assert.AreEqual (3, cookies.Count, "#G2");
- cookie = cookies [0];
- Assert.AreEqual (string.Empty, cookie.Comment, "#H:Comment");
- Assert.IsNull (cookie.CommentUri, "#H:CommentUri");
- Assert.IsFalse (cookie.Discard, "#H:Discard");
- Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#H:Domain");
- Assert.IsFalse (cookie.Expired, "#H:Expired");
- Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#H:Expires");
+ // our sorting is not 100% identical to MS implementation
+ for (int i = 0; i < cookies.Count; i++) {
+ cookie = cookies [i];
+ switch (cookie.Name) {
+ case "Country":
+ Assert.AreEqual (string.Empty, cookie.Comment, "#H:Comment");
+ Assert.IsNull (cookie.CommentUri, "#H:CommentUri");
+ Assert.IsFalse (cookie.Discard, "#H:Discard");
+ Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#H:Domain");
+ Assert.IsFalse (cookie.Expired, "#H:Expired");
+ Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#H:Expires");
#if NET_2_0
- Assert.IsFalse (cookie.HttpOnly, "#H:HttpOnly");
+ Assert.IsFalse (cookie.HttpOnly, "#H:HttpOnly");
#endif
- Assert.AreEqual ("Country", cookie.Name, "#H:Name");
- Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#H:Path");
- Assert.IsFalse (cookie.Secure, "#H:Secure");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#H:TimeStamp1");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#H:TimeStamp2");
- Assert.AreEqual ("Belgium", cookie.Value, "#H:Value");
- Assert.AreEqual (0, cookie.Version, "#H:Version");
-
- cookie = cookies [1];
- Assert.AreEqual (string.Empty, cookie.Comment, "#I:Comment");
- Assert.IsNull (cookie.CommentUri, "#I:CommentUri");
- Assert.IsFalse (cookie.Discard, "#I:Discard");
- Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#I:Domain");
- Assert.IsFalse (cookie.Expired, "#I:Expired");
- Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#I:Expires");
+ Assert.AreEqual ("Country", cookie.Name, "#H:Name");
+ Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#H:Path");
+ Assert.IsFalse (cookie.Secure, "#H:Secure");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#H:TimeStamp1");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#H:TimeStamp2");
+ Assert.AreEqual ("Belgium", cookie.Value, "#H:Value");
+ Assert.AreEqual (0, cookie.Version, "#H:Version");
+ break;
+ case "Weight":
+ Assert.AreEqual (string.Empty, cookie.Comment, "#I:Comment");
+ Assert.IsNull (cookie.CommentUri, "#I:CommentUri");
+ Assert.IsFalse (cookie.Discard, "#I:Discard");
+ Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#I:Domain");
+ Assert.IsFalse (cookie.Expired, "#I:Expired");
+ Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#I:Expires");
#if NET_2_0
- Assert.IsFalse (cookie.HttpOnly, "#I:HttpOnly");
+ Assert.IsFalse (cookie.HttpOnly, "#I:HttpOnly");
#endif
- Assert.AreEqual ("Weight", cookie.Name, "#I:Name");
- Assert.AreEqual ("/", cookie.Path, "#I:Path");
- Assert.IsFalse (cookie.Secure, "#I:Secure");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#I:TimeStamp1");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#I:TimeStamp2");
- Assert.AreEqual ("87", cookie.Value, "#I:Value");
- Assert.AreEqual (0, cookie.Version, "#I:Version");
-
- cookie = cookies [2];
- Assert.AreEqual (string.Empty, cookie.Comment, "#J:Comment");
- Assert.IsNull (cookie.CommentUri, "#J:CommentUri");
- Assert.IsFalse (cookie.Discard, "#J:Discard");
- Assert.AreEqual ("test.mono.com", cookie.Domain, "#J:Domain");
- Assert.IsFalse (cookie.Expired, "#J:Expired");
- Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#J:Expires");
+ Assert.AreEqual ("Weight", cookie.Name, "#I:Name");
+ Assert.AreEqual ("/", cookie.Path, "#I:Path");
+ Assert.IsFalse (cookie.Secure, "#I:Secure");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#I:TimeStamp1");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#I:TimeStamp2");
+ Assert.AreEqual ("87", cookie.Value, "#I:Value");
+ Assert.AreEqual (0, cookie.Version, "#I:Version");
+ break;
+ case "Age":
+ Assert.AreEqual (string.Empty, cookie.Comment, "#J:Comment");
+ Assert.IsNull (cookie.CommentUri, "#J:CommentUri");
+ Assert.IsFalse (cookie.Discard, "#J:Discard");
+ Assert.AreEqual ("test.mono.com", cookie.Domain, "#J:Domain");
+ Assert.IsFalse (cookie.Expired, "#J:Expired");
+ Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#J:Expires");
#if NET_2_0
- Assert.IsFalse (cookie.HttpOnly, "#J:HttpOnly");
+ Assert.IsFalse (cookie.HttpOnly, "#J:HttpOnly");
#endif
- Assert.AreEqual ("Age", cookie.Name, "#J:Name");
- Assert.AreEqual ("/Whatever/Do", cookie.Path, "#J:Path");
- Assert.IsFalse (cookie.Secure, "#J:Secure");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#J:TimeStamp1");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#J:TimeStamp2");
- Assert.AreEqual ("26", cookie.Value, "#J:Value");
- Assert.AreEqual (0, cookie.Version, "#J:Version");
+ Assert.AreEqual ("Age", cookie.Name, "#J:Name");
+ Assert.AreEqual ("/Whatever/Do", cookie.Path, "#J:Path");
+ Assert.IsFalse (cookie.Secure, "#J:Secure");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#J:TimeStamp1");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#J:TimeStamp2");
+ Assert.AreEqual ("26", cookie.Value, "#J:Value");
+ Assert.AreEqual (0, cookie.Version, "#J:Version");
+ break;
+ default:
+ Assert.Fail (cookie.Name);
+ break;
+ }
+ }
cc.SetCookies (uri, "Country=,A");
cookies = cc.GetCookies (uri);
Assert.IsNotNull (cookies, "#L1");
Assert.AreEqual (2, cookies.Count, "#L2");
- cookie = cookies [0];
- Assert.AreEqual (string.Empty, cookie.Comment, "#M:Comment");
- Assert.IsNull (cookie.CommentUri, "#M:CommentUri");
- Assert.IsFalse (cookie.Discard, "#M:Discard");
- Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#M:Domain");
- Assert.IsFalse (cookie.Expired, "#M:Expired");
- Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#M:Expires");
+ // our sorting is not 100% identical to MS implementation
+ for (int i = 0; i < cookies.Count; i++) {
+ cookie = cookies [i];
+ switch (cookie.Name) {
+ case "Country":
+ Assert.AreEqual (string.Empty, cookie.Comment, "#M:Comment");
+ Assert.IsNull (cookie.CommentUri, "#M:CommentUri");
+ Assert.IsFalse (cookie.Discard, "#M:Discard");
+ Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#M:Domain");
+ Assert.IsFalse (cookie.Expired, "#M:Expired");
+ Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#M:Expires");
#if NET_2_0
- Assert.IsFalse (cookie.HttpOnly, "#M:HttpOnly");
+ Assert.IsFalse (cookie.HttpOnly, "#M:HttpOnly");
#endif
- Assert.AreEqual ("Country", cookie.Name, "#M:Name");
- Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#M:Path");
- Assert.IsFalse (cookie.Secure, "#M:Secure");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#M:TimeStamp1");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#M:TimeStamp2");
- Assert.AreEqual (string.Empty, cookie.Value, "#M:Value");
- Assert.AreEqual (0, cookie.Version, "#M:Version");
-
- cookie = cookies [1];
- Assert.AreEqual (string.Empty, cookie.Comment, "#N:Comment");
- Assert.IsNull (cookie.CommentUri, "#N:CommentUri");
- Assert.IsFalse (cookie.Discard, "#N:Discard");
- Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#N:Domain");
- Assert.IsFalse (cookie.Expired, "#N:Expired");
- Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#N:Expires");
+ Assert.AreEqual ("Country", cookie.Name, "#M:Name");
+ Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#M:Path");
+ Assert.IsFalse (cookie.Secure, "#M:Secure");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#M:TimeStamp1");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#M:TimeStamp2");
+ Assert.AreEqual (string.Empty, cookie.Value, "#M:Value");
+ Assert.AreEqual (0, cookie.Version, "#M:Version");
+ break;
+ case "A":
+ Assert.AreEqual (string.Empty, cookie.Comment, "#N:Comment");
+ Assert.IsNull (cookie.CommentUri, "#N:CommentUri");
+ Assert.IsFalse (cookie.Discard, "#N:Discard");
+ Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#N:Domain");
+ Assert.IsFalse (cookie.Expired, "#N:Expired");
+ Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#N:Expires");
#if NET_2_0
- Assert.IsFalse (cookie.HttpOnly, "#N:HttpOnly");
+ Assert.IsFalse (cookie.HttpOnly, "#N:HttpOnly");
#endif
- Assert.AreEqual ("A", cookie.Name, "#N:Name");
- Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#N:Path");
- Assert.IsFalse (cookie.Secure, "#N:Secure");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#N:TimeStamp1");
- Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#N:TimeStamp2");
- Assert.AreEqual (string.Empty, cookie.Value, "#N:Value");
- Assert.AreEqual (0, cookie.Version, "#N:Version");
+ Assert.AreEqual ("A", cookie.Name, "#N:Name");
+ Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#N:Path");
+ Assert.IsFalse (cookie.Secure, "#N:Secure");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#N:TimeStamp1");
+ Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#N:TimeStamp2");
+ Assert.AreEqual (string.Empty, cookie.Value, "#N:Value");
+ Assert.AreEqual (0, cookie.Version, "#N:Version");
+ break;
+ default:
+ Assert.Fail (cookie.Name);
+ break;
+ }
+ }
}
[Test]
try {
cc.SetCookies (new Uri ("http://www.contoso.com"), null);
Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
+ }
+ catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
}
[Test]
- [Category ("NotWorking")]
- public void SetCookies_CookieHeader_Invalid ()
+ public void SetCookies_CookieHeader_Invalid_1 ()
{
- CookieContainer cc;
-
// cookie format error
- cc = new CookieContainer ();
+ CookieContainer cc = new CookieContainer ();
try {
cc.SetCookies (new Uri ("http://www.contoso.com"), "=lalala");
Assert.Fail ("#A1");
- } catch (CookieException ex) {
+ }
+ catch (CookieException ex) {
// An error has occurred when parsing Cookie
// header for Uri 'http://www.contoso.com/'
Assert.AreEqual (typeof (CookieException), ex.GetType (), "#A2");
Assert.IsNull (inner.InnerException, "#A7");
Assert.IsNotNull (inner.Message, "#A8");
}
+ }
+ [Test]
+ public void SetCookies_CookieHeader_Invalid_2 ()
+ {
// cookie path not part of URI path
- cc = new CookieContainer ();
+ CookieContainer cc = new CookieContainer ();
try {
cc.SetCookies (new Uri ("http://dev.test.mono.com/Whatever"),
"Age=26; path=/Whatever/Do; domain=test.mono.com");
Assert.Fail ("#B1");
- } catch (CookieException ex) {
+ }
+ catch (CookieException ex) {
// An error has occurred when parsing Cookie
// header for Uri 'http://dev.test.mono.com/Whatever'
Assert.AreEqual (typeof (CookieException), ex.GetType (), "#B2");
try {
cc.SetCookies (null, "Age=26; path=/Whatever; domain=test.mono.com");
Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
+ }
+ catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
}
[Test]
- [Category ("NotWorking")]
public void SetCookies_DomainMatchesHost ()
{
CookieContainer cc = new CookieContainer ();
-
#if NET_2_0
+ // domains looks identical - but "domain=test.mono.com" means "*.test.mono.com"
cc.SetCookies (new Uri ("http://test.mono.com/Whatever/Do"),
"Age=26; path=/Whatever; domain=test.mono.com");
CookieCollection cookies = cc.GetCookies (new Uri ("http://test.mono.com/Whatever/Do"));
}
[Test]
- [Category ("NotWorking")]
public void SetCookies_Domain_Local ()
{
CookieContainer cc;
"Age=26; path=/Whatever; domain=.local");
cookies = cc.GetCookies (new Uri ("http://localhost/Whatever/Do"));
Assert.IsNotNull (cookies, "#A1");
- Assert.AreEqual (1, cookies.Count, "#A2");
+ Assert.AreEqual (0, cookies.Count, "#A2");
cookies = cc.GetCookies (new Uri ("http://127.0.0.1/Whatever/Do"));
Assert.IsNotNull (cookies, "#A3");
Assert.AreEqual (0, cookies.Count, "#A4");
cookies = cc.GetCookies (new Uri ("http://" + Dns.GetHostName () + "/Whatever/Do"));
Assert.IsNotNull (cookies, "#A5");
- Assert.AreEqual (1, cookies.Count, "#A6");
+ Assert.AreEqual (0, cookies.Count, "#A6");
cc = new CookieContainer ();
cc.SetCookies (new Uri ("http://127.0.0.1/Whatever/Do"),
"Age=26; path=/Whatever; domain=.local");
cookies = cc.GetCookies (new Uri ("http://localhost/Whatever/Do"));
Assert.IsNotNull (cookies, "#B1");
- Assert.AreEqual (1, cookies.Count, "#B2");
+ Assert.AreEqual (0, cookies.Count, "#B2");
cookies = cc.GetCookies (new Uri ("http://127.0.0.1/Whatever/Do"));
Assert.IsNotNull (cookies, "#B3");
Assert.AreEqual (0, cookies.Count, "#B4");
cookies = cc.GetCookies (new Uri ("http://" + Dns.GetHostName () + "/Whatever/Do"));
Assert.IsNotNull (cookies, "#B5");
- Assert.AreEqual (1, cookies.Count, "#B6");
+ Assert.AreEqual (0, cookies.Count, "#B6");
cc = new CookieContainer ();
cc.SetCookies (new Uri ("http://" + Dns.GetHostName () + "/Whatever/Do"),
"Age=26; path=/Whatever; domain=.local");
cookies = cc.GetCookies (new Uri ("http://localhost/Whatever/Do"));
Assert.IsNotNull (cookies, "#C1");
- Assert.AreEqual (1, cookies.Count, "#C2");
+ Assert.AreEqual (0, cookies.Count, "#C2");
cookies = cc.GetCookies (new Uri ("http://127.0.0.1/Whatever/Do"));
Assert.IsNotNull (cookies, "#C3");
Assert.AreEqual (0, cookies.Count, "#C4");
cookies = cc.GetCookies (new Uri ("http://" + Dns.GetHostName () + "/Whatever/Do"));
Assert.IsNotNull (cookies, "#C5");
- Assert.AreEqual (1, cookies.Count, "#C6");
+ Assert.AreEqual (0, cookies.Count, "#C6");
}
[Test]
Assert.AreEqual (0, cookies.Count, "#C");
}
+
+ [Test]
+ public void MoreThanDefaultDomainCookieLimit ()
+ {
+ CookieContainer cc = new CookieContainer ();
+ for (int i = 1; i <= CookieContainer.DefaultPerDomainCookieLimit; i++) {
+ Cookie c = new Cookie (i.ToString (), i.ToString (), "/", "mono.com");
+ cc.Add (c);
+ }
+ Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, cc.Count, "Count");
+ Cookie c2 = new Cookie ("uho", "21", "/", "mono.com");
+ cc.Add (c2);
+ Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, cc.Count, "Count");
+ // so one (yes '1' ;-) was removed
+ }
+
+ [Test]
+ public void MoreThanDefaultCookieLimit ()
+ {
+ CookieContainer cc = new CookieContainer ();
+ for (int i = 1; i <= CookieContainer.DefaultCookieLimit; i++) {
+ Cookie c = new Cookie (i.ToString (), i.ToString (), "/", "www" + i.ToString () + ".mono.com");
+ cc.Add (c);
+ }
+ Assert.AreEqual (CookieContainer.DefaultCookieLimit, cc.Count, "Count");
+ Cookie c2 = new Cookie ("uho", "301", "/", "www301.mono.com");
+ cc.Add (c2);
+ Assert.AreEqual (CookieContainer.DefaultCookieLimit, cc.Count, "Count");
+ // so one (yes '1' ;-) was removed
+ }
}
}