CookieTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // CookieTest.cs - NUnit Test Cases for System.Net.Cookie
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.Net;
  10. namespace MonoTests.System.Net
  11. {
  12. public class CookieTest : TestCase
  13. {
  14. public CookieTest () :
  15. base ("[MonoTests.System.Net.CookieTest]") {}
  16. public CookieTest (string name) : base (name) {}
  17. protected override void SetUp () {}
  18. protected override void TearDown () {}
  19. public static ITest Suite
  20. {
  21. get {
  22. return new TestSuite (typeof (CookieTest));
  23. }
  24. }
  25. public void TestPublicFields ()
  26. {
  27. }
  28. public void TestConstructors ()
  29. {
  30. Cookie c = new Cookie ("somename", null, null, null);
  31. try {
  32. c = new Cookie (null, null, null, null);
  33. Fail ("#1: Name cannot be null");
  34. } catch (CookieException) {
  35. }
  36. }
  37. public void TestName ()
  38. {
  39. Cookie c = new Cookie ("SomeName", "SomeValue");
  40. AssertEquals ("#1", c.Name, "SomeName");
  41. try {
  42. c.Name = null;
  43. Fail ("#2a");
  44. } catch (CookieException) {
  45. AssertEquals ("#2b", "SomeName", c.Name);
  46. }
  47. try {
  48. c.Name = "";
  49. Fail ("#2c");
  50. } catch (CookieException) {
  51. AssertEquals ("#2d", "SomeName", c.Name);
  52. }
  53. try {
  54. c.Name = " ";
  55. Fail ("#2e");
  56. } catch (CookieException) {
  57. // bah! this fails, yet the name is changed..
  58. // inconsistent with previous test
  59. AssertEquals ("#2f", String.Empty, c.Name);
  60. }
  61. try {
  62. c.Name = "xxx\r\n";
  63. Fail ("#2g");
  64. } catch (CookieException) {
  65. AssertEquals ("#2h", String.Empty, c.Name);
  66. }
  67. try {
  68. c.Name = "xxx" + (char) 0x80;
  69. } catch (CookieException) {
  70. Fail ("#2i");
  71. }
  72. try {
  73. c.Name = "$omeName";
  74. Fail ("#3a: Name cannot start with '$' character");
  75. } catch (CookieException) {
  76. AssertEquals ("#3b", String.Empty, c.Name);
  77. }
  78. c.Name = "SomeName$";
  79. AssertEquals ("#4", c.Name, "SomeName$");
  80. try {
  81. c.Name = "Some=Name";
  82. Fail ("#5a: Name cannot contain '=' character");
  83. } catch (CookieException) {
  84. AssertEquals ("#5b", String.Empty, c.Name);
  85. }
  86. c.Name = "domain";
  87. AssertEquals ("#6", c.Name, "domain");
  88. }
  89. public void TestValue ()
  90. {
  91. // LAMESPEC: According to .Net specs the Value property should not accept
  92. // the semicolon and comma characters, yet it does
  93. /*
  94. Cookie c = new Cookie("SomeName", "SomeValue");
  95. try {
  96. c.Value = "Some;Value";
  97. Fail ("#1: semicolon should not be accepted");
  98. } catch (CookieException) {
  99. }
  100. try {
  101. c.Value = "Some,Value";
  102. Fail ("#2: comma should not be accepted");
  103. } catch (CookieException) {
  104. }
  105. c.Value = "Some\tValue";
  106. AssertEquals ("#3", c.Value, "Some\tValue");
  107. */
  108. }
  109. public void TestPort ()
  110. {
  111. Cookie c = new Cookie ("SomeName", "SomeValue");
  112. try {
  113. c.Port = "123";
  114. Fail ("#1: port must start and end with double quotes");
  115. } catch (CookieException) {
  116. }
  117. try {
  118. c.Port = "\"123\"";
  119. } catch (CookieException) {
  120. Fail ("#2");
  121. }
  122. try {
  123. c.Port = "\"123;124\"";
  124. Fail ("#3");
  125. } catch (CookieException) {
  126. }
  127. try {
  128. c.Port = "\"123,123,124\"";
  129. } catch (CookieException) {
  130. Fail ("#4");
  131. }
  132. try {
  133. c.Port = "\"123,124\"";
  134. } catch (CookieException) {
  135. Fail ("#5");
  136. }
  137. }
  138. public void TestEquals ()
  139. {
  140. Cookie c1 = new Cookie ("NAME", "VALUE", "PATH", "DOMAIN");
  141. Cookie c2 = new Cookie ("name", "value", "path", "domain");
  142. Assert("#1", !c1.Equals (c2));
  143. c2.Value = "VALUE";
  144. c2.Path = "PATH";
  145. Assert("#2", c1.Equals (c2));
  146. c2.Version = 1;
  147. Assert("#3", !c1.Equals (c2));
  148. }
  149. }
  150. }