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