CookieTest.cs 5.8 KB

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