CookieTest.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. // Daniel Nauck (dna(at)mono-project(dot)de)
  8. //
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using NUnit.Framework;
  12. using System;
  13. using System.Net;
  14. namespace MonoTests.System.Net
  15. {
  16. [TestFixture]
  17. public class CookieTest
  18. {
  19. [Test]
  20. public void PublicFields ()
  21. {
  22. Cookie c = new Cookie ();
  23. Assert.AreEqual (string.Empty, c.Name, "#A1");
  24. Assert.AreEqual (string.Empty, c.Value, "#A2");
  25. Assert.AreEqual (string.Empty, c.Domain, "#A3");
  26. Assert.AreEqual (string.Empty, c.Port, "#A4");
  27. Assert.AreEqual (string.Empty, c.Comment, "#A5");
  28. Assert.AreEqual (null, c.CommentUri, "#A6");
  29. Assert.IsFalse (c.Discard, "#A7");
  30. Assert.IsFalse (c.Expired, "#A8");
  31. Assert.AreEqual (DateTime.MinValue, c.Expires, "#A9");
  32. #if NET_2_0
  33. Assert.IsFalse (c.HttpOnly, "#A10");
  34. #endif
  35. Assert.AreEqual (string.Empty, c.Path, "#A11");
  36. Assert.IsFalse (c.Secure, "#A12");
  37. Assert.AreEqual (0, c.Version, "#A13");
  38. Assert.AreEqual (string.Empty, c.ToString (), "#A14");
  39. c.Expires = DateTime.Now;
  40. Assert.IsTrue (c.Expired, "#A15");
  41. c.Port = null;
  42. Assert.AreEqual (string.Empty, c.Port, "#A16");
  43. c.Value = null;
  44. Assert.AreEqual (string.Empty, c.Value, "#A17");
  45. }
  46. [Test]
  47. public void Constructors ()
  48. {
  49. Cookie c = new Cookie ("somename", null, null, null);
  50. try
  51. {
  52. c = new Cookie (null, null, null, null);
  53. Assertion.Fail ("#1: Name cannot be null");
  54. }
  55. catch (CookieException)
  56. {
  57. }
  58. }
  59. [Test]
  60. public void Name ()
  61. {
  62. Cookie c = new Cookie ("SomeName", "SomeValue");
  63. Assertion.AssertEquals ("#1", c.Name, "SomeName");
  64. try
  65. {
  66. c.Name = null;
  67. Assertion.Fail ("#2a");
  68. }
  69. catch (CookieException)
  70. {
  71. Assertion.AssertEquals ("#2b", "SomeName", c.Name);
  72. }
  73. try
  74. {
  75. c.Name = "";
  76. Assertion.Fail ("#2c");
  77. }
  78. catch (CookieException)
  79. {
  80. Assertion.AssertEquals ("#2d", "SomeName", c.Name);
  81. }
  82. try
  83. {
  84. c.Name = " ";
  85. Assertion.Fail ("#2e");
  86. }
  87. catch (CookieException)
  88. {
  89. // bah! this fails, yet the name is changed..
  90. // inconsistent with previous test
  91. Assertion.AssertEquals ("#2f", String.Empty, c.Name);
  92. }
  93. try
  94. {
  95. c.Name = "xxx\r\n";
  96. Assertion.Fail ("#2g");
  97. }
  98. catch (CookieException)
  99. {
  100. Assertion.AssertEquals ("#2h", String.Empty, c.Name);
  101. }
  102. try
  103. {
  104. c.Name = "xxx" + (char)0x80;
  105. }
  106. catch (CookieException)
  107. {
  108. Assertion.Fail ("#2i");
  109. }
  110. try
  111. {
  112. c.Name = "$omeName";
  113. Assertion.Fail ("#3a: Name cannot start with '$' character");
  114. }
  115. catch (CookieException)
  116. {
  117. Assertion.AssertEquals ("#3b", String.Empty, c.Name);
  118. }
  119. c.Name = "SomeName$";
  120. Assertion.AssertEquals ("#4", c.Name, "SomeName$");
  121. try
  122. {
  123. c.Name = "Some=Name";
  124. Assertion.Fail ("#5a: Name cannot contain '=' character");
  125. }
  126. catch (CookieException)
  127. {
  128. Assertion.AssertEquals ("#5b", String.Empty, c.Name);
  129. }
  130. c.Name = "domain";
  131. Assertion.AssertEquals ("#6", c.Name, "domain");
  132. }
  133. [Test]
  134. public void Value ()
  135. {
  136. // LAMESPEC: According to .Net specs the Value property should not accept
  137. // the semicolon and comma characters, yet it does
  138. /*
  139. Cookie c = new Cookie("SomeName", "SomeValue");
  140. try {
  141. c.Value = "Some;Value";
  142. Assertion.Fail ("#1: semicolon should not be accepted");
  143. } catch (CookieException) {
  144. }
  145. try {
  146. c.Value = "Some,Value";
  147. Assertion.Fail ("#2: comma should not be accepted");
  148. } catch (CookieException) {
  149. }
  150. c.Value = "Some\tValue";
  151. Assertion.AssertEquals ("#3", c.Value, "Some\tValue");
  152. */
  153. }
  154. [Test]
  155. public void Port ()
  156. {
  157. Cookie c = new Cookie ("SomeName", "SomeValue");
  158. try
  159. {
  160. c.Port = "123";
  161. Assertion.Fail ("#1: port must start and end with double quotes");
  162. }
  163. catch (CookieException)
  164. {
  165. }
  166. try
  167. {
  168. Assert.AreEqual (0, c.Version, "#6.1");
  169. c.Port = "\"123\"";
  170. Assert.AreEqual (1, c.Version, "#6.2");
  171. }
  172. catch (CookieException)
  173. {
  174. Assertion.Fail ("#2");
  175. }
  176. try
  177. {
  178. c.Port = "\"123;124\"";
  179. Assertion.Fail ("#3");
  180. }
  181. catch (CookieException)
  182. {
  183. }
  184. try
  185. {
  186. c.Port = "\"123,123,124\"";
  187. }
  188. catch (CookieException)
  189. {
  190. Assertion.Fail ("#4");
  191. }
  192. try
  193. {
  194. c.Port = "\"123,124\"";
  195. }
  196. catch (CookieException)
  197. {
  198. Assertion.Fail ("#5");
  199. }
  200. }
  201. [Test]
  202. public void Equals ()
  203. {
  204. Cookie c1 = new Cookie ("NAME", "VALUE", "PATH", "DOMAIN");
  205. Cookie c2 = new Cookie ("name", "value", "path", "domain");
  206. Assertion.Assert ("#1", !c1.Equals (c2));
  207. c2.Value = "VALUE";
  208. c2.Path = "PATH";
  209. Assertion.Assert ("#2", c1.Equals (c2));
  210. c2.Version = 1;
  211. Assertion.Assert ("#3", !c1.Equals (c2));
  212. }
  213. [Test]
  214. public void ToStringTest ()
  215. {
  216. Cookie c1 = new Cookie ("NAME", "VALUE", "/", "example.com");
  217. Assert.AreEqual ("NAME=VALUE", c1.ToString (), "#A1");
  218. Cookie c2 = new Cookie ();
  219. Assert.AreEqual (string.Empty, c2.ToString (), "#A2");
  220. Cookie c3 = new Cookie("NAME", "VALUE");
  221. Assert.AreEqual ("NAME=VALUE", c3.ToString (), "#A3");
  222. Cookie c4 = new Cookie ("NAME", "VALUE", "/", "example.com");
  223. c4.Version = 1;
  224. Assert.AreEqual ("$Version=1; NAME=VALUE; $Path=/; $Domain=example.com", c4.ToString (), "#A4");
  225. Cookie c5 = new Cookie ("NAME", "VALUE", "/", "example.com");
  226. c5.Port = "\"8080\"";
  227. Assert.AreEqual ("$Version=1; NAME=VALUE; $Path=/; $Domain=example.com; $Port=\"8080\"", c5.ToString (), "#A5");
  228. Cookie c6 = new Cookie ("NAME", "VALUE");
  229. c6.Version = 1;
  230. Assert.AreEqual ("$Version=1; NAME=VALUE", c6.ToString (), "#A6");
  231. }
  232. }
  233. }