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