CookieTest.cs 3.8 KB

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