CookieCollectionTest.cs 2.7 KB

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