2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System / Test / System.Net / CookieCollectionTest.cs
1 //\r
2 // CookieCollectionTest.cs - NUnit Test Cases for System.Net.CookieCollection\r
3 //\r
4 // Authors:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)\r
7 //\r
8 // (C) 2003 Martin Willemoes Hansen\r
9 //\r
10 \r
11 using NUnit.Framework;\r
12 using System;\r
13 using System.Net;\r
14 using System.Collections;\r
15 \r
16 namespace MonoTests.System.Net\r
17 {\r
18 \r
19 [TestFixture]\r
20 public class CookieCollectionTest\r
21 {\r
22         CookieCollection col;\r
23         \r
24         [SetUp]\r
25         public void GetReady () \r
26         {\r
27                 col = new CookieCollection ();  \r
28                 col.Add (new Cookie ("name1", "value1"));\r
29                 col.Add (new Cookie ("name2", "value2", "path2"));\r
30                 col.Add (new Cookie ("name3", "value3", "path3", "domain3"));           \r
31         }\r
32 \r
33         [Test]\r
34         public void Count ()\r
35         {\r
36                 Assert.AreEqual (col.Count, 3, "#1");\r
37         }\r
38 \r
39         [Test]\r
40         public void Indexer ()\r
41         {\r
42                 Cookie c = null;\r
43                 try {\r
44                         c = col [-1];\r
45                         Assert.Fail ("#1");\r
46                 } catch (ArgumentOutOfRangeException) {\r
47                 }\r
48                 try {\r
49                         c = col [col.Count];\r
50                         Assert.Fail ("#2");\r
51                 } catch (ArgumentOutOfRangeException) {\r
52                 }\r
53                 c = col ["name1"];\r
54                 Assert.AreEqual (c.Name, "name1", "#3");\r
55                 c = col ["NAME2"];\r
56                 Assert.AreEqual (c.Name, "name2", "#4");\r
57         }\r
58         \r
59         [Test]\r
60         public void Add ()\r
61         {\r
62                 try {\r
63                         Cookie c = null;\r
64                         col.Add (c);\r
65                         Assert.Fail ("#1");\r
66                 } catch (ArgumentNullException) {\r
67                 }\r
68                 \r
69                 // in the microsoft implementation this will fail,\r
70                 // so we'll have to fail to.\r
71                 try {\r
72                         col.Add (col);\r
73                         Assert.Fail ("#2");\r
74                 } catch (Exception) {\r
75                 }\r
76                 Assert.AreEqual (col.Count, 3, "#3");\r
77                 \r
78                 col.Add (new Cookie("name1", "value1"));                \r
79                 Assert.AreEqual (col.Count, 3, "#4");\r
80                 \r
81                 CookieCollection col2 = new CookieCollection();\r
82                 Cookie c4 = new Cookie("name4", "value4");\r
83                 Cookie c5 = new Cookie("name5", "value5");\r
84                 col2.Add (c4);\r
85                 col2.Add (c5);\r
86                 col.Add (col2);\r
87                 Assert.AreEqual (col.Count, 5, "#5");\r
88                 Assert.AreEqual (col ["NAME4"], c4, "#6");\r
89                 Assert.AreEqual (col [4], c5, "#7");\r
90         }\r
91         \r
92         [Test]\r
93         public void CopyTo ()\r
94         {\r
95                 Array a = Array.CreateInstance (typeof (Cookie), 3);\r
96                 col.CopyTo (a, 0);\r
97                 Assert.AreEqual (a.GetValue (0), col [0], "#1");\r
98                 Assert.AreEqual (a.GetValue (1), col [1], "#2");\r
99                 Assert.AreEqual (a.GetValue (2), col [2], "#3");\r
100         }\r
101         \r
102         [Test]\r
103         public void Enumerator ()\r
104         {\r
105                 IEnumerator enumerator = col.GetEnumerator ();\r
106                 enumerator.MoveNext ();\r
107                 Cookie c = (Cookie) enumerator.Current;\r
108                 Assert.AreEqual (c, col [0], "#1");\r
109                 col.Add (new Cookie ("name6", "value6"));\r
110                 try {\r
111                         enumerator.MoveNext ();\r
112                         Assert.Fail ("#2");\r
113                 } catch (InvalidOperationException) {\r
114                 }\r
115         }\r
116 }\r
117 }\r
118 \r