Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System / Test / System.Net / CookieContainerTest.cs
1 //
2 // System.Net.CookieContainerTest - CookieContainer tests
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //      Daniel Nauck    (dna(at)mono-project(dot)de)
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //  Marek Safar (marek.safar@gmail.com)
9 //
10 // Copyright (C) 2004,2009 Novell, Inc (http://www.novell.com)
11 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
12 //
13
14 using System;
15 using System.Net;
16 using System.Reflection;
17
18 using NUnit.Framework;
19
20 /*
21  * About the RFC 2109 conditional:
22  * 
23  * According to the MSDN docs, settings Cookie.Version = 1 should make the
24  * implementation comply with RFC 2109.  I tested this on Windows and it
25  * looks like .NET 4.5 has a few bugs in this area.
26  * 
27  * The tests in this file also don't comply with RFC 2109 (for instance, the
28  * domain must start with a dot and single suffixes such as .localhost are
29  * not allowed).
30  * 
31  * Since there currently is no reference implementation due to these bugs in
32  * .NET 4.5, I disabled these tests for the moment and made them test the
33  * default behavior (the newer RFC 2965).
34  * 
35  * .NET 4.5 fixes several bugs in its (default, non-2109) cookie implementation
36  * over .NET 2.0 - I modified the tests to reflect this new, improved behavior.
37  * 
38  * 09/13/12 martin
39  * 
40  */
41
42 namespace MonoTests.System.Net {
43         [TestFixture]
44         public class CookieContainerTest {
45                 [Test] // .ctor ()
46                 public void Constructor1 ()
47                 {
48                         CookieContainer c = new CookieContainer ();
49                         Assert.AreEqual (0, c.Count, "Count");
50                         Assert.AreEqual (CookieContainer.DefaultCookieLimit, c.Capacity, "Capacity");
51                         Assert.AreEqual (CookieContainer.DefaultCookieLengthLimit, c.MaxCookieSize, "MaxCookieSize");
52                         Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "PerDomainCapacity");
53                 }
54
55                 [Test] // .ctor (Int32)
56                 public void Constructor2 ()
57                 {
58                         CookieContainer c = new CookieContainer (234);
59                         Assert.AreEqual (0, c.Count, "Count");
60                         Assert.AreEqual (234, c.Capacity, "Capacity");
61                         Assert.AreEqual (CookieContainer.DefaultCookieLengthLimit, c.MaxCookieSize, "MaxCookieSize");
62                         Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "PerDomainCapacity");
63                 }
64
65                 [Test]
66                 public void Constructor2_Capacity_Invalid ()
67                 {
68                         // Capacity <= 0
69                         try {
70                                 new CookieContainer (0);
71                                 Assert.Fail ("#A1");
72                         }
73                         catch (ArgumentException ex) {
74                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
75                                 Assert.IsNull (ex.InnerException, "#A3");
76                                 // The specified value must be greater than 0
77                                 Assert.IsNotNull (ex.Message, "#A4");
78                                 Assert.AreEqual ("Capacity", ex.ParamName, "#A5");
79                         }
80
81                         // Capacity <= 0
82                         try {
83                                 new CookieContainer (-10);
84                                 Assert.Fail ("#B1");
85                         }
86                         catch (ArgumentException ex) {
87                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
88                                 Assert.IsNull (ex.InnerException, "#B3");
89                                 // The specified value must be greater than 0
90                                 Assert.IsNotNull (ex.Message, "#B4");
91                                 Assert.AreEqual ("Capacity", ex.ParamName, "#B5");
92                         }
93                 }
94
95                 [Test] // .ctor (Int32, Int32, Int32)
96                 public void Constructor3 ()
97                 {
98                         CookieContainer c;
99
100                         c = new CookieContainer (100, 50, 1000);
101                         Assert.AreEqual (100, c.Capacity, "#A1");
102                         Assert.AreEqual (50, c.PerDomainCapacity, "#A2");
103                         Assert.AreEqual (1000, c.MaxCookieSize, "#A3");
104
105                         c = new CookieContainer (234, int.MaxValue, 650);
106                         Assert.AreEqual (234, c.Capacity, "#A1");
107                         Assert.AreEqual (int.MaxValue, c.PerDomainCapacity, "#A2");
108                         Assert.AreEqual (650, c.MaxCookieSize, "#A3");
109
110                         c = new CookieContainer (234, 234, 100);
111                         Assert.AreEqual (234, c.Capacity, "#A1");
112                         Assert.AreEqual (234, c.PerDomainCapacity, "#A2");
113                         Assert.AreEqual (100, c.MaxCookieSize, "#A3");
114                 }
115
116                 [Test]
117                 public void Constructor3_Capacity_Invalid ()
118                 {
119                         // Capacity <= 0
120                         try {
121                                 new CookieContainer (0, 0, 100);
122                                 Assert.Fail ("#A1");
123                         }
124                         catch (ArgumentException ex) {
125                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
126                                 Assert.IsNull (ex.InnerException, "#A3");
127                                 // The specified value must be greater than 0
128                                 Assert.IsNotNull (ex.Message, "#A4");
129                                 Assert.AreEqual ("Capacity", ex.ParamName, "#A5");
130                         }
131
132                         // Capacity <= 0
133                         try {
134                                 new CookieContainer (-10, 0, 100);
135                                 Assert.Fail ("#B1");
136                         }
137                         catch (ArgumentException ex) {
138                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
139                                 Assert.IsNull (ex.InnerException, "#B3");
140                                 // The specified value must be greater than 0
141                                 Assert.IsNotNull (ex.Message, "#B4");
142                                 Assert.AreEqual ("Capacity", ex.ParamName, "#B5");
143                         }
144                 }
145
146                 [Test] // .ctor (Int32, Int32, Int32)
147                 public void Constructor3_MaxCookieSize_Invalid ()
148                 {
149                         // MaxCookieSize <= 0
150                         try {
151                                 new CookieContainer (100, 50, 0);
152                                 Assert.Fail ("#A1");
153                         }
154                         catch (ArgumentException ex) {
155                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
156                                 Assert.IsNull (ex.InnerException, "#A3");
157                                 // The specified value must be greater than 0
158                                 Assert.IsNotNull (ex.Message, "#A3");
159                                 Assert.AreEqual ("MaxCookieSize", ex.ParamName, "#A4");
160                         }
161
162                         // MaxCookieSize <= 0
163                         try {
164                                 new CookieContainer (100, 50, -4);
165                                 Assert.Fail ("#B1");
166                         }
167                         catch (ArgumentException ex) {
168                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
169                                 Assert.IsNull (ex.InnerException, "#B3");
170                                 // The specified value must be greater than 0
171                                 Assert.IsNotNull (ex.Message, "#B3");
172                                 Assert.AreEqual ("MaxCookieSize", ex.ParamName, "#B4");
173                         }
174                 }
175
176                 [Test] // .ctor (Int32, Int32, Int32)
177                 public void Constructor3_PerDomainCapacity_Invalid ()
178                 {
179                         // PerDomainCapacity <= 0
180                         try {
181                                 new CookieContainer (432, 0, 1000);
182                                 Assert.Fail ("#B1");
183                         }
184                         catch (ArgumentOutOfRangeException ex) {
185                                 // 'PerDomainCapacity' has to be greater than
186                                 // '0' and less than '432'
187                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
188                                 Assert.IsNull (ex.InnerException, "#B3");
189                                 Assert.IsNotNull (ex.Message, "#B4");
190                                 Assert.AreEqual ("perDomainCapacity", ex.ParamName, "#B5");
191                         }
192
193                         // PerDomainCapacity <= 0
194                         try {
195                                 new CookieContainer (432, -1, 1000);
196                                 Assert.Fail ("#C1");
197                         }
198                         catch (ArgumentOutOfRangeException ex) {
199                                 // 'PerDomainCapacity' has to be greater than
200                                 // '0' and less than '432'
201                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
202                                 Assert.IsNull (ex.InnerException, "#C3");
203                                 Assert.IsNotNull (ex.Message, "#C4");
204                                 Assert.AreEqual ("perDomainCapacity", ex.ParamName, "#C5");
205                         }
206
207                         // PerDomainCapacity > Capacity (and != Int32.MaxValue)
208                         try {
209                                 new CookieContainer (432, 433, 1000);
210                                 Assert.Fail ("#C1");
211                         }
212                         catch (ArgumentOutOfRangeException ex) {
213                                 // 'PerDomainCapacity' has to be greater than
214                                 // '0' and less than '432'
215                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
216                                 Assert.IsNull (ex.InnerException, "#C3");
217                                 Assert.IsNotNull (ex.Message, "#C4");
218                                 Assert.AreEqual ("perDomainCapacity", ex.ParamName, "#C5");
219                         }
220                 }
221
222                 [Test]
223                 public void TestDefaultLimits ()
224                 {
225                         Assert.AreEqual (4096, CookieContainer.DefaultCookieLengthLimit, "#1");
226                         Assert.AreEqual (300, CookieContainer.DefaultCookieLimit, "#2");
227                         Assert.AreEqual (20, CookieContainer.DefaultPerDomainCookieLimit, "#3");
228                 }
229
230                 [Test]
231                 public void Capacity ()
232                 {
233                         CookieContainer c = new CookieContainer ();
234                         c.Capacity = c.PerDomainCapacity;
235                         Assert.AreEqual (c.PerDomainCapacity, c.Capacity, "#A1");
236                         Assert.AreEqual (CookieContainer.DefaultCookieLengthLimit, c.MaxCookieSize, "#A2");
237                         Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "#A3");
238                         c.Capacity = int.MaxValue;
239                         Assert.AreEqual (int.MaxValue, c.Capacity, "#B1");
240                         Assert.AreEqual (CookieContainer.DefaultCookieLengthLimit, c.MaxCookieSize, "#B2");
241                         Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "#B3");
242                         c.PerDomainCapacity = int.MaxValue;
243                         c.Capacity = (c.PerDomainCapacity - 1);
244                         Assert.AreEqual ((c.PerDomainCapacity - 1), c.Capacity, "#C1");
245                         Assert.AreEqual (CookieContainer.DefaultCookieLengthLimit, c.MaxCookieSize, "#C2");
246                         Assert.AreEqual (int.MaxValue, c.PerDomainCapacity, "#C3");
247                 }
248
249                 [Test]
250                 public void Capacity_Value_Invalid ()
251                 {
252                         CookieContainer c = new CookieContainer ();
253
254                         // Capacity <= 0
255                         try {
256                                 c.Capacity = -5;
257                                 Assert.Fail ("#A1");
258                         }
259                         catch (ArgumentOutOfRangeException ex) {
260                                 // 'Capacity' has to be greater than '0' and
261                                 // less than '20'
262                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
263                                 Assert.IsNull (ex.InnerException, "#A3");
264                                 Assert.IsNotNull (ex.Message, "#A4");
265                                 Assert.AreEqual ("value", ex.ParamName, "#A5");
266                         }
267
268                         // Capacity <= 0
269                         try {
270                                 c.Capacity = 0;
271                                 Assert.Fail ("#B1");
272                         }
273                         catch (ArgumentOutOfRangeException ex) {
274                                 // 'Capacity' has to be greater than '0' and
275                                 // less than '20'
276                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
277                                 Assert.IsNull (ex.InnerException, "#B3");
278                                 Assert.IsNotNull (ex.Message, "#B4");
279                                 Assert.AreEqual ("value", ex.ParamName, "#B5");
280                         }
281
282                         // Capacity < PerDomainCapacity (and PerDomainCapacity != Int32.MaxValue)
283                         try {
284                                 c.Capacity = 5;
285                                 Assert.Fail ("#C1");
286                         }
287                         catch (ArgumentOutOfRangeException ex) {
288                                 // 'Capacity' has to be greater than '0' and
289                                 // less than '20'
290                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
291                                 Assert.IsNull (ex.InnerException, "#C3");
292                                 Assert.IsNotNull (ex.Message, "#C4");
293                                 Assert.AreEqual ("value", ex.ParamName, "#C5");
294                         }
295                 }
296
297                 [Test]
298                 public void MaxCookieSize ()
299                 {
300                         CookieContainer c = new CookieContainer ();
301                         c.MaxCookieSize = 80000;
302                         Assert.AreEqual (CookieContainer.DefaultCookieLimit, c.Capacity, "#A1");
303                         Assert.AreEqual (80000, c.MaxCookieSize, "#A2");
304                         Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "#A3");
305                         c.MaxCookieSize = int.MaxValue;
306                         Assert.AreEqual (CookieContainer.DefaultCookieLimit, c.Capacity, "#B1");
307                         Assert.AreEqual (int.MaxValue, c.MaxCookieSize, "#B2");
308                         Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "#B3");
309                         c.MaxCookieSize = 1;
310                         Assert.AreEqual (CookieContainer.DefaultCookieLimit, c.Capacity, "#C1");
311                         Assert.AreEqual (1, c.MaxCookieSize, "#C2");
312                         Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "#C3");
313                 }
314
315                 [Test]
316                 public void MaxCookieSize_Value_Invalid ()
317                 {
318                         CookieContainer c = new CookieContainer ();
319
320                         // MaxCookieSize <= 0
321                         try {
322                                 c.MaxCookieSize = -5;
323                                 Assert.Fail ("#A1");
324                         }
325                         catch (ArgumentOutOfRangeException ex) {
326                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
327                                 Assert.IsNull (ex.InnerException, "#A3");
328                                 Assert.IsNotNull (ex.Message, "#A4");
329                                 Assert.AreEqual ("value", ex.ParamName, "#A5");
330                         }
331
332                         // MaxCookieSize <= 0
333                         try {
334                                 c.MaxCookieSize = -1;
335                                 Assert.Fail ("#B1");
336                         }
337                         catch (ArgumentOutOfRangeException ex) {
338                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
339                                 Assert.IsNull (ex.InnerException, "#B3");
340                                 Assert.IsNotNull (ex.Message, "#B4");
341                                 Assert.AreEqual ("value", ex.ParamName, "#B5");
342                         }
343
344                         // MaxCookieSize <= 0
345                         try {
346                                 c.MaxCookieSize = 0;
347                                 Assert.Fail ("#C1");
348                         }
349                         catch (ArgumentOutOfRangeException ex) {
350                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
351                                 Assert.IsNull (ex.InnerException, "#C3");
352                                 Assert.IsNotNull (ex.Message, "#C4");
353                                 Assert.AreEqual ("value", ex.ParamName, "#C5");
354                         }
355                 }
356
357                 [Test]
358                 public void PerDomainCapacity ()
359                 {
360                         CookieContainer c = new CookieContainer ();
361                         c.PerDomainCapacity = c.Capacity;
362                         Assert.AreEqual (c.Capacity, c.PerDomainCapacity, "#1");
363                         c.PerDomainCapacity = int.MaxValue;
364                         Assert.AreEqual (int.MaxValue, c.PerDomainCapacity, "#2");
365                         c.PerDomainCapacity = c.Capacity - 5;
366                         Assert.AreEqual ((c.Capacity - 5), c.PerDomainCapacity, "#3");
367                         c.PerDomainCapacity = 1;
368                         Assert.AreEqual (1, c.PerDomainCapacity, "#4");
369                 }
370
371                 [Test]
372                 public void PerDomainCapacity_Value_Invalid ()
373                 {
374                         CookieContainer c = new CookieContainer ();
375
376                         // PerDomainCapacity <= 0
377                         try {
378                                 c.PerDomainCapacity = -5;
379                                 Assert.Fail ("#A1");
380                         }
381                         catch (ArgumentOutOfRangeException ex) {
382                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
383                                 Assert.IsNull (ex.InnerException, "#A3");
384                                 Assert.IsNotNull (ex.Message, "#A4");
385                                 Assert.AreEqual ("value", ex.ParamName, "#A5");
386                         }
387
388                         // PerDomainCapacity <= 0
389                         try {
390                                 c.PerDomainCapacity = 0;
391                                 Assert.Fail ("#B1");
392                         }
393                         catch (ArgumentOutOfRangeException ex) {
394                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
395                                 Assert.IsNull (ex.InnerException, "#B3");
396                                 Assert.IsNotNull (ex.Message, "#B4");
397                                 Assert.AreEqual ("value", ex.ParamName, "#B5");
398                         }
399
400                         c.Capacity = (c.PerDomainCapacity + 5);
401
402                         // PerDomainCapacity > Capacity (and != Int32.MaxValue)
403                         try {
404                                 c.PerDomainCapacity = (c.Capacity + 1);
405                                 Assert.Fail ("#C1");
406                         }
407                         catch (ArgumentOutOfRangeException ex) {
408                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
409                                 Assert.IsNull (ex.InnerException, "#C3");
410                                 Assert.IsNotNull (ex.Message, "#C4");
411                                 Assert.AreEqual ("value", ex.ParamName, "#C5");
412                         }
413                 }
414
415                 [Test]
416                 public void Add_LocalWithPort ()
417                 {
418                         CookieContainer cc = new CookieContainer ();
419                         var orig = new Cookie ("mycookie", "vv");
420                         cc.Add (new Uri ("http://localhost:8810/"), orig);
421                         var c = cc.GetCookies (new Uri ("http://localhost:8810/"))[0];
422                         Assert.AreEqual ("", c.Comment, "#1");
423                         Assert.IsNull (c.CommentUri, "#2");
424                         Assert.IsFalse (c.Discard, "#3");
425                         Assert.AreEqual ("localhost", c.Domain, "#4");
426                         Assert.IsFalse (c.Expired, "#5");
427                         Assert.AreEqual (DateTime.MinValue, c.Expires, "#6");
428                         Assert.IsFalse (c.HttpOnly, "#7");
429                         Assert.AreEqual ("mycookie", c.Name, "#8");
430                         Assert.AreEqual ("/", c.Path, "#9");
431                         Assert.AreEqual ("", c.Port, "#10");
432                         Assert.IsFalse (c.Secure, "#11");
433                         Assert.AreEqual ("vv", c.Value, "#13");
434                         Assert.AreEqual (0, c.Version, "#14");
435                         Assert.AreEqual ("mycookie=vv", c.ToString (), "#15");
436                 }
437
438                 [Test] // Add (Cookie)
439                 public void Add1 ()
440                 {
441                         CookieContainer cc = new CookieContainer ();
442                         Cookie cookie = new Cookie ("Age", "28", string.Empty, "localhost");
443                         Assert.AreEqual ("Age", cookie.Name, "Name");
444                         Assert.AreEqual ("28", cookie.Value, "Value");
445                         Assert.AreEqual (String.Empty, cookie.Path, "Path");
446                         Assert.AreEqual ("localhost", cookie.Domain, "Domain");
447                         // does not survive the addition "cloning"
448                         cookie.Comment = "comment";
449                         cookie.CommentUri = new Uri ("http://localhost");
450                         cookie.Discard = true;
451                         cookie.Expires = DateTime.MaxValue;
452                         cookie.HttpOnly = true;
453                         cookie.Secure = true;
454                         // except version
455 #if RFC2109
456                         cookie.Version = 1;
457 #endif
458
459                         cc.Add (cookie);
460                         Assert.AreEqual (1, cc.Count, "#A1");
461
462                         CookieCollection cookies = cc.GetCookies (new Uri ("https://localhost/Whatever"));
463                         Assert.AreEqual (1, cookies.Count, "#A2");
464                         Assert.AreNotSame (cookie, cookies [0], "!same");
465
466                         cookie = cookies [0];
467                         Assert.AreEqual ("Age", cookie.Name, "Clone-Name");
468                         Assert.AreEqual ("28", cookie.Value, "Clone-Value");
469                         Assert.AreEqual (string.Empty, cookie.Path, "Clone-Path");
470                         Assert.AreEqual ("localhost", cookie.Domain, "Clone-Domain");
471                         Assert.AreEqual ("comment", cookie.Comment, "Clone-Comment");
472                         Assert.AreEqual (new Uri ("http://localhost"), cookie.CommentUri, "Clone-CommentUri");
473                         Assert.IsTrue (cookie.Discard, "Clone-Discard");
474                         Assert.AreEqual (DateTime.MaxValue, cookie.Expires, "Clone-Expires");
475                         Assert.IsTrue (cookie.HttpOnly, "Clone-HttpOnly");
476                         Assert.IsTrue (cookie.Secure, "Clone-Secure");
477 #if RFC2109
478                         Assert.AreEqual (1, cookie.Version, "Clone-Version");
479 #else
480                         Assert.AreEqual (0, cookie.Version, "Clone-Version");
481 #endif
482
483                         cookies = cc.GetCookies (new Uri ("https://localhost/Whatever"));
484                         // the same Cookie instance returned for a second query
485                         Assert.AreSame (cookie, cookies [0], "!same-2");
486                 }
487
488                 [Test] // Add (Cookie)
489                 public void Add1_Domain_Empty ()
490                 {
491                         CookieContainer cc = new CookieContainer ();
492                         Cookie cookie = new Cookie ("Age", "28");
493                         try {
494                                 cc.Add (cookie);
495                                 Assert.Fail ("#1");
496                         }
497                         catch (ArgumentException ex) {
498                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
499                                 Assert.IsNull (ex.InnerException, "#3");
500                                 Assert.IsNotNull (ex.Message, "#4");
501                                 Assert.AreEqual ("cookie.Domain", ex.ParamName, "#5");
502                         }
503                 }
504
505                 [Test]
506                 public void Add_Domain_Invalid ()
507                 {
508                         var cc = new CookieContainer ();
509                         try {
510                                 cc.Add (new Cookie ("Foo", "Bar", string.Empty, ".com"));
511                                 Assert.Fail ("#A1");
512                         } catch (CookieException) {
513                                 ;
514                         }
515                         
516                         try {
517                                 cc.Add (new Uri ("http://mono.com/"), new Cookie ("Foo", "Bar", string.Empty, ".evil.org"));
518                                 Assert.Fail ("#A2");
519                         } catch (CookieException) {
520                                 ;
521                         }
522                 }
523
524                 [Test] // Add (CookieCollection)
525                 public void Add2_Cookies_Null ()
526                 {
527                         CookieContainer cc = new CookieContainer ();
528                         try {
529                                 cc.Add ((CookieCollection) null);
530                                 Assert.Fail ("#1");
531                         }
532                         catch (ArgumentNullException ex) {
533                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
534                                 Assert.IsNull (ex.InnerException, "#3");
535                                 Assert.IsNotNull (ex.Message, "#4");
536                                 Assert.AreEqual ("cookies", ex.ParamName, "#5");
537                         }
538                 }
539
540                 [Test] // Add (Uri, Cookie)
541                 public void Add3_Uri_Null ()
542                 {
543                         CookieContainer cc = new CookieContainer ();
544                         Cookie cookie = new Cookie ("Age", "28", "", "localhost");
545                         try {
546                                 cc.Add ((Uri) null, cookie);
547                                 Assert.Fail ("#1");
548                         }
549                         catch (ArgumentNullException ex) {
550                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
551                                 Assert.IsNull (ex.InnerException, "#3");
552                                 Assert.IsNotNull (ex.Message, "#4");
553                                 Assert.AreEqual ("uri", ex.ParamName, "#5");
554                         }
555                 }
556
557                 [Test] // Add (Uri, Cookie)
558                 public void Add3_Cookie_Null ()
559                 {
560                         CookieContainer cc = new CookieContainer ();
561                         Uri uri = new Uri ("http://www.contoso.com");
562                         try {
563                                 cc.Add (uri, (Cookie) null);
564                                 Assert.Fail ("#1");
565                         }
566                         catch (ArgumentNullException ex) {
567                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
568                                 Assert.IsNull (ex.InnerException, "#3");
569                                 Assert.IsNotNull (ex.Message, "#4");
570                                 Assert.AreEqual ("cookie", ex.ParamName, "#5");
571                         }
572                 }
573
574                 [Test] // Add (Uri, CookieCollection)
575                 public void Add4_Uri_Null ()
576                 {
577                         CookieContainer cc = new CookieContainer ();
578                         CookieCollection cookies = new CookieCollection ();
579                         try {
580                                 cc.Add ((Uri) null, cookies);
581                                 Assert.Fail ("#1");
582                         }
583                         catch (ArgumentNullException ex) {
584                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
585                                 Assert.IsNull (ex.InnerException, "#3");
586                                 Assert.IsNotNull (ex.Message, "#4");
587                                 Assert.AreEqual ("uri", ex.ParamName, "#5");
588                         }
589                 }
590
591                 [Test] // Add (Uri, CookieCollection)
592                 public void Add4_Cookie_Null ()
593                 {
594                         CookieContainer cc = new CookieContainer ();
595                         Uri uri = new Uri ("http://www.contoso.com");
596                         try {
597                                 cc.Add (uri, (CookieCollection) null);
598                                 Assert.Fail ("#1");
599                         }
600                         catch (ArgumentNullException ex) {
601                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
602                                 Assert.IsNull (ex.InnerException, "#3");
603                                 Assert.IsNotNull (ex.Message, "#4");
604                                 Assert.AreEqual ("cookies", ex.ParamName, "#5");
605                         }
606                 }
607
608                 /*
609                  * This test demonstrates one of the new, fixed behaviors in .NET 4.5:
610                  *
611                  * The cookie domain implicitly assumes a leading dot, so "x.y" now matches
612                  * "foo.x.y" and "foo.bar.x.y".
613                  *
614                  */
615                 [Test]
616                 public void Add5_Subdomain ()
617                 {
618                         var cc = new CookieContainer ();
619                         var cookie = new Cookie ("Foo", "Bar", string.Empty, "mono.com");
620                         cc.Add (cookie);
621
622                         var coll = cc.GetCookies (new Uri ("http://www.mono.com/Whatever/"));
623                         Assert.AreEqual (1, coll.Count, "#A1");
624
625                         var coll2 = cc.GetCookies (new Uri ("http://www.us.mono.com/Whatever/"));
626                         Assert.AreEqual (1, coll.Count, "#A2");
627                 }
628
629                 public void Add6_Insecure ()
630                 {
631                         var cc = new CookieContainer ();
632                         var cookie = new Cookie ("Foo", "Bar", string.Empty, ".mono.com");
633                         cookie.Secure = true;
634                         // FIXME: This should throw an exception - but .NET 4.5 does not.
635                         cc.Add (new Uri ("http://mono.com/"), cookie);
636                                 
637                         var coll = cc.GetCookies (new Uri ("http://mono.com/"));
638                         Assert.AreEqual (0, coll.Count, "#A1");
639                 }
640
641                 [Test]
642                 public void TestAdd_Cookie ()
643                 {
644                         CookieContainer cc = new CookieContainer ();
645                         Uri uri = new Uri ("http://www.contoso.com");
646                         cc.Add (uri, new CookieCollection ());
647                         DateTime timestamp = DateTime.Now;
648                         cc.Add (uri, new Cookie ("hola", "Adios"));
649                         CookieCollection coll = cc.GetCookies (uri);
650                         Cookie cookie = coll [0];
651                         Assert.AreEqual ("", cookie.Comment, "#1");
652                         Assert.IsNull (cookie.CommentUri, "#2");
653                         Assert.AreEqual ("www.contoso.com", cookie.Domain, "#3");
654                         Assert.IsFalse (cookie.Expired, "#4");
655                         Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#5");
656                         Assert.AreEqual ("hola", cookie.Name, "#6");
657                         Assert.AreEqual ("/", cookie.Path, "#7");
658                         Assert.AreEqual ("", cookie.Port, "#8");
659                         Assert.IsFalse (cookie.Secure, "#9");
660                         // FIX the next test
661                         TimeSpan ts = cookie.TimeStamp - timestamp;
662                         if (ts.TotalMilliseconds > 500)
663                                 Assert.Fail ("#10");
664
665                         Assert.AreEqual ("Adios", cookie.Value, "#11");
666                         Assert.AreEqual (0, cookie.Version, "#12");
667                 }
668
669                 [Test]
670                 //              [Category ("NotWorking")]
671                 public void TestAddExpired_Cookie ()
672                 {
673                         CookieContainer cc = new CookieContainer ();
674                         Uri uri = new Uri ("http://www.contoso.com");
675                         DateTime expires = DateTime.Now.Subtract (new TimeSpan (1, 30, 0));
676
677                         //expired cookie
678                         Cookie c1 = new Cookie ("TEST", "MyValue", "/", uri.Host);
679                         c1.Expires = expires;
680                         cc.Add (c1);
681                         Assert.AreEqual (0, cc.Count, "#A1");
682                         CookieCollection coll = cc.GetCookies (uri);
683                         Assert.AreEqual (0, coll.Count, "#A1.1");
684
685                         //expired cookie
686                         Cookie c2 = new Cookie ("TEST2", "MyValue2");
687                         c2.Expires = expires;
688                         cc.Add (uri, c2);
689                         Assert.AreEqual (0, cc.Count, "#B1");
690
691                         //not expired cookie
692                         Cookie c3 = new Cookie ("TEST3", "MyValue3");
693                         cc.Add (uri, c3);
694                         Assert.AreEqual (1, cc.Count, "#C1");
695
696                         //not expired cookie
697                         Cookie c4 = new Cookie ("TEST4", "MyValue4", "/", ".contoso.com");
698                         cc.Add (uri, c4);
699                         Assert.AreEqual (2, cc.Count, "#E1");
700                         coll = cc.GetCookies (uri);
701                         Assert.AreEqual (2, coll.Count, "#E1.1");
702
703                         //expired cookie
704                         Cookie c5 = new Cookie ("TEST5", "MyValue5", "/", ".contoso.com");
705                         c5.Expires = expires;
706                         cc.Add (c5);
707                         Assert.AreEqual (2, cc.Count, "#F1");
708                         coll = cc.GetCookies (uri);
709                         Assert.AreEqual (2, coll.Count, "#F1.1");
710                         Assert.IsNull (coll ["TEST5"], "#F2");
711
712                         //expired cookie
713                         Cookie c6 = new Cookie ("TEST6", "MyValue6", "/", ".contoso.com");
714                         c6.Expires = expires;
715                         cc.Add (uri, c6);
716                         Assert.AreEqual (2, cc.Count, "#G1");
717                         coll = cc.GetCookies (uri);
718                         Assert.AreEqual (2, coll.Count, "#G1.1");
719                         Assert.IsNull (coll ["TEST6"], "#G2");
720                 }
721
722                 [Test]
723                 public void GetCookieHeader1 ()
724                 {
725                         CookieContainer cc;
726                         Cookie cookie;
727
728                         cc = new CookieContainer ();
729                         cookie = new Cookie ("name1", "value1", "/path", "localhost");
730                         cookie.Comment = "Short name";
731                         cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
732                         cookie.Version = 0;
733                         cc.Add (cookie);
734 #if RFC2109
735                         cookie = new Cookie ("name2", "value2", "/path/sub", "localhost");
736                         cookie.Comment = "Description";
737                         cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
738                         cookie.Version = 1;
739                         cc.Add (cookie);
740                         Assert.AreEqual ("$Version=1; name2=value2; $Path=/path/sub; name1=value1", cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#A1");
741 #endif
742                         Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://localhost/path")), "#A2");
743                         Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://localhost/whatever")), "#A3");
744                 }
745
746                 [Test]
747                 public void GetCookieHeader2a ()
748                 {
749                         CookieContainer cc = new CookieContainer ();
750                         Cookie cookie = new Cookie ("Country", "Belgium", "/path", "mono.com");
751                         cc.Add (cookie);
752                         cookie = new Cookie ("Age", "26", "/path", "dev.mono.com");
753                         cc.Add (cookie);
754
755                         Assert.AreEqual ("Age=26; Country=Belgium", cc.GetCookieHeader (new Uri ("http://dev.mono.com/path/ok")), "#A1");
756                         Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://mono.com/path")), "#A2");
757                         Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://test.mono.com/path")), "#A3");
758                         Assert.AreEqual ("Age=26; Country=Belgium", cc.GetCookieHeader (new Uri ("http://us.dev.mono.com/path")), "#A4");
759                 }
760
761                 [Test]
762                 public void GetCookieHeader2b ()
763                 {
764                         CookieContainer cc = new CookieContainer ();
765                         Cookie cookie = new Cookie ("Country", "Belgium", "/path", ".mono.com");
766                         cc.Add (cookie);
767                         cookie = new Cookie ("Age", "26", "/path", ".dev.mono.com");
768                         cc.Add (cookie);
769
770                         Assert.AreEqual ("Age=26; Country=Belgium", cc.GetCookieHeader (new Uri ("http://dev.mono.com/path/ok")), "#C1");
771                         Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://mono.com/path")), "#C2");
772                         Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://test.mono.com/path")), "#C3");
773                         Assert.AreEqual ("Age=26; Country=Belgium", cc.GetCookieHeader (new Uri ("http://us.dev.mono.com/path")), "#C4");
774                 }
775
776                 [Test]
777                 public void GetCookieHeader3 ()
778                 {
779                         CookieContainer cc = new CookieContainer ();
780                         cc.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do"),
781                                 "Country=Belgium; path=/Whatever; domain=mono.com;" +
782                                 "Age=26; path=/Whatever; domain=test.mono.com," +
783                                 "Weight=87; path=/Whatever/Do; domain=.mono.com");
784                         Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://dev.mono.com/Whatever/Do")), "#C1");
785                         Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://test.mono.com/Whatever/Do")), "#C2");
786                         Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://mono.com/Whatever/Do")), "#C3");
787                         Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://us.test.mono.com/Whatever/Do")), "#C4");
788                 }
789
790                 [Test]
791                 public void GetCookieHeader4 ()
792                 {
793                         CookieContainer cc = new CookieContainer ();
794                         Cookie cookie = new Cookie ("Height", "178", "/Whatever", "mono.com");
795                         cc.Add (cookie);
796                         cookie = new Cookie ("Town", "Brussels", "/Whatever", ".mono.com");
797                         cc.Add (cookie);
798                         cookie = new Cookie ("Income", "34445", "/Whatever/", ".test.mono.com");
799                         cc.Add (cookie);
800                         cookie = new Cookie ("Sex", "Male", "/WhateveR/DO", ".test.mono.com");
801                         cc.Add (cookie);
802                         cc.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do/You"),
803                                 "Country=Belgium," +
804                                 "Age=26; path=/Whatever/Do; domain=test.mono.com," +
805                                 "Weight=87; path=/");
806                         Assert.AreEqual ("Age=26; Income=34445; Height=178; Town=Brussels",
807                                 cc.GetCookieHeader (new Uri ("http://us.test.mono.com/Whatever/Do/Ok")),
808                                 "#D");
809                 }
810
811 #if RFC2109
812                 [Test]
813                 public void GetCookieHeader5a ()
814                 {
815                         CookieContainer cc = new CookieContainer ();
816                         Cookie cookie = new Cookie ("name1", "value1", "", "localhost");
817                         cookie.Comment = "Short name";
818                         cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
819                         cookie.Version = 1;
820                         cc.Add (cookie);
821                         Assert.AreEqual ("$Version=1; name1=value1; $Domain=localhost", cookie.ToString (), "#E0");
822                         Assert.AreEqual ("$Version=1; name1=value1; $Path=/",
823                                 cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#E1");
824                 }
825
826                 [Test]
827                 public void GetCookieHeader5b ()
828                 {
829                         CookieContainer cc = new CookieContainer ();
830                         Cookie cookie = new Cookie ("name1", "value1");
831                         cookie.Domain = "localhost";
832                         cookie.Comment = "Short name";
833                         cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
834                         cookie.Version = 1;
835                         cc.Add (cookie);
836                         Assert.AreEqual ("$Version=1; name1=value1; $Domain=localhost", cookie.ToString (), "#E0");
837                         Assert.AreEqual ("$Version=1; name1=value1; $Path=/",
838                                 cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#E1");
839                 }
840 #endif
841
842                 [Test]
843                 public void GetCookieHeader6 ()
844                 {
845                         CookieContainer cc = new CookieContainer ();
846                         Cookie cookie = new Cookie ("name1", "value1", "", "localhost");
847                         cookie.Comment = "Short name";
848                         cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
849                         cookie.Version = 0;
850                         cc.Add (cookie);
851                         Assert.AreEqual ("name1=value1",
852                                 cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#E2");
853                 }
854
855                 [Test]
856                 public void GetCookieHeader7a ()
857                 {
858                         CookieContainer cc = new CookieContainer ();
859                         Cookie cookie = new Cookie ("name1", "value1", "/path", ".mono.com");
860                         cookie.Comment = "Short name";
861                         cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
862                         cookie.Version = 0;
863                         cc.Add (cookie);
864 #if RFC2109
865                         cookie = new Cookie ("name2", "value2", "/path/sub", ".mono.com");
866                         cookie.Comment = "Description";
867                         cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
868                         cookie.Version = 1;
869                         cc.Add (cookie);
870                         Assert.AreEqual ("$Version=1; name2=value2; $Path=/path/sub; $Domain=.mono.com; name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path/sub")), "#A1");
871 #endif
872                         Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path")), "#A2");
873                         Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://live.mono.com/whatever")), "#A3");
874                         Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://gomono.com/path/sub")), "#A4");
875                         Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://mono.com/path/sub")), "#A5");
876                 }
877
878                 [Test]
879                 public void GetCookieHeader7b ()
880                 {
881                         CookieContainer cc = new CookieContainer ();
882                         Cookie cookie = new Cookie ("name1", "value1", "/path", "live.mono.com");
883                         cookie.Comment = "Short name";
884                         cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
885                         cookie.Version = 0;
886                         cc.Add (cookie);
887 #if RFC2109
888                         cookie = new Cookie ("name2", "value2", "/path/sub", "live.mono.com");
889                         cookie.Comment = "Description";
890                         cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
891                         cookie.Version = 1;
892                         cc.Add (cookie);
893                         Assert.AreEqual ("$Version=1; name2=value2; $Path=/path/sub; name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path/sub")), "#B1");
894 #endif
895                         Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path")), "#B2");
896                         Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://live.mono.com/whatever")), "#B3");
897                         Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://go.live.mono.com/path/sub")), "#B4");
898                         Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://go.live.mono.com/path")), "#B5");
899                         Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://go.live.mono.com/whatever")), "#B6");
900                         Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://golive.mono.com/whatever")), "#B7");
901                 }
902
903                 [Test]
904                 public void GetCookieHeader_Uri_Null ()
905                 {
906                         CookieContainer cc = new CookieContainer ();
907                         try {
908                                 cc.GetCookieHeader ((Uri) null);
909                                 Assert.Fail ("#1");
910                         }
911                         catch (ArgumentNullException ex) {
912                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
913                                 Assert.IsNull (ex.InnerException, "#3");
914                                 Assert.IsNotNull (ex.Message, "#4");
915                                 Assert.AreEqual ("uri", ex.ParamName, "#5");
916                         }
917                 }
918
919                 [Test]
920                 public void GetCookies ()
921                 {
922                         CookieContainer container = new CookieContainer ();
923                         container.Add (new Cookie ("name", "value1", "/path", "localhost"));
924                         container.Add (new Cookie ("name", "value2", "/path/sub", "localhost"));
925
926                         CookieCollection cookies = container.GetCookies (
927                                 new Uri ("http://localhost/path/sub"));
928                         Assert.IsNotNull (cookies, "#A1");
929                         Assert.AreEqual (2, cookies.Count, "#A2");
930
931                         Cookie cookie = cookies [0];
932                         Assert.AreEqual ("name", cookie.Name, "#B1");
933                         Assert.AreEqual ("value2", cookie.Value, "#B2");
934                         Assert.AreEqual ("/path/sub", cookie.Path, "#B3");
935                         Assert.AreEqual ("localhost", cookie.Domain, "#B4");
936
937                         cookie = cookies [1];
938                         Assert.AreEqual ("name", cookie.Name, "#C1");
939                         Assert.AreEqual ("value1", cookie.Value, "#C2");
940                         Assert.AreEqual ("/path", cookie.Path, "#C3");
941                         Assert.AreEqual ("localhost", cookie.Domain, "#C4");
942
943                         cookies = container.GetCookies (new Uri ("http://localhost/path"));
944                         Assert.IsNotNull (cookies, "#D1");
945                         Assert.AreEqual (1, cookies.Count, "#D2");
946
947                         cookie = cookies [0];
948                         Assert.AreEqual ("name", cookie.Name, "#E1");
949                         Assert.AreEqual ("value1", cookie.Value, "#E2");
950                         Assert.AreEqual ("/path", cookie.Path, "#E3");
951                         Assert.AreEqual ("localhost", cookie.Domain, "#E4");
952
953                         cookies = container.GetCookies (new Uri ("http://localhost/whatever"));
954                         Assert.IsNotNull (cookies, "#F1");
955                         Assert.AreEqual (0, cookies.Count, "#F2");
956                 }
957
958                 [Test]
959                 public void GetCookies2a ()
960                 {
961                         CookieContainer container = new CookieContainer ();
962                         container.Add (new Cookie ("Country", "Belgium", "/path", "mono.com"));
963                         container.Add (new Cookie ("Age", "26", "/path", "dev.mono.com"));
964
965                         CookieCollection cookies = container.GetCookies (new Uri ("http://dev.mono.com/path/ok"));
966                         Assert.IsNotNull (cookies, "#G1");
967                         Assert.AreEqual (2, cookies.Count, "#G2");
968
969                         Cookie cookie = cookies [0];
970                         Assert.AreEqual ("Age", cookie.Name, "#H1");
971                         Assert.AreEqual ("26", cookie.Value, "#H2");
972                         Assert.AreEqual ("/path", cookie.Path, "#H3");
973                         Assert.AreEqual ("dev.mono.com", cookie.Domain, "#H4");
974
975                         cookies = container.GetCookies (new Uri ("http://mono.com/path"));
976                         Assert.IsNotNull (cookies, "#I1");
977                         Assert.AreEqual (1, cookies.Count, "#I2");
978
979                         cookie = cookies [0];
980                         Assert.AreEqual ("Country", cookie.Name, "#J1");
981                         Assert.AreEqual ("Belgium", cookie.Value, "#J2");
982                         Assert.AreEqual ("/path", cookie.Path, "#J3");
983                         Assert.AreEqual ("mono.com", cookie.Domain, "#J4");
984
985                         cookies = container.GetCookies (new Uri ("http://test.mono.com/path"));
986                         Assert.IsNotNull (cookies, "#K1");
987                         Assert.AreEqual (1, cookies.Count, "#K2");
988
989                         cookies = container.GetCookies (new Uri ("http://us.dev.mono.com/path"));
990                         Assert.IsNotNull (cookies, "#L1");
991                         Assert.AreEqual (2, cookies.Count, "#L2");
992                 }
993
994                 [Test]
995                 public void GetCookies2b ()
996                 {
997                         CookieContainer container = new CookieContainer ();
998                         container.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do"),
999                                 "Country=Belgium; path=/Whatever; domain=mono.com," +
1000                                 "Age=26; path=/Whatever; domain=test.mono.com," +
1001                                 "Weight=87; path=/Whatever/Do; domain=.mono.com;");
1002
1003                         CookieCollection cookies = container.GetCookies (new Uri ("http://dev.mono.com/Whatever/Do"));
1004                         Assert.IsNotNull (cookies, "#M1");
1005                         Assert.AreEqual (2, cookies.Count, "#M2");
1006
1007                         Cookie cookie = cookies [0];
1008                         Assert.AreEqual ("Weight", cookie.Name, "#N1");
1009                         Assert.AreEqual ("87", cookie.Value, "#N2");
1010                         Assert.AreEqual ("/Whatever/Do", cookie.Path, "#N3");
1011                         Assert.AreEqual (".mono.com", cookie.Domain, "#N4");
1012                         cookie = cookies [1];
1013                         Assert.AreEqual ("Country", cookie.Name, "#N5");
1014                         Assert.AreEqual ("Belgium", cookie.Value, "#N6");
1015                         Assert.AreEqual ("/Whatever", cookie.Path, "#N7");
1016                         Assert.AreEqual ("mono.com", cookie.Domain, "#N8");
1017
1018                         cookies = container.GetCookies (new Uri ("http://test.mono.com/Whatever/Do"));
1019                         Assert.IsNotNull (cookies, "#O1");
1020                         Assert.AreEqual (3, cookies.Count, "#O2");
1021
1022                         cookie = cookies [1];
1023                         Assert.AreEqual ("Weight", cookie.Name, "#P1");
1024                         Assert.AreEqual ("87", cookie.Value, "#P2");
1025                         Assert.AreEqual ("/Whatever/Do", cookie.Path, "#P3");
1026                         Assert.AreEqual (".mono.com", cookie.Domain, "#P4");
1027                         cookie = cookies [2];
1028                         Assert.AreEqual ("Country", cookie.Name, "#P5");
1029                         Assert.AreEqual ("Belgium", cookie.Value, "#P6");
1030                         Assert.AreEqual ("/Whatever", cookie.Path, "#P7");
1031                         Assert.AreEqual ("mono.com", cookie.Domain, "#P8");
1032
1033                         cookies = container.GetCookies (new Uri ("http://mono.com/Whatever/Do"));
1034                         Assert.IsNotNull (cookies, "#Q1");
1035                         Assert.AreEqual (2, cookies.Count, "#Q2");
1036
1037                         cookies = container.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do"));
1038                         Assert.IsNotNull (cookies, "#R1");
1039                         Assert.AreEqual (3, cookies.Count, "#R2");
1040
1041                         cookie = cookies [0];
1042                         Assert.AreEqual ("Age", cookie.Name, "#S1");
1043                         Assert.AreEqual ("26", cookie.Value, "#S2");
1044                         Assert.AreEqual ("/Whatever", cookie.Path, "#S3");
1045                         Assert.AreEqual ("test.mono.com", cookie.Domain, "#S4");
1046                         cookie = cookies [1];
1047                         Assert.AreEqual ("Weight", cookie.Name, "#S5");
1048                         Assert.AreEqual ("87", cookie.Value, "#S6");
1049                         Assert.AreEqual ("/Whatever/Do", cookie.Path, "#S7");
1050                         Assert.AreEqual (".mono.com", cookie.Domain, "#S8");
1051                         cookie = cookies [2];
1052                         Assert.AreEqual ("Country", cookie.Name, "#S9");
1053                         Assert.AreEqual ("Belgium", cookie.Value, "#S10");
1054                         Assert.AreEqual ("/Whatever", cookie.Path, "#S11");
1055                         Assert.AreEqual ("mono.com", cookie.Domain, "#S12");
1056                 }
1057
1058                 [Test]
1059                 public void GetCookies2c ()
1060                 {
1061                         CookieContainer container = new CookieContainer ();
1062                         container.Add (new Cookie ("Height", "178", "/Whatever", "mono.com"));
1063                         container.Add (new Cookie ("Town", "Brussels", "/Whatever", ".mono.com"));
1064                         container.Add (new Cookie ("Income", "34445", "/Whatever/", ".test.mono.com"));
1065                         container.Add (new Cookie ("Sex", "Male", "/WhateveR/DO", ".test.mono.com"));
1066                         container.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do/You"),
1067                                 "Country=Belgium," +
1068                                 "Age=26; path=/Whatever/Do; domain=test.mono.com," +
1069                                 "Weight=87; path=/");
1070
1071                         CookieCollection cookies = container.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do/Ok"));
1072                         Assert.IsNotNull (cookies, "#T1");
1073                         Assert.AreEqual (4, cookies.Count, "#T2");
1074
1075                         Cookie cookie = cookies [0];
1076                         Assert.AreEqual ("Age", cookie.Name, "#U1");
1077                         Assert.AreEqual ("26", cookie.Value, "#U2");
1078                         Assert.AreEqual ("/Whatever/Do", cookie.Path, "#U3");
1079                         Assert.AreEqual ("test.mono.com", cookie.Domain, "#U4");
1080                         cookie = cookies [1];
1081                         Assert.AreEqual ("Income", cookie.Name, "#U5");
1082                         Assert.AreEqual ("34445", cookie.Value, "#U6");
1083                         Assert.AreEqual ("/Whatever/", cookie.Path, "#U7");
1084                         Assert.AreEqual (".test.mono.com", cookie.Domain, "#U8");
1085                         cookie = cookies [3];
1086                         Assert.AreEqual ("Town", cookie.Name, "#U9");
1087                         Assert.AreEqual ("Brussels", cookie.Value, "#U10");
1088                         Assert.AreEqual ("/Whatever", cookie.Path, "#U11");
1089                         Assert.AreEqual (".mono.com", cookie.Domain, "#U12");
1090                 }
1091
1092                 [Test]
1093                 public void GetCookies_Uri_Null ()
1094                 {
1095                         CookieContainer cc = new CookieContainer ();
1096                         try {
1097                                 cc.GetCookies (null);
1098                                 Assert.Fail ("#1");
1099                         }
1100                         catch (ArgumentNullException ex) {
1101                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1102                                 Assert.IsNull (ex.InnerException, "#3");
1103                                 Assert.IsNotNull (ex.Message, "#4");
1104                                 Assert.AreEqual ("uri", ex.ParamName, "#5");
1105                         }
1106                 }
1107
1108                 [Test]
1109                 //              [Category ("NotWorking")]
1110                 public void SetCookies ()
1111                 {
1112                         Uri uri = new Uri ("http://dev.test.mono.com/Whatever/Do/You");
1113
1114                         DateTime now = DateTime.Now;
1115
1116                         CookieContainer cc = new CookieContainer ();
1117                         cc.SetCookies (uri, "Country=Belgium," +
1118                                 "Age=26;   ; path=/Whatever/Do; domain=test.mono.com," +
1119                                 "Weight=87; path=/; ");
1120                         Assert.AreEqual (3, cc.Count, "#A");
1121
1122                         CookieCollection cookies = cc.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do/Ok"));
1123                         Assert.IsNotNull (cookies, "#B1");
1124                         Assert.AreEqual (1, cookies.Count, "#B2");
1125
1126                         Cookie cookie = cookies [0];
1127                         Assert.AreEqual (string.Empty, cookie.Comment, "#C:Comment");
1128                         Assert.IsNull (cookie.CommentUri, "#C:CommentUri");
1129                         Assert.IsFalse (cookie.Discard, "#C:Discard");
1130                         Assert.AreEqual ("test.mono.com", cookie.Domain, "#C:Domain");
1131                         Assert.IsFalse (cookie.Expired, "#C:Expired");
1132                         Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#C:Expires");
1133                         Assert.IsFalse (cookie.HttpOnly, "#C:HttpOnly");
1134                         Assert.AreEqual ("Age", cookie.Name, "#C:Name");
1135                         Assert.AreEqual ("/Whatever/Do", cookie.Path, "#C:Path");
1136                         Assert.IsFalse (cookie.Secure, "#C:Secure");
1137                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#C:TimeStamp1");
1138                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#C:TimeStamp2");
1139                         Assert.AreEqual ("26", cookie.Value, "#C:Value");
1140                         Assert.AreEqual (0, cookie.Version, "#C:Version");
1141
1142                         cookies = cc.GetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do/Ok"));
1143                         Assert.IsNotNull (cookies, "#D1");
1144                         Assert.AreEqual (2, cookies.Count, "#D2");
1145
1146                         // our sorting is not 100% identical to MS implementation
1147                         for (int i = 0; i < cookies.Count; i++) {
1148                                 cookie = cookies [i];
1149                                 switch (cookie.Name) {
1150                                 case "Weight":
1151                                         Assert.AreEqual (string.Empty, cookie.Comment, "#E:Comment");
1152                                         Assert.IsNull (cookie.CommentUri, "#E:CommentUri");
1153                                         Assert.IsFalse (cookie.Discard, "#E:Discard");
1154                                         Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#E:Domain");
1155                                         Assert.IsFalse (cookie.Expired, "#E:Expired");
1156                                         Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#E:Expires");
1157                                         Assert.IsFalse (cookie.HttpOnly, "#E:HttpOnly");
1158                                         Assert.AreEqual ("Weight", cookie.Name, "#E:Name");
1159                                         Assert.AreEqual ("/", cookie.Path, "#E:Path");
1160                                         Assert.IsFalse (cookie.Secure, "#E:Secure");
1161                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#E:TimeStamp1");
1162                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#E:TimeStamp2");
1163                                         Assert.AreEqual ("87", cookie.Value, "#E:Value");
1164                                         Assert.AreEqual (0, cookie.Version, "#E:Version");
1165                                         break;
1166                                 case "Age":
1167                                         Assert.AreEqual (string.Empty, cookie.Comment, "#F:Comment");
1168                                         Assert.IsNull (cookie.CommentUri, "#F:CommentUri");
1169                                         Assert.IsFalse (cookie.Discard, "#F:Discard");
1170                                         Assert.AreEqual ("test.mono.com", cookie.Domain, "#F:Domain");
1171                                         Assert.IsFalse (cookie.Expired, "#F:Expired");
1172                                         Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#F:Expires");
1173                                         Assert.IsFalse (cookie.HttpOnly, "#F:HttpOnly");
1174                                         Assert.AreEqual ("Age", cookie.Name, "#F:Name");
1175                                         Assert.AreEqual ("/Whatever/Do", cookie.Path, "#F:Path");
1176                                         Assert.IsFalse (cookie.Secure, "#F:Secure");
1177                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#F:TimeStamp1");
1178                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#F:TimeStamp2");
1179                                         Assert.AreEqual ("26", cookie.Value, "#F:Value");
1180                                         Assert.AreEqual (0, cookie.Version, "#F:Version");
1181                                         break;
1182                                 default:
1183                                         Assert.Fail (cookie.Name);
1184                                         break;
1185                                 }
1186                         }
1187
1188                         cookies = cc.GetCookies (uri);
1189                         Assert.IsNotNull (cookies, "#G1");
1190                         Assert.AreEqual (3, cookies.Count, "#G2");
1191
1192                         // our sorting is not 100% identical to MS implementation
1193                         for (int i = 0; i < cookies.Count; i++) {
1194                                 cookie = cookies [i];
1195                                 switch (cookie.Name) {
1196                                 case "Country":
1197                                         Assert.AreEqual (string.Empty, cookie.Comment, "#H:Comment");
1198                                         Assert.IsNull (cookie.CommentUri, "#H:CommentUri");
1199                                         Assert.IsFalse (cookie.Discard, "#H:Discard");
1200                                         Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#H:Domain");
1201                                         Assert.IsFalse (cookie.Expired, "#H:Expired");
1202                                         Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#H:Expires");
1203                                         Assert.IsFalse (cookie.HttpOnly, "#H:HttpOnly");
1204                                         Assert.AreEqual ("Country", cookie.Name, "#H:Name");
1205                                         Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#H:Path");
1206                                         Assert.IsFalse (cookie.Secure, "#H:Secure");
1207                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#H:TimeStamp1");
1208                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#H:TimeStamp2");
1209                                         Assert.AreEqual ("Belgium", cookie.Value, "#H:Value");
1210                                         Assert.AreEqual (0, cookie.Version, "#H:Version");
1211                                         break;
1212                                 case "Weight":
1213                                         Assert.AreEqual (string.Empty, cookie.Comment, "#I:Comment");
1214                                         Assert.IsNull (cookie.CommentUri, "#I:CommentUri");
1215                                         Assert.IsFalse (cookie.Discard, "#I:Discard");
1216                                         Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#I:Domain");
1217                                         Assert.IsFalse (cookie.Expired, "#I:Expired");
1218                                         Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#I:Expires");
1219                                         Assert.IsFalse (cookie.HttpOnly, "#I:HttpOnly");
1220                                         Assert.AreEqual ("Weight", cookie.Name, "#I:Name");
1221                                         Assert.AreEqual ("/", cookie.Path, "#I:Path");
1222                                         Assert.IsFalse (cookie.Secure, "#I:Secure");
1223                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#I:TimeStamp1");
1224                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#I:TimeStamp2");
1225                                         Assert.AreEqual ("87", cookie.Value, "#I:Value");
1226                                         Assert.AreEqual (0, cookie.Version, "#I:Version");
1227                                         break;
1228                                 case "Age":
1229                                         Assert.AreEqual (string.Empty, cookie.Comment, "#J:Comment");
1230                                         Assert.IsNull (cookie.CommentUri, "#J:CommentUri");
1231                                         Assert.IsFalse (cookie.Discard, "#J:Discard");
1232                                         Assert.AreEqual ("test.mono.com", cookie.Domain, "#J:Domain");
1233                                         Assert.IsFalse (cookie.Expired, "#J:Expired");
1234                                         Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#J:Expires");
1235                                         Assert.IsFalse (cookie.HttpOnly, "#J:HttpOnly");
1236                                         Assert.AreEqual ("Age", cookie.Name, "#J:Name");
1237                                         Assert.AreEqual ("/Whatever/Do", cookie.Path, "#J:Path");
1238                                         Assert.IsFalse (cookie.Secure, "#J:Secure");
1239                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#J:TimeStamp1");
1240                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#J:TimeStamp2");
1241                                         Assert.AreEqual ("26", cookie.Value, "#J:Value");
1242                                         Assert.AreEqual (0, cookie.Version, "#J:Version");
1243                                         break;
1244                                 default:
1245                                         Assert.Fail (cookie.Name);
1246                                         break;
1247                                 }
1248                         }
1249
1250                         cc.SetCookies (uri, "Country=,A");
1251                         cookies = cc.GetCookies (uri);
1252                         Assert.IsNotNull (cookies, "#K1");
1253                         Assert.AreEqual (4, cookies.Count, "#K2");
1254
1255                         cc = new CookieContainer ();
1256                         cc.SetCookies (uri, "Country=,A");
1257                         cookies = cc.GetCookies (uri);
1258                         Assert.IsNotNull (cookies, "#L1");
1259                         Assert.AreEqual (2, cookies.Count, "#L2");
1260
1261                         // our sorting is not 100% identical to MS implementation
1262                         for (int i = 0; i < cookies.Count; i++) {
1263                                 cookie = cookies [i];
1264                                 switch (cookie.Name) {
1265                                 case "Country":
1266                                         Assert.AreEqual (string.Empty, cookie.Comment, "#M:Comment");
1267                                         Assert.IsNull (cookie.CommentUri, "#M:CommentUri");
1268                                         Assert.IsFalse (cookie.Discard, "#M:Discard");
1269                                         Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#M:Domain");
1270                                         Assert.IsFalse (cookie.Expired, "#M:Expired");
1271                                         Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#M:Expires");
1272                                         Assert.IsFalse (cookie.HttpOnly, "#M:HttpOnly");
1273                                         Assert.AreEqual ("Country", cookie.Name, "#M:Name");
1274                                         Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#M:Path");
1275                                         Assert.IsFalse (cookie.Secure, "#M:Secure");
1276                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#M:TimeStamp1");
1277                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#M:TimeStamp2");
1278                                         Assert.AreEqual (string.Empty, cookie.Value, "#M:Value");
1279                                         Assert.AreEqual (0, cookie.Version, "#M:Version");
1280                                         break;
1281                                 case "A":
1282                                         Assert.AreEqual (string.Empty, cookie.Comment, "#N:Comment");
1283                                         Assert.IsNull (cookie.CommentUri, "#N:CommentUri");
1284                                         Assert.IsFalse (cookie.Discard, "#N:Discard");
1285                                         Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#N:Domain");
1286                                         Assert.IsFalse (cookie.Expired, "#N:Expired");
1287                                         Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#N:Expires");
1288                                         Assert.IsFalse (cookie.HttpOnly, "#N:HttpOnly");
1289                                         Assert.AreEqual ("A", cookie.Name, "#N:Name");
1290                                         Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#N:Path");
1291                                         Assert.IsFalse (cookie.Secure, "#N:Secure");
1292                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#N:TimeStamp1");
1293                                         Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#N:TimeStamp2");
1294                                         Assert.AreEqual (string.Empty, cookie.Value, "#N:Value");
1295                                         Assert.AreEqual (0, cookie.Version, "#N:Version");
1296                                         break;
1297                                 default:
1298                                         Assert.Fail (cookie.Name);
1299                                         break;
1300                                 }
1301                         }
1302                 }
1303
1304                 [Test]
1305                 public void SetCookies_CookieHeader_Empty ()
1306                 {
1307                         CookieContainer cc = new CookieContainer ();
1308                         cc.SetCookies (new Uri ("http://www.contoso.com"), string.Empty);
1309                         Assert.AreEqual (0, cc.Count);
1310                 }
1311
1312                 [Test]
1313                 public void SetCookies_CookieHeader_Null ()
1314                 {
1315                         CookieContainer cc = new CookieContainer ();
1316                         try {
1317                                 cc.SetCookies (new Uri ("http://www.contoso.com"), null);
1318                                 Assert.Fail ("#1");
1319                         }
1320                         catch (ArgumentNullException ex) {
1321                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1322                                 Assert.IsNull (ex.InnerException, "#3");
1323                                 Assert.IsNotNull (ex.Message, "#4");
1324                                 Assert.AreEqual ("cookieHeader", ex.ParamName, "#5");
1325                         }
1326                 }
1327
1328                 [Test]
1329                 public void SetCookies_CookieHeader_Invalid_1 ()
1330                 {
1331                         // cookie format error
1332                         CookieContainer cc = new CookieContainer ();
1333                         try {
1334                                 cc.SetCookies (new Uri ("http://www.contoso.com"), "=lalala");
1335                                 Assert.Fail ("#A1");
1336                         }
1337                         catch (CookieException ex) {
1338                                 // An error has occurred when parsing Cookie
1339                                 // header for Uri 'http://www.contoso.com/'
1340                                 Assert.AreEqual (typeof (CookieException), ex.GetType (), "#A2");
1341                                 Assert.IsNotNull (ex.InnerException, "#A3");
1342                                 Assert.IsNotNull (ex.Message, "#A4");
1343                                 Assert.IsTrue (ex.Message.IndexOf ("http://www.contoso.com/") != -1, "#A5");
1344
1345                                 // Cookie format error
1346                                 CookieException inner = ex.InnerException as CookieException;
1347                                 Assert.AreEqual (typeof (CookieException), inner.GetType (), "#A6");
1348                                 Assert.IsNull (inner.InnerException, "#A7");
1349                                 Assert.IsNotNull (inner.Message, "#A8");
1350                         }
1351                 }
1352
1353                 [Test]
1354                 public void SetCookies_CookieHeader_Invalid_2 ()
1355                 {
1356                         // cookie path not part of URI path
1357                         CookieContainer cc = new CookieContainer ();
1358                         try {
1359                                 cc.SetCookies (new Uri ("http://dev.test.mono.com/Whatever"),
1360                                         "Age=26; path=/Whatever/Do; domain=test.mono.com");
1361                                 Assert.Fail ("#B1");
1362                         }
1363                         catch (CookieException ex) {
1364                                 // An error has occurred when parsing Cookie
1365                                 // header for Uri 'http://dev.test.mono.com/Whatever'
1366                                 Assert.AreEqual (typeof (CookieException), ex.GetType (), "#B2");
1367                                 Assert.IsNotNull (ex.InnerException, "#B3");
1368                                 Assert.IsNotNull (ex.Message, "#B4");
1369                                 Assert.IsTrue (ex.Message.IndexOf ("http://dev.test.mono.com/Whatever") != -1, "#B5");
1370
1371                                 // The 'Path'='/Whatever/Do' part of the Cookie
1372                                 // is invalid
1373                                 CookieException inner = ex.InnerException as CookieException;
1374                                 Assert.AreEqual (typeof (CookieException), inner.GetType (), "#B6");
1375                                 Assert.IsNull (inner.InnerException, "#B7");
1376                                 Assert.IsNotNull (inner.Message, "#B8");
1377                                 Assert.IsTrue (inner.Message.IndexOf ("'Path'='/Whatever/Do'") != -1 ||
1378                                                inner.Message.IndexOf ("\"Path\"=\"/Whatever/Do\"") != 1, "#B9");
1379                         }
1380                 }
1381
1382                 [Test]
1383                 public void SetCookies_Uri_Null ()
1384                 {
1385                         CookieContainer cc = new CookieContainer ();
1386                         try {
1387                                 cc.SetCookies (null, "Age=26; path=/Whatever; domain=test.mono.com");
1388                                 Assert.Fail ("#1");
1389                         }
1390                         catch (ArgumentNullException ex) {
1391                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1392                                 Assert.IsNull (ex.InnerException, "#3");
1393                                 Assert.IsNotNull (ex.Message, "#4");
1394                                 Assert.AreEqual ("uri", ex.ParamName, "#5");
1395                         }
1396                 }
1397
1398                 [Test]
1399                 public void SetCookies_DomainMatchesHost ()
1400                 {
1401                         CookieContainer cc = new CookieContainer ();
1402                         // domains looks identical - but "domain=test.mono.com" means "*.test.mono.com"
1403                         cc.SetCookies (new Uri ("http://test.mono.com/Whatever/Do"),
1404                                 "Age=26; path=/Whatever; domain=test.mono.com");
1405                         CookieCollection cookies = cc.GetCookies (new Uri ("http://test.mono.com/Whatever/Do"));
1406                         Assert.IsNotNull (cookies, "#A1");
1407                         Assert.AreEqual (1, cookies.Count, "#A2");
1408                         cookies = cc.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do"));
1409                         Assert.IsNotNull (cookies, "#A3");
1410                         Assert.AreEqual (1, cookies.Count, "#A4");
1411                 }
1412
1413 #if FIXME
1414                 [Test]
1415                 public void SetCookies_Domain_Local ()
1416                 {
1417                         CookieContainer cc;
1418                         CookieCollection cookies;
1419                         string hostname = Dns.GetHostName ();
1420
1421                         cc = new CookieContainer ();
1422                         cc.SetCookies (new Uri ("http://localhost/Whatever/Do"),
1423                                 "Age=26; path=/Whatever; domain=.local");
1424                         cookies = cc.GetCookies (new Uri ("http://localhost/Whatever/Do"));
1425                         Assert.IsNotNull (cookies, "#A1");
1426                         Assert.AreEqual (0, cookies.Count, "#A2");
1427                         cookies = cc.GetCookies (new Uri ("http://127.0.0.1/Whatever/Do"));
1428                         Assert.IsNotNull (cookies, "#A3");
1429                         Assert.AreEqual (0, cookies.Count, "#A4");
1430                         cookies = cc.GetCookies (new Uri ("http://" + hostname + "/Whatever/Do"));
1431                         Assert.IsNotNull (cookies, "#A5");
1432                         Assert.AreEqual (hostname.EndsWith (".local") ? 1 : 0, cookies.Count, "#A6");
1433
1434                         cc = new CookieContainer ();
1435                         cc.SetCookies (new Uri ("http://127.0.0.1/Whatever/Do"),
1436                                 "Age=26; path=/Whatever; domain=.local");
1437                         cookies = cc.GetCookies (new Uri ("http://localhost/Whatever/Do"));
1438                         Assert.IsNotNull (cookies, "#B1");
1439                         Assert.AreEqual (0, cookies.Count, "#B2");
1440                         cookies = cc.GetCookies (new Uri ("http://127.0.0.1/Whatever/Do"));
1441                         Assert.IsNotNull (cookies, "#B3");
1442                         Assert.AreEqual (0, cookies.Count, "#B4");
1443                         cookies = cc.GetCookies (new Uri ("http://" + hostname + "/Whatever/Do"));
1444                         Assert.IsNotNull (cookies, "#B5");
1445                         Assert.AreEqual (hostname.EndsWith (".local") ? 1 : 0, cookies.Count, "#B6");
1446
1447                         cc = new CookieContainer ();
1448                         cc.SetCookies (new Uri ("http://" + hostname + "/Whatever/Do"),
1449                                 "Age=26; path=/Whatever; domain=.local");
1450                         cookies = cc.GetCookies (new Uri ("http://localhost/Whatever/Do"));
1451                         Assert.IsNotNull (cookies, "#C1");
1452                         Assert.AreEqual (0, cookies.Count, "#C2");
1453                         cookies = cc.GetCookies (new Uri ("http://127.0.0.1/Whatever/Do"));
1454                         Assert.IsNotNull (cookies, "#C3");
1455                         Assert.AreEqual (0, cookies.Count, "#C4");
1456                         cookies = cc.GetCookies (new Uri ("http://" + hostname + "/Whatever/Do"));
1457                         Assert.IsNotNull (cookies, "#C5");
1458                         Assert.AreEqual (hostname.EndsWith (".local") ? 1 : 0, cookies.Count, "#C6");
1459                 }
1460 #endif
1461
1462                 [Test]
1463                 public void bug421827 ()
1464                 {
1465                         CookieContainer container;
1466                         CookieCollection cookies;
1467                         Cookie cookie;
1468
1469                         container = new CookieContainer ();
1470                         container.SetCookies (new Uri ("http://test.mono.com/Whatever/Do"),
1471                                 "Country=Belgium; path=/Whatever; domain=mono.com");
1472                         cookies = container.GetCookies (new Uri ("http://dev.mono.com/Whatever"));
1473
1474                         Assert.AreEqual (1, cookies.Count, "#A1");
1475                         cookie = cookies [0];
1476                         Assert.AreEqual ("Country", cookie.Name, "#A2");
1477                         Assert.AreEqual ("mono.com", cookie.Domain, "#A3");
1478                         Assert.AreEqual ("/Whatever", cookie.Path, "#A4");
1479                         Assert.AreEqual (0, cookie.Version, "#A5");
1480
1481                         container = new CookieContainer ();
1482                         container.SetCookies (new Uri ("http://sub.mono.com/Whatever/Do"),
1483                                 "Country=Belgium; path=/Whatever; domain=mono.com");
1484                         cookies = container.GetCookies (new Uri ("http://gomono.com/Whatever"));
1485
1486                         Assert.AreEqual (0, cookies.Count, "#B");
1487
1488                         container = new CookieContainer ();
1489                         container.SetCookies (new Uri ("http://sub.mono.com/Whatever/Do"),
1490                                 "Country=Belgium; path=/Whatever; domain=mono.com");
1491                         cookies = container.GetCookies (new Uri ("http://amono.com/Whatever"));
1492
1493                         Assert.AreEqual (0, cookies.Count, "#C");
1494                 }
1495
1496                 [Test]
1497                 public void MoreThanDefaultDomainCookieLimit ()
1498                 {
1499                         CookieContainer cc = new CookieContainer ();
1500                         for (int i = 1; i <= CookieContainer.DefaultPerDomainCookieLimit; i++) {
1501                                 Cookie c = new Cookie (i.ToString (), i.ToString (), "/", "mono.com");
1502                                 cc.Add (c);
1503                         }
1504                         Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, cc.Count, "Count");
1505                         Cookie c2 = new Cookie ("uho", "21", "/", "mono.com");
1506                         cc.Add (c2);
1507                         Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, cc.Count, "Count");
1508                         // so one (yes '1' ;-) was removed
1509                 }
1510
1511                 [Test]
1512                 public void MoreThanDefaultCookieLimit ()
1513                 {
1514                         CookieContainer cc = new CookieContainer ();
1515                         for (int i = 1; i <= CookieContainer.DefaultCookieLimit; i++) {
1516                                 Cookie c = new Cookie (i.ToString (), i.ToString (), "/", "www" + i.ToString () + ".mono.com");
1517                                 cc.Add (c);
1518                         }
1519                         Assert.AreEqual (CookieContainer.DefaultCookieLimit, cc.Count, "Count");
1520                         Cookie c2 = new Cookie ("uho", "301", "/", "www301.mono.com");
1521                         cc.Add (c2);
1522                         Assert.AreEqual (CookieContainer.DefaultCookieLimit, cc.Count, "Count");
1523                         // so one (yes '1' ;-) was removed
1524                 }
1525
1526                 [Test]
1527                 public void SaveAndLoadViaAddUriCookie ()
1528                 {
1529                         Cookie cookie = new Cookie ("name", "value")
1530                         {
1531                                 Domain = ".example.com",
1532                                 Expires = new DateTime (2015, 1, 1, 0, 0, 0, DateTimeKind.Utc),
1533                                 HttpOnly = true,
1534                                 Secure = true,
1535                         };
1536
1537                         Uri uri = new Uri ("https://www.example.com/path/file");
1538                         CookieContainer container = new CookieContainer ();
1539                         container.Add (uri, cookie);
1540                         CookieCollection collection = container.GetCookies (uri);
1541                         Assert.AreEqual (collection.Count, 1, "#A1");
1542                         Cookie cloned = collection [0];
1543                         
1544                         Assert.AreEqual (cookie.Comment, cloned.Comment, "#A2");
1545                         Assert.AreEqual (cookie.CommentUri, cloned.CommentUri, "#A3");
1546                         Assert.AreEqual (cookie.Domain, cloned.Domain, "#A4");
1547                         Assert.AreEqual (cookie.Discard, cloned.Discard, "#A5");
1548                         Assert.AreEqual (cookie.Expired, cloned.Expired, "#A6");
1549                         Assert.AreEqual (cookie.Expires.ToUniversalTime (), cloned.Expires.ToUniversalTime (), "#A7");
1550                         Assert.AreEqual (cookie.HttpOnly, cloned.HttpOnly, "#A8");
1551                         Assert.AreEqual (cookie.Name, cloned.Name, "#A9");
1552                         Assert.AreEqual ("/path/file", cloned.Path, "#A10");
1553                         Assert.AreEqual (cookie.Port, cloned.Port, "#A11");
1554                         Assert.AreEqual (cookie.Value, cloned.Value, "#A12");
1555                         Assert.AreEqual (cookie.Version, cloned.Version, "#A13");
1556                         Assert.AreEqual (cookie.Secure, cloned.Secure, "#A14");
1557                 }
1558
1559                 [Test]
1560                 public void SaveAndLoadViaSetCookies ()
1561                 {
1562                         Cookie cookie = new Cookie ("name", "value")
1563                         {
1564                                 Domain = ".example.com",
1565                                 Expires = new DateTime (2015, 1, 1, 0, 0, 0, DateTimeKind.Utc),
1566                                 HttpOnly = true,
1567                                 Secure = true,
1568                         };
1569
1570                         Uri uri = new Uri ("https://www.example.com/path/file");
1571                         CookieContainer container = new CookieContainer ();
1572                         container.SetCookies (uri, "name=value; domain=.example.com; expires=Thu, 01-Jan-2015 00:00:00 GMT; HttpOnly; secure");
1573                         CookieCollection collection = container.GetCookies (uri);
1574                         Assert.AreEqual (collection.Count, 1, "#A1");
1575                         Cookie cloned = collection [0];
1576                         
1577                         Assert.AreEqual (cookie.Comment, cloned.Comment, "#A2");
1578                         Assert.AreEqual (cookie.CommentUri, cloned.CommentUri, "#A3");
1579                         Assert.AreEqual (cookie.Domain, cloned.Domain, "#A4");
1580                         Assert.AreEqual (cookie.Discard, cloned.Discard, "#A5");
1581                         Assert.AreEqual (cookie.Expired, cloned.Expired, "#A6");
1582                         Assert.AreEqual (cookie.Expires.ToUniversalTime (), cloned.Expires.ToUniversalTime (), "#A7");
1583                         Assert.AreEqual (cookie.HttpOnly, cloned.HttpOnly, "#A8");
1584                         Assert.AreEqual (cookie.Name, cloned.Name, "#A9");
1585                         Assert.AreEqual (cookie.Path, "", "#A10");
1586                         Assert.AreEqual (cloned.Path, "/path/file", "#A11");
1587                         Assert.AreEqual (cookie.Port, cloned.Port, "#A12");
1588                         Assert.AreEqual (cookie.Value, cloned.Value, "#A13");
1589                         Assert.AreEqual (cookie.Version, cloned.Version, "#A14");
1590                         Assert.AreEqual (cookie.Secure, cloned.Secure, "#A15");
1591                 }
1592         }
1593 }