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