CookieCollectionTest.cs 2.6 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. Assert.AreEqual (col.Count, 3, "#1");
  32. }
  33. [Test]
  34. public void Indexer ()
  35. {
  36. Cookie c = null;
  37. try {
  38. c = col [-1];
  39. Assert.Fail ("#1");
  40. } catch (ArgumentOutOfRangeException) {
  41. }
  42. try {
  43. c = col [col.Count];
  44. Assert.Fail ("#2");
  45. } catch (ArgumentOutOfRangeException) {
  46. }
  47. c = col ["name1"];
  48. Assert.AreEqual (c.Name, "name1", "#3");
  49. c = col ["NAME2"];
  50. Assert.AreEqual (c.Name, "name2", "#4");
  51. }
  52. [Test]
  53. public void Add ()
  54. {
  55. try {
  56. Cookie c = null;
  57. col.Add (c);
  58. Assert.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. Assert.Fail ("#2");
  66. } catch (Exception) {
  67. }
  68. Assert.AreEqual (col.Count, 3, "#3");
  69. col.Add (new Cookie("name1", "value1"));
  70. Assert.AreEqual (col.Count, 3, "#4");
  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. Assert.AreEqual (col.Count, 5, "#5");
  78. Assert.AreEqual (col ["NAME4"], c4, "#6");
  79. Assert.AreEqual (col [4], c5, "#7");
  80. }
  81. [Test]
  82. public void CopyTo ()
  83. {
  84. Array a = Array.CreateInstance (typeof (Cookie), 3);
  85. col.CopyTo (a, 0);
  86. Assert.AreEqual (a.GetValue (0), col [0], "#1");
  87. Assert.AreEqual (a.GetValue (1), col [1], "#2");
  88. Assert.AreEqual (a.GetValue (2), col [2], "#3");
  89. }
  90. [Test]
  91. public void Enumerator ()
  92. {
  93. IEnumerator enumerator = col.GetEnumerator ();
  94. enumerator.MoveNext ();
  95. Cookie c = (Cookie) enumerator.Current;
  96. Assert.AreEqual (c, col [0], "#1");
  97. col.Add (new Cookie ("name6", "value6"));
  98. try {
  99. enumerator.MoveNext ();
  100. Assert.Fail ("#2");
  101. } catch (InvalidOperationException) {
  102. }
  103. }
  104. }
  105. }