CookieTest.cs 5.8 KB

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