CookieContainerTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // System.Net.CookieContainerTest - CookieContainer tests
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (c) Copyright 2004 Novell, Inc. (http://www.novell.com)
  8. //
  9. using System;
  10. using System.Net;
  11. using System.Reflection;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Net
  14. {
  15. [TestFixture]
  16. public class CookieContainerTest
  17. {
  18. [Test]
  19. public void TestCtor1 ()
  20. {
  21. CookieContainer c = new CookieContainer (234);
  22. Assert.AreEqual (234, c.Capacity, "#1");
  23. try {
  24. new CookieContainer (0);
  25. Assert.Fail ("#2");
  26. } catch (ArgumentException) {
  27. }
  28. try {
  29. new CookieContainer (-10);
  30. Assert.Fail ("#3");
  31. } catch (ArgumentException) {
  32. }
  33. }
  34. [Test]
  35. public void TestCtor3 ()
  36. {
  37. CookieContainer c = new CookieContainer (100, 50, 1000);
  38. Assert.AreEqual (100, c.Capacity, "#1");
  39. Assert.AreEqual (50, c.PerDomainCapacity, "#2");
  40. Assert.AreEqual (1000, c.MaxCookieSize, "#3");
  41. try {
  42. new CookieContainer (100, 0, 1000);
  43. Assert.Fail ("#4");
  44. } catch (ArgumentException) {
  45. }
  46. try {
  47. new CookieContainer (100, -1, 1000);
  48. Assert.Fail ("#5");
  49. } catch (ArgumentException) {
  50. }
  51. c = new CookieContainer (100, int.MaxValue, 1000);
  52. Assert.AreEqual (int.MaxValue, c.PerDomainCapacity, "#6");
  53. try {
  54. new CookieContainer (100, 50, 0);
  55. Assert.Fail ("#7");
  56. } catch (ArgumentException) {
  57. }
  58. try {
  59. new CookieContainer (100, 500, -4);
  60. Assert.Fail ("#8");
  61. } catch (ArgumentException) {
  62. }
  63. }
  64. [Test]
  65. public void TestDefaultLimits ()
  66. {
  67. Assert.AreEqual (4096, CookieContainer.DefaultCookieLengthLimit, "#1");
  68. Assert.AreEqual (300, CookieContainer.DefaultCookieLimit, "#2");
  69. Assert.AreEqual (20, CookieContainer.DefaultPerDomainCookieLimit, "#3");
  70. }
  71. [Test]
  72. public void TestCapacity ()
  73. {
  74. CookieContainer c = new CookieContainer ();
  75. Assert.AreEqual (300, c.Capacity, "#1");
  76. c.Capacity = 200;
  77. Assert.AreEqual (200, c.Capacity, "#2");
  78. try {
  79. c.Capacity = -5;
  80. Assert.Fail ("#3");
  81. } catch (ArgumentOutOfRangeException) {
  82. }
  83. try {
  84. c.Capacity = 5; // must be >= PerDomainCapacity if PerDomainCapacity != Int32.MaxValue
  85. Assert.Fail ("#4");
  86. } catch (ArgumentOutOfRangeException) {
  87. }
  88. }
  89. [Test]
  90. public void TestMaxCookieSize ()
  91. {
  92. CookieContainer c = new CookieContainer ();
  93. Assert.AreEqual (4096, c.MaxCookieSize, "#1");
  94. try {
  95. c.MaxCookieSize = -5;
  96. Assert.Fail ("#2");
  97. } catch (ArgumentOutOfRangeException) {
  98. }
  99. try {
  100. c.MaxCookieSize = -1;
  101. Assert.Fail ("#3");
  102. } catch (ArgumentOutOfRangeException) {
  103. }
  104. c.MaxCookieSize = 80000;
  105. Assert.AreEqual (80000, c.MaxCookieSize, "#4");
  106. c.MaxCookieSize = int.MaxValue;
  107. Assert.AreEqual (int.MaxValue, c.MaxCookieSize, "#5");
  108. }
  109. [Test]
  110. public void TestAdd_Args ()
  111. {
  112. CookieContainer cc = new CookieContainer ();
  113. try {
  114. cc.Add ((Cookie) null);
  115. Assert.Fail ("#1");
  116. } catch (ArgumentNullException) {
  117. }
  118. try {
  119. cc.Add ((CookieCollection) null);
  120. Assert.Fail ("#2");
  121. } catch (ArgumentNullException) {
  122. }
  123. try {
  124. cc.Add (null, (Cookie) null);
  125. Assert.Fail ("#3");
  126. } catch (ArgumentNullException) {
  127. }
  128. try {
  129. cc.Add (null, (CookieCollection) null);
  130. Assert.Fail ("#4");
  131. } catch (ArgumentNullException) {
  132. }
  133. try {
  134. cc.Add (new Uri ("http://www.contoso.com"), (Cookie) null);
  135. Assert.Fail ("#5");
  136. } catch (ArgumentNullException) {
  137. }
  138. try {
  139. cc.Add (new Uri ("http://www.contoso.com"), (CookieCollection) null);
  140. Assert.Fail ("#6");
  141. } catch (ArgumentNullException) {
  142. }
  143. }
  144. [Test]
  145. public void TestAdd_Cookie ()
  146. {
  147. CookieContainer cc = new CookieContainer ();
  148. Uri uri = new Uri ("http://www.contoso.com");
  149. cc.Add (uri, new CookieCollection ());
  150. DateTime timestamp = DateTime.Now;
  151. cc.Add (uri, new Cookie ("hola", "Adios"));
  152. CookieCollection coll = cc.GetCookies (uri);
  153. Cookie cookie = coll [0];
  154. Assert.AreEqual ("", cookie.Comment, "#1");
  155. Assert.IsNull (cookie.CommentUri, "#2");
  156. Assert.AreEqual ("www.contoso.com", cookie.Domain, "#3");
  157. Assert.IsFalse (cookie.Expired, "#4");
  158. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#5");
  159. Assert.AreEqual ("hola", cookie.Name, "#6");
  160. Assert.AreEqual ("/", cookie.Path, "#7");
  161. Assert.AreEqual ("", cookie.Port, "#8");
  162. Assert.IsFalse (cookie.Secure, "#9");
  163. // FIX the next test
  164. TimeSpan ts = cookie.TimeStamp - timestamp;
  165. if (ts.TotalMilliseconds > 500)
  166. Assert.Fail ("#10");
  167. Assert.AreEqual ("Adios", cookie.Value, "#11");
  168. Assert.AreEqual (0, cookie.Version, "#12");
  169. }
  170. [Test]
  171. public void TestGetCookies_Args ()
  172. {
  173. CookieContainer cc = new CookieContainer ();
  174. try {
  175. cc.GetCookies (null);
  176. Assert.Fail ("#1");
  177. } catch (ArgumentNullException) {
  178. }
  179. }
  180. [Test]
  181. public void TestSetCookies_Args ()
  182. {
  183. CookieContainer cc = new CookieContainer ();
  184. try {
  185. cc.SetCookies (null, "");
  186. Assert.Fail ("#1");
  187. } catch (ArgumentNullException) {
  188. }
  189. try {
  190. cc.SetCookies (new Uri ("http://www.contoso.com"), null);
  191. Assert.Fail ("#2");
  192. } catch (ArgumentNullException) {
  193. }
  194. try {
  195. cc.SetCookies (new Uri ("http://www.contoso.com"), "=lalala");
  196. Assert.Fail ("#3");
  197. } catch (CookieException) {
  198. }
  199. cc.SetCookies (new Uri ("http://www.contoso.com"), "");
  200. }
  201. [Test]
  202. public void GetCookies ()
  203. {
  204. CookieContainer container = new CookieContainer ();
  205. container.Add (new Cookie ("name", "value1", "/path", "localhost"));
  206. container.Add (new Cookie ("name", "value2", "/path/sub", "localhost"));
  207. CookieCollection cookies = container.GetCookies (
  208. new Uri ("http://localhost/path/sub"));
  209. Assert.IsNotNull (cookies, "#A1");
  210. Assert.AreEqual (2, cookies.Count, "#A2");
  211. Cookie cookie = cookies [0];
  212. Assert.AreEqual ("name", cookie.Name, "#B1");
  213. Assert.AreEqual ("value2", cookie.Value, "#B2");
  214. Assert.AreEqual ("/path/sub", cookie.Path, "#B3");
  215. Assert.AreEqual ("localhost", cookie.Domain, "#B4");
  216. cookie = cookies [1];
  217. Assert.AreEqual ("name", cookie.Name, "#C1");
  218. Assert.AreEqual ("value1", cookie.Value, "#C2");
  219. Assert.AreEqual ("/path", cookie.Path, "#C3");
  220. Assert.AreEqual ("localhost", cookie.Domain, "#C4");
  221. cookies = container.GetCookies (new Uri ("http://localhost/path"));
  222. Assert.IsNotNull (cookies, "#D1");
  223. Assert.AreEqual (1, cookies.Count, "#D2");
  224. cookie = cookies [0];
  225. Assert.AreEqual ("name", cookie.Name, "#E1");
  226. Assert.AreEqual ("value1", cookie.Value, "#E2");
  227. Assert.AreEqual ("/path", cookie.Path, "#E3");
  228. Assert.AreEqual ("localhost", cookie.Domain, "#E4");
  229. cookies = container.GetCookies (new Uri ("http://localhost/whatever"));
  230. Assert.IsNotNull (cookies, "#F1");
  231. Assert.AreEqual (0, cookies.Count, "#F2");
  232. }
  233. [Test]
  234. [ExpectedException (typeof (ArgumentNullException))]
  235. public void GetCookies_Uri_Null ()
  236. {
  237. CookieContainer container = new CookieContainer ();
  238. container.GetCookies ((Uri) null);
  239. }
  240. }
  241. }