CookieContainerTest.cs 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  1. //
  2. // System.Net.CookieContainerTest - CookieContainer tests
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Daniel Nauck (dna(at)mono-project(dot)de)
  7. // Sebastien Pouliot <[email protected]>
  8. // Marek Safar ([email protected])
  9. //
  10. // Copyright (C) 2004,2009 Novell, Inc (http://www.novell.com)
  11. // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
  12. //
  13. using System;
  14. using System.Net;
  15. using System.Reflection;
  16. using NUnit.Framework;
  17. /*
  18. * About the RFC 2109 conditional:
  19. *
  20. * According to the MSDN docs, settings Cookie.Version = 1 should make the
  21. * implementation comply with RFC 2109. I tested this on Windows and it
  22. * looks like .NET 4.5 has a few bugs in this area.
  23. *
  24. * The tests in this file also don't comply with RFC 2109 (for instance, the
  25. * domain must start with a dot and single suffixes such as .localhost are
  26. * not allowed).
  27. *
  28. * Since there currently is no reference implementation due to these bugs in
  29. * .NET 4.5, I disabled these tests for the moment and made them test the
  30. * default behavior (the newer RFC 2965).
  31. *
  32. * .NET 4.5 fixes several bugs in its (default, non-2109) cookie implementation
  33. * over .NET 2.0 - I modified the tests to reflect this new, improved behavior.
  34. *
  35. * 09/13/12 martin
  36. *
  37. */
  38. namespace MonoTests.System.Net {
  39. [TestFixture]
  40. public class CookieContainerTest {
  41. [Test] // .ctor ()
  42. public void Constructor1 ()
  43. {
  44. CookieContainer c = new CookieContainer ();
  45. Assert.AreEqual (0, c.Count, "Count");
  46. Assert.AreEqual (CookieContainer.DefaultCookieLimit, c.Capacity, "Capacity");
  47. Assert.AreEqual (CookieContainer.DefaultCookieLengthLimit, c.MaxCookieSize, "MaxCookieSize");
  48. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "PerDomainCapacity");
  49. }
  50. [Test] // .ctor (Int32)
  51. public void Constructor2 ()
  52. {
  53. CookieContainer c = new CookieContainer (234);
  54. Assert.AreEqual (0, c.Count, "Count");
  55. Assert.AreEqual (234, c.Capacity, "Capacity");
  56. Assert.AreEqual (CookieContainer.DefaultCookieLengthLimit, c.MaxCookieSize, "MaxCookieSize");
  57. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "PerDomainCapacity");
  58. }
  59. [Test]
  60. public void Constructor2_Capacity_Invalid ()
  61. {
  62. // Capacity <= 0
  63. try {
  64. new CookieContainer (0);
  65. Assert.Fail ("#A1");
  66. }
  67. catch (ArgumentException ex) {
  68. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  69. Assert.IsNull (ex.InnerException, "#A3");
  70. // The specified value must be greater than 0
  71. Assert.IsNotNull (ex.Message, "#A4");
  72. Assert.AreEqual ("Capacity", ex.ParamName, "#A5");
  73. }
  74. // Capacity <= 0
  75. try {
  76. new CookieContainer (-10);
  77. Assert.Fail ("#B1");
  78. }
  79. catch (ArgumentException ex) {
  80. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  81. Assert.IsNull (ex.InnerException, "#B3");
  82. // The specified value must be greater than 0
  83. Assert.IsNotNull (ex.Message, "#B4");
  84. Assert.AreEqual ("Capacity", ex.ParamName, "#B5");
  85. }
  86. }
  87. [Test] // .ctor (Int32, Int32, Int32)
  88. public void Constructor3 ()
  89. {
  90. CookieContainer c;
  91. c = new CookieContainer (100, 50, 1000);
  92. Assert.AreEqual (100, c.Capacity, "#A1");
  93. Assert.AreEqual (50, c.PerDomainCapacity, "#A2");
  94. Assert.AreEqual (1000, c.MaxCookieSize, "#A3");
  95. c = new CookieContainer (234, int.MaxValue, 650);
  96. Assert.AreEqual (234, c.Capacity, "#A1");
  97. Assert.AreEqual (int.MaxValue, c.PerDomainCapacity, "#A2");
  98. Assert.AreEqual (650, c.MaxCookieSize, "#A3");
  99. c = new CookieContainer (234, 234, 100);
  100. Assert.AreEqual (234, c.Capacity, "#A1");
  101. Assert.AreEqual (234, c.PerDomainCapacity, "#A2");
  102. Assert.AreEqual (100, c.MaxCookieSize, "#A3");
  103. }
  104. [Test]
  105. public void Constructor3_Capacity_Invalid ()
  106. {
  107. // Capacity <= 0
  108. try {
  109. new CookieContainer (0, 0, 100);
  110. Assert.Fail ("#A1");
  111. }
  112. catch (ArgumentException ex) {
  113. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  114. Assert.IsNull (ex.InnerException, "#A3");
  115. // The specified value must be greater than 0
  116. Assert.IsNotNull (ex.Message, "#A4");
  117. Assert.AreEqual ("Capacity", ex.ParamName, "#A5");
  118. }
  119. // Capacity <= 0
  120. try {
  121. new CookieContainer (-10, 0, 100);
  122. Assert.Fail ("#B1");
  123. }
  124. catch (ArgumentException ex) {
  125. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  126. Assert.IsNull (ex.InnerException, "#B3");
  127. // The specified value must be greater than 0
  128. Assert.IsNotNull (ex.Message, "#B4");
  129. Assert.AreEqual ("Capacity", ex.ParamName, "#B5");
  130. }
  131. }
  132. [Test] // .ctor (Int32, Int32, Int32)
  133. public void Constructor3_MaxCookieSize_Invalid ()
  134. {
  135. // MaxCookieSize <= 0
  136. try {
  137. new CookieContainer (100, 50, 0);
  138. Assert.Fail ("#A1");
  139. }
  140. catch (ArgumentException ex) {
  141. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  142. Assert.IsNull (ex.InnerException, "#A3");
  143. // The specified value must be greater than 0
  144. Assert.IsNotNull (ex.Message, "#A3");
  145. Assert.AreEqual ("MaxCookieSize", ex.ParamName, "#A4");
  146. }
  147. // MaxCookieSize <= 0
  148. try {
  149. new CookieContainer (100, 50, -4);
  150. Assert.Fail ("#B1");
  151. }
  152. catch (ArgumentException ex) {
  153. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  154. Assert.IsNull (ex.InnerException, "#B3");
  155. // The specified value must be greater than 0
  156. Assert.IsNotNull (ex.Message, "#B3");
  157. Assert.AreEqual ("MaxCookieSize", ex.ParamName, "#B4");
  158. }
  159. }
  160. [Test] // .ctor (Int32, Int32, Int32)
  161. public void Constructor3_PerDomainCapacity_Invalid ()
  162. {
  163. // PerDomainCapacity <= 0
  164. try {
  165. new CookieContainer (432, 0, 1000);
  166. Assert.Fail ("#B1");
  167. }
  168. catch (ArgumentOutOfRangeException ex) {
  169. // 'PerDomainCapacity' has to be greater than
  170. // '0' and less than '432'
  171. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  172. Assert.IsNull (ex.InnerException, "#B3");
  173. Assert.IsNotNull (ex.Message, "#B4");
  174. Assert.AreEqual ("perDomainCapacity", ex.ParamName, "#B5");
  175. }
  176. // PerDomainCapacity <= 0
  177. try {
  178. new CookieContainer (432, -1, 1000);
  179. Assert.Fail ("#C1");
  180. }
  181. catch (ArgumentOutOfRangeException ex) {
  182. // 'PerDomainCapacity' has to be greater than
  183. // '0' and less than '432'
  184. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  185. Assert.IsNull (ex.InnerException, "#C3");
  186. Assert.IsNotNull (ex.Message, "#C4");
  187. Assert.AreEqual ("perDomainCapacity", ex.ParamName, "#C5");
  188. }
  189. // PerDomainCapacity > Capacity (and != Int32.MaxValue)
  190. try {
  191. new CookieContainer (432, 433, 1000);
  192. Assert.Fail ("#C1");
  193. }
  194. catch (ArgumentOutOfRangeException ex) {
  195. // 'PerDomainCapacity' has to be greater than
  196. // '0' and less than '432'
  197. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  198. Assert.IsNull (ex.InnerException, "#C3");
  199. Assert.IsNotNull (ex.Message, "#C4");
  200. Assert.AreEqual ("perDomainCapacity", ex.ParamName, "#C5");
  201. }
  202. }
  203. [Test]
  204. public void TestDefaultLimits ()
  205. {
  206. Assert.AreEqual (4096, CookieContainer.DefaultCookieLengthLimit, "#1");
  207. Assert.AreEqual (300, CookieContainer.DefaultCookieLimit, "#2");
  208. Assert.AreEqual (20, CookieContainer.DefaultPerDomainCookieLimit, "#3");
  209. }
  210. [Test]
  211. public void Capacity ()
  212. {
  213. CookieContainer c = new CookieContainer ();
  214. c.Capacity = c.PerDomainCapacity;
  215. Assert.AreEqual (c.PerDomainCapacity, c.Capacity, "#A1");
  216. Assert.AreEqual (CookieContainer.DefaultCookieLengthLimit, c.MaxCookieSize, "#A2");
  217. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "#A3");
  218. c.Capacity = int.MaxValue;
  219. Assert.AreEqual (int.MaxValue, c.Capacity, "#B1");
  220. Assert.AreEqual (CookieContainer.DefaultCookieLengthLimit, c.MaxCookieSize, "#B2");
  221. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "#B3");
  222. c.PerDomainCapacity = int.MaxValue;
  223. c.Capacity = (c.PerDomainCapacity - 1);
  224. Assert.AreEqual ((c.PerDomainCapacity - 1), c.Capacity, "#C1");
  225. Assert.AreEqual (CookieContainer.DefaultCookieLengthLimit, c.MaxCookieSize, "#C2");
  226. Assert.AreEqual (int.MaxValue, c.PerDomainCapacity, "#C3");
  227. }
  228. [Test]
  229. public void Capacity_Value_Invalid ()
  230. {
  231. CookieContainer c = new CookieContainer ();
  232. // Capacity <= 0
  233. try {
  234. c.Capacity = -5;
  235. Assert.Fail ("#A1");
  236. }
  237. catch (ArgumentOutOfRangeException ex) {
  238. // 'Capacity' has to be greater than '0' and
  239. // less than '20'
  240. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  241. Assert.IsNull (ex.InnerException, "#A3");
  242. Assert.IsNotNull (ex.Message, "#A4");
  243. Assert.AreEqual ("value", ex.ParamName, "#A5");
  244. }
  245. // Capacity <= 0
  246. try {
  247. c.Capacity = 0;
  248. Assert.Fail ("#B1");
  249. }
  250. catch (ArgumentOutOfRangeException ex) {
  251. // 'Capacity' has to be greater than '0' and
  252. // less than '20'
  253. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  254. Assert.IsNull (ex.InnerException, "#B3");
  255. Assert.IsNotNull (ex.Message, "#B4");
  256. Assert.AreEqual ("value", ex.ParamName, "#B5");
  257. }
  258. // Capacity < PerDomainCapacity (and PerDomainCapacity != Int32.MaxValue)
  259. try {
  260. c.Capacity = 5;
  261. Assert.Fail ("#C1");
  262. }
  263. catch (ArgumentOutOfRangeException ex) {
  264. // 'Capacity' has to be greater than '0' and
  265. // less than '20'
  266. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  267. Assert.IsNull (ex.InnerException, "#C3");
  268. Assert.IsNotNull (ex.Message, "#C4");
  269. Assert.AreEqual ("value", ex.ParamName, "#C5");
  270. }
  271. }
  272. [Test]
  273. public void MaxCookieSize ()
  274. {
  275. CookieContainer c = new CookieContainer ();
  276. c.MaxCookieSize = 80000;
  277. Assert.AreEqual (CookieContainer.DefaultCookieLimit, c.Capacity, "#A1");
  278. Assert.AreEqual (80000, c.MaxCookieSize, "#A2");
  279. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "#A3");
  280. c.MaxCookieSize = int.MaxValue;
  281. Assert.AreEqual (CookieContainer.DefaultCookieLimit, c.Capacity, "#B1");
  282. Assert.AreEqual (int.MaxValue, c.MaxCookieSize, "#B2");
  283. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "#B3");
  284. c.MaxCookieSize = 1;
  285. Assert.AreEqual (CookieContainer.DefaultCookieLimit, c.Capacity, "#C1");
  286. Assert.AreEqual (1, c.MaxCookieSize, "#C2");
  287. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, c.PerDomainCapacity, "#C3");
  288. }
  289. [Test]
  290. public void MaxCookieSize_Value_Invalid ()
  291. {
  292. CookieContainer c = new CookieContainer ();
  293. // MaxCookieSize <= 0
  294. try {
  295. c.MaxCookieSize = -5;
  296. Assert.Fail ("#A1");
  297. }
  298. catch (ArgumentOutOfRangeException ex) {
  299. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  300. Assert.IsNull (ex.InnerException, "#A3");
  301. Assert.IsNotNull (ex.Message, "#A4");
  302. Assert.AreEqual ("value", ex.ParamName, "#A5");
  303. }
  304. // MaxCookieSize <= 0
  305. try {
  306. c.MaxCookieSize = -1;
  307. Assert.Fail ("#B1");
  308. }
  309. catch (ArgumentOutOfRangeException ex) {
  310. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  311. Assert.IsNull (ex.InnerException, "#B3");
  312. Assert.IsNotNull (ex.Message, "#B4");
  313. Assert.AreEqual ("value", ex.ParamName, "#B5");
  314. }
  315. // MaxCookieSize <= 0
  316. try {
  317. c.MaxCookieSize = 0;
  318. Assert.Fail ("#C1");
  319. }
  320. catch (ArgumentOutOfRangeException ex) {
  321. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  322. Assert.IsNull (ex.InnerException, "#C3");
  323. Assert.IsNotNull (ex.Message, "#C4");
  324. Assert.AreEqual ("value", ex.ParamName, "#C5");
  325. }
  326. }
  327. [Test]
  328. public void PerDomainCapacity ()
  329. {
  330. CookieContainer c = new CookieContainer ();
  331. c.PerDomainCapacity = c.Capacity;
  332. Assert.AreEqual (c.Capacity, c.PerDomainCapacity, "#1");
  333. c.PerDomainCapacity = int.MaxValue;
  334. Assert.AreEqual (int.MaxValue, c.PerDomainCapacity, "#2");
  335. c.PerDomainCapacity = c.Capacity - 5;
  336. Assert.AreEqual ((c.Capacity - 5), c.PerDomainCapacity, "#3");
  337. c.PerDomainCapacity = 1;
  338. Assert.AreEqual (1, c.PerDomainCapacity, "#4");
  339. }
  340. [Test]
  341. public void PerDomainCapacity_Value_Invalid ()
  342. {
  343. CookieContainer c = new CookieContainer ();
  344. // PerDomainCapacity <= 0
  345. try {
  346. c.PerDomainCapacity = -5;
  347. Assert.Fail ("#A1");
  348. }
  349. catch (ArgumentOutOfRangeException ex) {
  350. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  351. Assert.IsNull (ex.InnerException, "#A3");
  352. Assert.IsNotNull (ex.Message, "#A4");
  353. Assert.AreEqual ("value", ex.ParamName, "#A5");
  354. }
  355. // PerDomainCapacity <= 0
  356. try {
  357. c.PerDomainCapacity = 0;
  358. Assert.Fail ("#B1");
  359. }
  360. catch (ArgumentOutOfRangeException ex) {
  361. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  362. Assert.IsNull (ex.InnerException, "#B3");
  363. Assert.IsNotNull (ex.Message, "#B4");
  364. Assert.AreEqual ("value", ex.ParamName, "#B5");
  365. }
  366. c.Capacity = (c.PerDomainCapacity + 5);
  367. // PerDomainCapacity > Capacity (and != Int32.MaxValue)
  368. try {
  369. c.PerDomainCapacity = (c.Capacity + 1);
  370. Assert.Fail ("#C1");
  371. }
  372. catch (ArgumentOutOfRangeException ex) {
  373. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  374. Assert.IsNull (ex.InnerException, "#C3");
  375. Assert.IsNotNull (ex.Message, "#C4");
  376. Assert.AreEqual ("value", ex.ParamName, "#C5");
  377. }
  378. }
  379. [Test]
  380. public void Add_LocalWithPort ()
  381. {
  382. CookieContainer cc = new CookieContainer ();
  383. var orig = new Cookie ("mycookie", "vv");
  384. cc.Add (new Uri ("http://localhost:8810/"), orig);
  385. var c = cc.GetCookies (new Uri ("http://localhost:8810/"))[0];
  386. Assert.AreEqual ("", c.Comment, "#1");
  387. Assert.IsNull (c.CommentUri, "#2");
  388. Assert.IsFalse (c.Discard, "#3");
  389. Assert.AreEqual ("localhost", c.Domain, "#4");
  390. Assert.IsFalse (c.Expired, "#5");
  391. Assert.AreEqual (DateTime.MinValue, c.Expires, "#6");
  392. Assert.IsFalse (c.HttpOnly, "#7");
  393. Assert.AreEqual ("mycookie", c.Name, "#8");
  394. Assert.AreEqual ("/", c.Path, "#9");
  395. Assert.AreEqual ("", c.Port, "#10");
  396. Assert.IsFalse (c.Secure, "#11");
  397. Assert.AreEqual ("vv", c.Value, "#13");
  398. Assert.AreEqual (0, c.Version, "#14");
  399. Assert.AreEqual ("mycookie=vv", c.ToString (), "#15");
  400. }
  401. [Test] // Add (Cookie)
  402. public void Add1 ()
  403. {
  404. CookieContainer cc = new CookieContainer ();
  405. Cookie cookie = new Cookie ("Age", "28", string.Empty, "localhost");
  406. Assert.AreEqual ("Age", cookie.Name, "Name");
  407. Assert.AreEqual ("28", cookie.Value, "Value");
  408. Assert.AreEqual (String.Empty, cookie.Path, "Path");
  409. Assert.AreEqual ("localhost", cookie.Domain, "Domain");
  410. // does not survive the addition "cloning"
  411. cookie.Comment = "comment";
  412. cookie.CommentUri = new Uri ("http://localhost");
  413. cookie.Discard = true;
  414. cookie.Expires = DateTime.MaxValue;
  415. cookie.HttpOnly = true;
  416. cookie.Secure = true;
  417. // except version
  418. #if RFC2109
  419. cookie.Version = 1;
  420. #endif
  421. cc.Add (cookie);
  422. Assert.AreEqual (1, cc.Count, "#A1");
  423. CookieCollection cookies = cc.GetCookies (new Uri ("https://localhost/Whatever"));
  424. Assert.AreEqual (1, cookies.Count, "#A2");
  425. Assert.AreNotSame (cookie, cookies [0], "!same");
  426. cookie = cookies [0];
  427. Assert.AreEqual ("Age", cookie.Name, "Clone-Name");
  428. Assert.AreEqual ("28", cookie.Value, "Clone-Value");
  429. Assert.AreEqual (string.Empty, cookie.Path, "Clone-Path");
  430. Assert.AreEqual ("localhost", cookie.Domain, "Clone-Domain");
  431. Assert.AreEqual ("comment", cookie.Comment, "Clone-Comment");
  432. Assert.AreEqual (new Uri ("http://localhost"), cookie.CommentUri, "Clone-CommentUri");
  433. Assert.IsTrue (cookie.Discard, "Clone-Discard");
  434. Assert.AreEqual (DateTime.MaxValue, cookie.Expires, "Clone-Expires");
  435. Assert.IsTrue (cookie.HttpOnly, "Clone-HttpOnly");
  436. Assert.IsTrue (cookie.Secure, "Clone-Secure");
  437. #if RFC2109
  438. Assert.AreEqual (1, cookie.Version, "Clone-Version");
  439. #else
  440. Assert.AreEqual (0, cookie.Version, "Clone-Version");
  441. #endif
  442. cookies = cc.GetCookies (new Uri ("https://localhost/Whatever"));
  443. // the same Cookie instance returned for a second query
  444. Assert.AreSame (cookie, cookies [0], "!same-2");
  445. }
  446. [Test] // Add (Cookie)
  447. public void Add1_Domain_Empty ()
  448. {
  449. CookieContainer cc = new CookieContainer ();
  450. Cookie cookie = new Cookie ("Age", "28");
  451. try {
  452. cc.Add (cookie);
  453. Assert.Fail ("#1");
  454. }
  455. catch (ArgumentException ex) {
  456. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  457. Assert.IsNull (ex.InnerException, "#3");
  458. Assert.IsNotNull (ex.Message, "#4");
  459. Assert.AreEqual ("cookie.Domain", ex.ParamName, "#5");
  460. }
  461. }
  462. [Test]
  463. public void Add_Domain_Invalid ()
  464. {
  465. var cc = new CookieContainer ();
  466. try {
  467. cc.Add (new Cookie ("Foo", "Bar", string.Empty, ".com"));
  468. Assert.Fail ("#A1");
  469. } catch (CookieException) {
  470. ;
  471. }
  472. try {
  473. cc.Add (new Uri ("http://mono.com/"), new Cookie ("Foo", "Bar", string.Empty, ".evil.org"));
  474. Assert.Fail ("#A2");
  475. } catch (CookieException) {
  476. ;
  477. }
  478. }
  479. [Test] // Add (CookieCollection)
  480. public void Add2_Cookies_Null ()
  481. {
  482. CookieContainer cc = new CookieContainer ();
  483. try {
  484. cc.Add ((CookieCollection) null);
  485. Assert.Fail ("#1");
  486. }
  487. catch (ArgumentNullException ex) {
  488. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  489. Assert.IsNull (ex.InnerException, "#3");
  490. Assert.IsNotNull (ex.Message, "#4");
  491. Assert.AreEqual ("cookies", ex.ParamName, "#5");
  492. }
  493. }
  494. [Test] // Add (Uri, Cookie)
  495. public void Add3_Uri_Null ()
  496. {
  497. CookieContainer cc = new CookieContainer ();
  498. Cookie cookie = new Cookie ("Age", "28", "", "localhost");
  499. try {
  500. cc.Add ((Uri) null, cookie);
  501. Assert.Fail ("#1");
  502. }
  503. catch (ArgumentNullException ex) {
  504. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  505. Assert.IsNull (ex.InnerException, "#3");
  506. Assert.IsNotNull (ex.Message, "#4");
  507. Assert.AreEqual ("uri", ex.ParamName, "#5");
  508. }
  509. }
  510. [Test] // Add (Uri, Cookie)
  511. public void Add3_Cookie_Null ()
  512. {
  513. CookieContainer cc = new CookieContainer ();
  514. Uri uri = new Uri ("http://www.contoso.com");
  515. try {
  516. cc.Add (uri, (Cookie) null);
  517. Assert.Fail ("#1");
  518. }
  519. catch (ArgumentNullException ex) {
  520. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  521. Assert.IsNull (ex.InnerException, "#3");
  522. Assert.IsNotNull (ex.Message, "#4");
  523. Assert.AreEqual ("cookie", ex.ParamName, "#5");
  524. }
  525. }
  526. [Test] // Add (Uri, CookieCollection)
  527. public void Add4_Uri_Null ()
  528. {
  529. CookieContainer cc = new CookieContainer ();
  530. CookieCollection cookies = new CookieCollection ();
  531. try {
  532. cc.Add ((Uri) null, cookies);
  533. Assert.Fail ("#1");
  534. }
  535. catch (ArgumentNullException ex) {
  536. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  537. Assert.IsNull (ex.InnerException, "#3");
  538. Assert.IsNotNull (ex.Message, "#4");
  539. Assert.AreEqual ("uri", ex.ParamName, "#5");
  540. }
  541. }
  542. [Test] // Add (Uri, CookieCollection)
  543. public void Add4_Cookie_Null ()
  544. {
  545. CookieContainer cc = new CookieContainer ();
  546. Uri uri = new Uri ("http://www.contoso.com");
  547. try {
  548. cc.Add (uri, (CookieCollection) null);
  549. Assert.Fail ("#1");
  550. }
  551. catch (ArgumentNullException ex) {
  552. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  553. Assert.IsNull (ex.InnerException, "#3");
  554. Assert.IsNotNull (ex.Message, "#4");
  555. Assert.AreEqual ("cookies", ex.ParamName, "#5");
  556. }
  557. }
  558. /*
  559. * This test demonstrates one of the new, fixed behaviors in .NET 4.5:
  560. *
  561. * The cookie domain implicitly assumes a leading dot, so "x.y" now matches
  562. * "foo.x.y" and "foo.bar.x.y".
  563. *
  564. */
  565. [Test]
  566. public void Add5_Subdomain ()
  567. {
  568. var cc = new CookieContainer ();
  569. var cookie = new Cookie ("Foo", "Bar", string.Empty, "mono.com");
  570. cc.Add (cookie);
  571. var coll = cc.GetCookies (new Uri ("http://www.mono.com/Whatever/"));
  572. Assert.AreEqual (1, coll.Count, "#A1");
  573. var coll2 = cc.GetCookies (new Uri ("http://www.us.mono.com/Whatever/"));
  574. Assert.AreEqual (1, coll.Count, "#A2");
  575. }
  576. public void Add6_Insecure ()
  577. {
  578. var cc = new CookieContainer ();
  579. var cookie = new Cookie ("Foo", "Bar", string.Empty, ".mono.com");
  580. cookie.Secure = true;
  581. // FIXME: This should throw an exception - but .NET 4.5 does not.
  582. cc.Add (new Uri ("http://mono.com/"), cookie);
  583. var coll = cc.GetCookies (new Uri ("http://mono.com/"));
  584. Assert.AreEqual (0, coll.Count, "#A1");
  585. }
  586. [Test]
  587. public void TestAdd_Cookie ()
  588. {
  589. CookieContainer cc = new CookieContainer ();
  590. Uri uri = new Uri ("http://www.contoso.com");
  591. cc.Add (uri, new CookieCollection ());
  592. DateTime timestamp = DateTime.Now;
  593. cc.Add (uri, new Cookie ("hola", "Adios"));
  594. CookieCollection coll = cc.GetCookies (uri);
  595. Cookie cookie = coll [0];
  596. Assert.AreEqual ("", cookie.Comment, "#1");
  597. Assert.IsNull (cookie.CommentUri, "#2");
  598. Assert.AreEqual ("www.contoso.com", cookie.Domain, "#3");
  599. Assert.IsFalse (cookie.Expired, "#4");
  600. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#5");
  601. Assert.AreEqual ("hola", cookie.Name, "#6");
  602. Assert.AreEqual ("/", cookie.Path, "#7");
  603. Assert.AreEqual ("", cookie.Port, "#8");
  604. Assert.IsFalse (cookie.Secure, "#9");
  605. // FIX the next test
  606. TimeSpan ts = cookie.TimeStamp - timestamp;
  607. if (ts.TotalMilliseconds > 500)
  608. Assert.Fail ("#10");
  609. Assert.AreEqual ("Adios", cookie.Value, "#11");
  610. Assert.AreEqual (0, cookie.Version, "#12");
  611. }
  612. [Test]
  613. // [Category ("NotWorking")]
  614. public void TestAddExpired_Cookie ()
  615. {
  616. CookieContainer cc = new CookieContainer ();
  617. Uri uri = new Uri ("http://www.contoso.com");
  618. DateTime expires = DateTime.Now.Subtract (new TimeSpan (1, 30, 0));
  619. //expired cookie
  620. Cookie c1 = new Cookie ("TEST", "MyValue", "/", uri.Host);
  621. c1.Expires = expires;
  622. cc.Add (c1);
  623. Assert.AreEqual (0, cc.Count, "#A1");
  624. CookieCollection coll = cc.GetCookies (uri);
  625. Assert.AreEqual (0, coll.Count, "#A1.1");
  626. //expired cookie
  627. Cookie c2 = new Cookie ("TEST2", "MyValue2");
  628. c2.Expires = expires;
  629. cc.Add (uri, c2);
  630. Assert.AreEqual (0, cc.Count, "#B1");
  631. //not expired cookie
  632. Cookie c3 = new Cookie ("TEST3", "MyValue3");
  633. cc.Add (uri, c3);
  634. Assert.AreEqual (1, cc.Count, "#C1");
  635. //not expired cookie
  636. Cookie c4 = new Cookie ("TEST4", "MyValue4", "/", ".contoso.com");
  637. cc.Add (uri, c4);
  638. Assert.AreEqual (2, cc.Count, "#E1");
  639. coll = cc.GetCookies (uri);
  640. Assert.AreEqual (2, coll.Count, "#E1.1");
  641. //expired cookie
  642. Cookie c5 = new Cookie ("TEST5", "MyValue5", "/", ".contoso.com");
  643. c5.Expires = expires;
  644. cc.Add (c5);
  645. Assert.AreEqual (2, cc.Count, "#F1");
  646. coll = cc.GetCookies (uri);
  647. Assert.AreEqual (2, coll.Count, "#F1.1");
  648. Assert.IsNull (coll ["TEST5"], "#F2");
  649. //expired cookie
  650. Cookie c6 = new Cookie ("TEST6", "MyValue6", "/", ".contoso.com");
  651. c6.Expires = expires;
  652. cc.Add (uri, c6);
  653. Assert.AreEqual (2, cc.Count, "#G1");
  654. coll = cc.GetCookies (uri);
  655. Assert.AreEqual (2, coll.Count, "#G1.1");
  656. Assert.IsNull (coll ["TEST6"], "#G2");
  657. }
  658. [Test]
  659. public void GetCookieHeader1 ()
  660. {
  661. CookieContainer cc;
  662. Cookie cookie;
  663. cc = new CookieContainer ();
  664. cookie = new Cookie ("name1", "value1", "/path", "localhost");
  665. cookie.Comment = "Short name";
  666. cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
  667. cookie.Version = 0;
  668. cc.Add (cookie);
  669. #if RFC2109
  670. cookie = new Cookie ("name2", "value2", "/path/sub", "localhost");
  671. cookie.Comment = "Description";
  672. cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
  673. cookie.Version = 1;
  674. cc.Add (cookie);
  675. Assert.AreEqual ("$Version=1; name2=value2; $Path=/path/sub; name1=value1", cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#A1");
  676. #endif
  677. Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://localhost/path")), "#A2");
  678. Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://localhost/whatever")), "#A3");
  679. }
  680. [Test]
  681. public void GetCookieHeader2a ()
  682. {
  683. CookieContainer cc = new CookieContainer ();
  684. Cookie cookie = new Cookie ("Country", "Belgium", "/path", "mono.com");
  685. cc.Add (cookie);
  686. cookie = new Cookie ("Age", "26", "/path", "dev.mono.com");
  687. cc.Add (cookie);
  688. Assert.AreEqual ("Age=26; Country=Belgium", cc.GetCookieHeader (new Uri ("http://dev.mono.com/path/ok")), "#A1");
  689. Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://mono.com/path")), "#A2");
  690. Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://test.mono.com/path")), "#A3");
  691. Assert.AreEqual ("Age=26; Country=Belgium", cc.GetCookieHeader (new Uri ("http://us.dev.mono.com/path")), "#A4");
  692. }
  693. [Test]
  694. public void GetCookieHeader2b ()
  695. {
  696. CookieContainer cc = new CookieContainer ();
  697. Cookie cookie = new Cookie ("Country", "Belgium", "/path", ".mono.com");
  698. cc.Add (cookie);
  699. cookie = new Cookie ("Age", "26", "/path", ".dev.mono.com");
  700. cc.Add (cookie);
  701. Assert.AreEqual ("Age=26; Country=Belgium", cc.GetCookieHeader (new Uri ("http://dev.mono.com/path/ok")), "#C1");
  702. Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://mono.com/path")), "#C2");
  703. Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://test.mono.com/path")), "#C3");
  704. Assert.AreEqual ("Age=26; Country=Belgium", cc.GetCookieHeader (new Uri ("http://us.dev.mono.com/path")), "#C4");
  705. }
  706. [Test]
  707. public void GetCookieHeader3 ()
  708. {
  709. CookieContainer cc = new CookieContainer ();
  710. cc.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do"),
  711. "Country=Belgium; path=/Whatever; domain=mono.com;" +
  712. "Age=26; path=/Whatever; domain=test.mono.com," +
  713. "Weight=87; path=/Whatever/Do; domain=.mono.com");
  714. Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://dev.mono.com/Whatever/Do")), "#C1");
  715. Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://test.mono.com/Whatever/Do")), "#C2");
  716. Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://mono.com/Whatever/Do")), "#C3");
  717. Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://us.test.mono.com/Whatever/Do")), "#C4");
  718. }
  719. [Test]
  720. public void GetCookieHeader4 ()
  721. {
  722. CookieContainer cc = new CookieContainer ();
  723. Cookie cookie = new Cookie ("Height", "178", "/Whatever", "mono.com");
  724. cc.Add (cookie);
  725. cookie = new Cookie ("Town", "Brussels", "/Whatever", ".mono.com");
  726. cc.Add (cookie);
  727. cookie = new Cookie ("Income", "34445", "/Whatever/", ".test.mono.com");
  728. cc.Add (cookie);
  729. cookie = new Cookie ("Sex", "Male", "/WhateveR/DO", ".test.mono.com");
  730. cc.Add (cookie);
  731. cc.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do/You"),
  732. "Country=Belgium," +
  733. "Age=26; path=/Whatever/Do; domain=test.mono.com," +
  734. "Weight=87; path=/");
  735. Assert.AreEqual ("Age=26; Income=34445; Height=178; Town=Brussels",
  736. cc.GetCookieHeader (new Uri ("http://us.test.mono.com/Whatever/Do/Ok")),
  737. "#D");
  738. }
  739. #if RFC2109
  740. [Test]
  741. public void GetCookieHeader5a ()
  742. {
  743. CookieContainer cc = new CookieContainer ();
  744. Cookie cookie = new Cookie ("name1", "value1", "", "localhost");
  745. cookie.Comment = "Short name";
  746. cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
  747. cookie.Version = 1;
  748. cc.Add (cookie);
  749. Assert.AreEqual ("$Version=1; name1=value1; $Domain=localhost", cookie.ToString (), "#E0");
  750. Assert.AreEqual ("$Version=1; name1=value1; $Path=/",
  751. cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#E1");
  752. }
  753. [Test]
  754. public void GetCookieHeader5b ()
  755. {
  756. CookieContainer cc = new CookieContainer ();
  757. Cookie cookie = new Cookie ("name1", "value1");
  758. cookie.Domain = "localhost";
  759. cookie.Comment = "Short name";
  760. cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
  761. cookie.Version = 1;
  762. cc.Add (cookie);
  763. Assert.AreEqual ("$Version=1; name1=value1; $Domain=localhost", cookie.ToString (), "#E0");
  764. Assert.AreEqual ("$Version=1; name1=value1; $Path=/",
  765. cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#E1");
  766. }
  767. #endif
  768. [Test]
  769. public void GetCookieHeader6 ()
  770. {
  771. CookieContainer cc = new CookieContainer ();
  772. Cookie cookie = new Cookie ("name1", "value1", "", "localhost");
  773. cookie.Comment = "Short name";
  774. cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
  775. cookie.Version = 0;
  776. cc.Add (cookie);
  777. Assert.AreEqual ("name1=value1",
  778. cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#E2");
  779. }
  780. [Test]
  781. public void GetCookieHeader7a ()
  782. {
  783. CookieContainer cc = new CookieContainer ();
  784. Cookie cookie = new Cookie ("name1", "value1", "/path", ".mono.com");
  785. cookie.Comment = "Short name";
  786. cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
  787. cookie.Version = 0;
  788. cc.Add (cookie);
  789. #if RFC2109
  790. cookie = new Cookie ("name2", "value2", "/path/sub", ".mono.com");
  791. cookie.Comment = "Description";
  792. cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
  793. cookie.Version = 1;
  794. cc.Add (cookie);
  795. Assert.AreEqual ("$Version=1; name2=value2; $Path=/path/sub; $Domain=.mono.com; name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path/sub")), "#A1");
  796. #endif
  797. Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path")), "#A2");
  798. Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://live.mono.com/whatever")), "#A3");
  799. Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://gomono.com/path/sub")), "#A4");
  800. Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://mono.com/path/sub")), "#A5");
  801. }
  802. [Test]
  803. public void GetCookieHeader7b ()
  804. {
  805. CookieContainer cc = new CookieContainer ();
  806. Cookie cookie = new Cookie ("name1", "value1", "/path", "live.mono.com");
  807. cookie.Comment = "Short name";
  808. cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
  809. cookie.Version = 0;
  810. cc.Add (cookie);
  811. #if RFC2109
  812. cookie = new Cookie ("name2", "value2", "/path/sub", "live.mono.com");
  813. cookie.Comment = "Description";
  814. cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
  815. cookie.Version = 1;
  816. cc.Add (cookie);
  817. Assert.AreEqual ("$Version=1; name2=value2; $Path=/path/sub; name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path/sub")), "#B1");
  818. #endif
  819. Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path")), "#B2");
  820. Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://live.mono.com/whatever")), "#B3");
  821. Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://go.live.mono.com/path/sub")), "#B4");
  822. Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://go.live.mono.com/path")), "#B5");
  823. Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://go.live.mono.com/whatever")), "#B6");
  824. Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://golive.mono.com/whatever")), "#B7");
  825. }
  826. [Test]
  827. public void GetCookieHeader_Uri_Null ()
  828. {
  829. CookieContainer cc = new CookieContainer ();
  830. try {
  831. cc.GetCookieHeader ((Uri) null);
  832. Assert.Fail ("#1");
  833. }
  834. catch (ArgumentNullException ex) {
  835. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  836. Assert.IsNull (ex.InnerException, "#3");
  837. Assert.IsNotNull (ex.Message, "#4");
  838. Assert.AreEqual ("uri", ex.ParamName, "#5");
  839. }
  840. }
  841. [Test]
  842. public void GetCookies ()
  843. {
  844. CookieContainer container = new CookieContainer ();
  845. container.Add (new Cookie ("name", "value1", "/path", "localhost"));
  846. container.Add (new Cookie ("name", "value2", "/path/sub", "localhost"));
  847. CookieCollection cookies = container.GetCookies (
  848. new Uri ("http://localhost/path/sub"));
  849. Assert.IsNotNull (cookies, "#A1");
  850. Assert.AreEqual (2, cookies.Count, "#A2");
  851. Cookie cookie = cookies [0];
  852. Assert.AreEqual ("name", cookie.Name, "#B1");
  853. Assert.AreEqual ("value2", cookie.Value, "#B2");
  854. Assert.AreEqual ("/path/sub", cookie.Path, "#B3");
  855. Assert.AreEqual ("localhost", cookie.Domain, "#B4");
  856. cookie = cookies [1];
  857. Assert.AreEqual ("name", cookie.Name, "#C1");
  858. Assert.AreEqual ("value1", cookie.Value, "#C2");
  859. Assert.AreEqual ("/path", cookie.Path, "#C3");
  860. Assert.AreEqual ("localhost", cookie.Domain, "#C4");
  861. cookies = container.GetCookies (new Uri ("http://localhost/path"));
  862. Assert.IsNotNull (cookies, "#D1");
  863. Assert.AreEqual (1, cookies.Count, "#D2");
  864. cookie = cookies [0];
  865. Assert.AreEqual ("name", cookie.Name, "#E1");
  866. Assert.AreEqual ("value1", cookie.Value, "#E2");
  867. Assert.AreEqual ("/path", cookie.Path, "#E3");
  868. Assert.AreEqual ("localhost", cookie.Domain, "#E4");
  869. cookies = container.GetCookies (new Uri ("http://localhost/whatever"));
  870. Assert.IsNotNull (cookies, "#F1");
  871. Assert.AreEqual (0, cookies.Count, "#F2");
  872. }
  873. [Test]
  874. public void GetCookies2a ()
  875. {
  876. CookieContainer container = new CookieContainer ();
  877. container.Add (new Cookie ("Country", "Belgium", "/path", "mono.com"));
  878. container.Add (new Cookie ("Age", "26", "/path", "dev.mono.com"));
  879. CookieCollection cookies = container.GetCookies (new Uri ("http://dev.mono.com/path/ok"));
  880. Assert.IsNotNull (cookies, "#G1");
  881. Assert.AreEqual (2, cookies.Count, "#G2");
  882. Cookie cookie = cookies [0];
  883. Assert.AreEqual ("Age", cookie.Name, "#H1");
  884. Assert.AreEqual ("26", cookie.Value, "#H2");
  885. Assert.AreEqual ("/path", cookie.Path, "#H3");
  886. Assert.AreEqual ("dev.mono.com", cookie.Domain, "#H4");
  887. cookies = container.GetCookies (new Uri ("http://mono.com/path"));
  888. Assert.IsNotNull (cookies, "#I1");
  889. Assert.AreEqual (1, cookies.Count, "#I2");
  890. cookie = cookies [0];
  891. Assert.AreEqual ("Country", cookie.Name, "#J1");
  892. Assert.AreEqual ("Belgium", cookie.Value, "#J2");
  893. Assert.AreEqual ("/path", cookie.Path, "#J3");
  894. Assert.AreEqual ("mono.com", cookie.Domain, "#J4");
  895. cookies = container.GetCookies (new Uri ("http://test.mono.com/path"));
  896. Assert.IsNotNull (cookies, "#K1");
  897. Assert.AreEqual (1, cookies.Count, "#K2");
  898. cookies = container.GetCookies (new Uri ("http://us.dev.mono.com/path"));
  899. Assert.IsNotNull (cookies, "#L1");
  900. Assert.AreEqual (2, cookies.Count, "#L2");
  901. }
  902. [Test]
  903. public void GetCookies2b ()
  904. {
  905. CookieContainer container = new CookieContainer ();
  906. container.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do"),
  907. "Country=Belgium; path=/Whatever; domain=mono.com," +
  908. "Age=26; path=/Whatever; domain=test.mono.com," +
  909. "Weight=87; path=/Whatever/Do; domain=.mono.com;");
  910. CookieCollection cookies = container.GetCookies (new Uri ("http://dev.mono.com/Whatever/Do"));
  911. Assert.IsNotNull (cookies, "#M1");
  912. Assert.AreEqual (2, cookies.Count, "#M2");
  913. Cookie cookie = cookies [0];
  914. Assert.AreEqual ("Weight", cookie.Name, "#N1");
  915. Assert.AreEqual ("87", cookie.Value, "#N2");
  916. Assert.AreEqual ("/Whatever/Do", cookie.Path, "#N3");
  917. Assert.AreEqual (".mono.com", cookie.Domain, "#N4");
  918. cookie = cookies [1];
  919. Assert.AreEqual ("Country", cookie.Name, "#N5");
  920. Assert.AreEqual ("Belgium", cookie.Value, "#N6");
  921. Assert.AreEqual ("/Whatever", cookie.Path, "#N7");
  922. Assert.AreEqual ("mono.com", cookie.Domain, "#N8");
  923. cookies = container.GetCookies (new Uri ("http://test.mono.com/Whatever/Do"));
  924. Assert.IsNotNull (cookies, "#O1");
  925. Assert.AreEqual (3, cookies.Count, "#O2");
  926. cookie = cookies [1];
  927. Assert.AreEqual ("Weight", cookie.Name, "#P1");
  928. Assert.AreEqual ("87", cookie.Value, "#P2");
  929. Assert.AreEqual ("/Whatever/Do", cookie.Path, "#P3");
  930. Assert.AreEqual (".mono.com", cookie.Domain, "#P4");
  931. cookie = cookies [2];
  932. Assert.AreEqual ("Country", cookie.Name, "#P5");
  933. Assert.AreEqual ("Belgium", cookie.Value, "#P6");
  934. Assert.AreEqual ("/Whatever", cookie.Path, "#P7");
  935. Assert.AreEqual ("mono.com", cookie.Domain, "#P8");
  936. cookies = container.GetCookies (new Uri ("http://mono.com/Whatever/Do"));
  937. Assert.IsNotNull (cookies, "#Q1");
  938. Assert.AreEqual (2, cookies.Count, "#Q2");
  939. cookies = container.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do"));
  940. Assert.IsNotNull (cookies, "#R1");
  941. Assert.AreEqual (3, cookies.Count, "#R2");
  942. cookie = cookies [0];
  943. Assert.AreEqual ("Age", cookie.Name, "#S1");
  944. Assert.AreEqual ("26", cookie.Value, "#S2");
  945. Assert.AreEqual ("/Whatever", cookie.Path, "#S3");
  946. Assert.AreEqual ("test.mono.com", cookie.Domain, "#S4");
  947. cookie = cookies [1];
  948. Assert.AreEqual ("Weight", cookie.Name, "#S5");
  949. Assert.AreEqual ("87", cookie.Value, "#S6");
  950. Assert.AreEqual ("/Whatever/Do", cookie.Path, "#S7");
  951. Assert.AreEqual (".mono.com", cookie.Domain, "#S8");
  952. cookie = cookies [2];
  953. Assert.AreEqual ("Country", cookie.Name, "#S9");
  954. Assert.AreEqual ("Belgium", cookie.Value, "#S10");
  955. Assert.AreEqual ("/Whatever", cookie.Path, "#S11");
  956. Assert.AreEqual ("mono.com", cookie.Domain, "#S12");
  957. }
  958. [Test]
  959. public void GetCookies2c ()
  960. {
  961. CookieContainer container = new CookieContainer ();
  962. container.Add (new Cookie ("Height", "178", "/Whatever", "mono.com"));
  963. container.Add (new Cookie ("Town", "Brussels", "/Whatever", ".mono.com"));
  964. container.Add (new Cookie ("Income", "34445", "/Whatever/", ".test.mono.com"));
  965. container.Add (new Cookie ("Sex", "Male", "/WhateveR/DO", ".test.mono.com"));
  966. container.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do/You"),
  967. "Country=Belgium," +
  968. "Age=26; path=/Whatever/Do; domain=test.mono.com," +
  969. "Weight=87; path=/");
  970. CookieCollection cookies = container.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do/Ok"));
  971. Assert.IsNotNull (cookies, "#T1");
  972. Assert.AreEqual (4, cookies.Count, "#T2");
  973. Cookie cookie = cookies [0];
  974. Assert.AreEqual ("Age", cookie.Name, "#U1");
  975. Assert.AreEqual ("26", cookie.Value, "#U2");
  976. Assert.AreEqual ("/Whatever/Do", cookie.Path, "#U3");
  977. Assert.AreEqual ("test.mono.com", cookie.Domain, "#U4");
  978. cookie = cookies [1];
  979. Assert.AreEqual ("Income", cookie.Name, "#U5");
  980. Assert.AreEqual ("34445", cookie.Value, "#U6");
  981. Assert.AreEqual ("/Whatever/", cookie.Path, "#U7");
  982. Assert.AreEqual (".test.mono.com", cookie.Domain, "#U8");
  983. cookie = cookies [3];
  984. Assert.AreEqual ("Town", cookie.Name, "#U9");
  985. Assert.AreEqual ("Brussels", cookie.Value, "#U10");
  986. Assert.AreEqual ("/Whatever", cookie.Path, "#U11");
  987. Assert.AreEqual (".mono.com", cookie.Domain, "#U12");
  988. }
  989. [Test]
  990. public void GetCookies_Uri_Null ()
  991. {
  992. CookieContainer cc = new CookieContainer ();
  993. try {
  994. cc.GetCookies (null);
  995. Assert.Fail ("#1");
  996. }
  997. catch (ArgumentNullException ex) {
  998. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  999. Assert.IsNull (ex.InnerException, "#3");
  1000. Assert.IsNotNull (ex.Message, "#4");
  1001. Assert.AreEqual ("uri", ex.ParamName, "#5");
  1002. }
  1003. }
  1004. [Test]
  1005. // [Category ("NotWorking")]
  1006. public void SetCookies ()
  1007. {
  1008. Uri uri = new Uri ("http://dev.test.mono.com/Whatever/Do/You");
  1009. DateTime now = DateTime.Now;
  1010. CookieContainer cc = new CookieContainer ();
  1011. cc.SetCookies (uri, "Country=Belgium," +
  1012. "Age=26; ; path=/Whatever/Do; domain=test.mono.com," +
  1013. "Weight=87; path=/; ");
  1014. Assert.AreEqual (3, cc.Count, "#A");
  1015. CookieCollection cookies = cc.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do/Ok"));
  1016. Assert.IsNotNull (cookies, "#B1");
  1017. Assert.AreEqual (1, cookies.Count, "#B2");
  1018. Cookie cookie = cookies [0];
  1019. Assert.AreEqual (string.Empty, cookie.Comment, "#C:Comment");
  1020. Assert.IsNull (cookie.CommentUri, "#C:CommentUri");
  1021. Assert.IsFalse (cookie.Discard, "#C:Discard");
  1022. Assert.AreEqual ("test.mono.com", cookie.Domain, "#C:Domain");
  1023. Assert.IsFalse (cookie.Expired, "#C:Expired");
  1024. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#C:Expires");
  1025. Assert.IsFalse (cookie.HttpOnly, "#C:HttpOnly");
  1026. Assert.AreEqual ("Age", cookie.Name, "#C:Name");
  1027. Assert.AreEqual ("/Whatever/Do", cookie.Path, "#C:Path");
  1028. Assert.IsFalse (cookie.Secure, "#C:Secure");
  1029. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#C:TimeStamp1");
  1030. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#C:TimeStamp2");
  1031. Assert.AreEqual ("26", cookie.Value, "#C:Value");
  1032. Assert.AreEqual (0, cookie.Version, "#C:Version");
  1033. cookies = cc.GetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do/Ok"));
  1034. Assert.IsNotNull (cookies, "#D1");
  1035. Assert.AreEqual (2, cookies.Count, "#D2");
  1036. // our sorting is not 100% identical to MS implementation
  1037. for (int i = 0; i < cookies.Count; i++) {
  1038. cookie = cookies [i];
  1039. switch (cookie.Name) {
  1040. case "Weight":
  1041. Assert.AreEqual (string.Empty, cookie.Comment, "#E:Comment");
  1042. Assert.IsNull (cookie.CommentUri, "#E:CommentUri");
  1043. Assert.IsFalse (cookie.Discard, "#E:Discard");
  1044. Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#E:Domain");
  1045. Assert.IsFalse (cookie.Expired, "#E:Expired");
  1046. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#E:Expires");
  1047. Assert.IsFalse (cookie.HttpOnly, "#E:HttpOnly");
  1048. Assert.AreEqual ("Weight", cookie.Name, "#E:Name");
  1049. Assert.AreEqual ("/", cookie.Path, "#E:Path");
  1050. Assert.IsFalse (cookie.Secure, "#E:Secure");
  1051. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#E:TimeStamp1");
  1052. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#E:TimeStamp2");
  1053. Assert.AreEqual ("87", cookie.Value, "#E:Value");
  1054. Assert.AreEqual (0, cookie.Version, "#E:Version");
  1055. break;
  1056. case "Age":
  1057. Assert.AreEqual (string.Empty, cookie.Comment, "#F:Comment");
  1058. Assert.IsNull (cookie.CommentUri, "#F:CommentUri");
  1059. Assert.IsFalse (cookie.Discard, "#F:Discard");
  1060. Assert.AreEqual ("test.mono.com", cookie.Domain, "#F:Domain");
  1061. Assert.IsFalse (cookie.Expired, "#F:Expired");
  1062. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#F:Expires");
  1063. Assert.IsFalse (cookie.HttpOnly, "#F:HttpOnly");
  1064. Assert.AreEqual ("Age", cookie.Name, "#F:Name");
  1065. Assert.AreEqual ("/Whatever/Do", cookie.Path, "#F:Path");
  1066. Assert.IsFalse (cookie.Secure, "#F:Secure");
  1067. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#F:TimeStamp1");
  1068. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#F:TimeStamp2");
  1069. Assert.AreEqual ("26", cookie.Value, "#F:Value");
  1070. Assert.AreEqual (0, cookie.Version, "#F:Version");
  1071. break;
  1072. default:
  1073. Assert.Fail (cookie.Name);
  1074. break;
  1075. }
  1076. }
  1077. cookies = cc.GetCookies (uri);
  1078. Assert.IsNotNull (cookies, "#G1");
  1079. Assert.AreEqual (3, cookies.Count, "#G2");
  1080. // our sorting is not 100% identical to MS implementation
  1081. for (int i = 0; i < cookies.Count; i++) {
  1082. cookie = cookies [i];
  1083. switch (cookie.Name) {
  1084. case "Country":
  1085. Assert.AreEqual (string.Empty, cookie.Comment, "#H:Comment");
  1086. Assert.IsNull (cookie.CommentUri, "#H:CommentUri");
  1087. Assert.IsFalse (cookie.Discard, "#H:Discard");
  1088. Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#H:Domain");
  1089. Assert.IsFalse (cookie.Expired, "#H:Expired");
  1090. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#H:Expires");
  1091. Assert.IsFalse (cookie.HttpOnly, "#H:HttpOnly");
  1092. Assert.AreEqual ("Country", cookie.Name, "#H:Name");
  1093. Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#H:Path");
  1094. Assert.IsFalse (cookie.Secure, "#H:Secure");
  1095. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#H:TimeStamp1");
  1096. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#H:TimeStamp2");
  1097. Assert.AreEqual ("Belgium", cookie.Value, "#H:Value");
  1098. Assert.AreEqual (0, cookie.Version, "#H:Version");
  1099. break;
  1100. case "Weight":
  1101. Assert.AreEqual (string.Empty, cookie.Comment, "#I:Comment");
  1102. Assert.IsNull (cookie.CommentUri, "#I:CommentUri");
  1103. Assert.IsFalse (cookie.Discard, "#I:Discard");
  1104. Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#I:Domain");
  1105. Assert.IsFalse (cookie.Expired, "#I:Expired");
  1106. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#I:Expires");
  1107. Assert.IsFalse (cookie.HttpOnly, "#I:HttpOnly");
  1108. Assert.AreEqual ("Weight", cookie.Name, "#I:Name");
  1109. Assert.AreEqual ("/", cookie.Path, "#I:Path");
  1110. Assert.IsFalse (cookie.Secure, "#I:Secure");
  1111. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#I:TimeStamp1");
  1112. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#I:TimeStamp2");
  1113. Assert.AreEqual ("87", cookie.Value, "#I:Value");
  1114. Assert.AreEqual (0, cookie.Version, "#I:Version");
  1115. break;
  1116. case "Age":
  1117. Assert.AreEqual (string.Empty, cookie.Comment, "#J:Comment");
  1118. Assert.IsNull (cookie.CommentUri, "#J:CommentUri");
  1119. Assert.IsFalse (cookie.Discard, "#J:Discard");
  1120. Assert.AreEqual ("test.mono.com", cookie.Domain, "#J:Domain");
  1121. Assert.IsFalse (cookie.Expired, "#J:Expired");
  1122. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#J:Expires");
  1123. Assert.IsFalse (cookie.HttpOnly, "#J:HttpOnly");
  1124. Assert.AreEqual ("Age", cookie.Name, "#J:Name");
  1125. Assert.AreEqual ("/Whatever/Do", cookie.Path, "#J:Path");
  1126. Assert.IsFalse (cookie.Secure, "#J:Secure");
  1127. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#J:TimeStamp1");
  1128. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#J:TimeStamp2");
  1129. Assert.AreEqual ("26", cookie.Value, "#J:Value");
  1130. Assert.AreEqual (0, cookie.Version, "#J:Version");
  1131. break;
  1132. default:
  1133. Assert.Fail (cookie.Name);
  1134. break;
  1135. }
  1136. }
  1137. cc.SetCookies (uri, "Country=,A");
  1138. cookies = cc.GetCookies (uri);
  1139. Assert.IsNotNull (cookies, "#K1");
  1140. Assert.AreEqual (4, cookies.Count, "#K2");
  1141. cc = new CookieContainer ();
  1142. cc.SetCookies (uri, "Country=,A");
  1143. cookies = cc.GetCookies (uri);
  1144. Assert.IsNotNull (cookies, "#L1");
  1145. Assert.AreEqual (2, cookies.Count, "#L2");
  1146. // our sorting is not 100% identical to MS implementation
  1147. for (int i = 0; i < cookies.Count; i++) {
  1148. cookie = cookies [i];
  1149. switch (cookie.Name) {
  1150. case "Country":
  1151. Assert.AreEqual (string.Empty, cookie.Comment, "#M:Comment");
  1152. Assert.IsNull (cookie.CommentUri, "#M:CommentUri");
  1153. Assert.IsFalse (cookie.Discard, "#M:Discard");
  1154. Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#M:Domain");
  1155. Assert.IsFalse (cookie.Expired, "#M:Expired");
  1156. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#M:Expires");
  1157. Assert.IsFalse (cookie.HttpOnly, "#M:HttpOnly");
  1158. Assert.AreEqual ("Country", cookie.Name, "#M:Name");
  1159. Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#M:Path");
  1160. Assert.IsFalse (cookie.Secure, "#M:Secure");
  1161. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#M:TimeStamp1");
  1162. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#M:TimeStamp2");
  1163. Assert.AreEqual (string.Empty, cookie.Value, "#M:Value");
  1164. Assert.AreEqual (0, cookie.Version, "#M:Version");
  1165. break;
  1166. case "A":
  1167. Assert.AreEqual (string.Empty, cookie.Comment, "#N:Comment");
  1168. Assert.IsNull (cookie.CommentUri, "#N:CommentUri");
  1169. Assert.IsFalse (cookie.Discard, "#N:Discard");
  1170. Assert.AreEqual ("dev.test.mono.com", cookie.Domain, "#N:Domain");
  1171. Assert.IsFalse (cookie.Expired, "#N:Expired");
  1172. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#N:Expires");
  1173. Assert.IsFalse (cookie.HttpOnly, "#N:HttpOnly");
  1174. Assert.AreEqual ("A", cookie.Name, "#N:Name");
  1175. Assert.AreEqual ("/Whatever/Do/You", cookie.Path, "#N:Path");
  1176. Assert.IsFalse (cookie.Secure, "#N:Secure");
  1177. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds >= 0, "#N:TimeStamp1");
  1178. Assert.IsTrue ((cookie.TimeStamp - now).TotalMilliseconds < 1000, "#N:TimeStamp2");
  1179. Assert.AreEqual (string.Empty, cookie.Value, "#N:Value");
  1180. Assert.AreEqual (0, cookie.Version, "#N:Version");
  1181. break;
  1182. default:
  1183. Assert.Fail (cookie.Name);
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. [Test]
  1189. public void SetCookies_CookieHeader_Empty ()
  1190. {
  1191. CookieContainer cc = new CookieContainer ();
  1192. cc.SetCookies (new Uri ("http://www.contoso.com"), string.Empty);
  1193. Assert.AreEqual (0, cc.Count);
  1194. }
  1195. [Test]
  1196. public void SetCookies_CookieHeader_Null ()
  1197. {
  1198. CookieContainer cc = new CookieContainer ();
  1199. try {
  1200. cc.SetCookies (new Uri ("http://www.contoso.com"), null);
  1201. Assert.Fail ("#1");
  1202. }
  1203. catch (ArgumentNullException ex) {
  1204. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1205. Assert.IsNull (ex.InnerException, "#3");
  1206. Assert.IsNotNull (ex.Message, "#4");
  1207. Assert.AreEqual ("cookieHeader", ex.ParamName, "#5");
  1208. }
  1209. }
  1210. [Test]
  1211. public void SetCookies_CookieHeader_Invalid_1 ()
  1212. {
  1213. // cookie format error
  1214. CookieContainer cc = new CookieContainer ();
  1215. try {
  1216. cc.SetCookies (new Uri ("http://www.contoso.com"), "=lalala");
  1217. Assert.Fail ("#A1");
  1218. }
  1219. catch (CookieException ex) {
  1220. // An error has occurred when parsing Cookie
  1221. // header for Uri 'http://www.contoso.com/'
  1222. Assert.AreEqual (typeof (CookieException), ex.GetType (), "#A2");
  1223. Assert.IsNotNull (ex.InnerException, "#A3");
  1224. Assert.IsNotNull (ex.Message, "#A4");
  1225. Assert.IsTrue (ex.Message.IndexOf ("http://www.contoso.com/") != -1, "#A5");
  1226. // Cookie format error
  1227. CookieException inner = ex.InnerException as CookieException;
  1228. Assert.AreEqual (typeof (CookieException), inner.GetType (), "#A6");
  1229. Assert.IsNull (inner.InnerException, "#A7");
  1230. Assert.IsNotNull (inner.Message, "#A8");
  1231. }
  1232. }
  1233. [Test]
  1234. public void SetCookies_CookieHeader_Invalid_2 ()
  1235. {
  1236. // cookie path not part of URI path
  1237. CookieContainer cc = new CookieContainer ();
  1238. try {
  1239. cc.SetCookies (new Uri ("http://dev.test.mono.com/Whatever"),
  1240. "Age=26; path=/Whatever/Do; domain=test.mono.com");
  1241. Assert.Fail ("#B1");
  1242. }
  1243. catch (CookieException ex) {
  1244. // An error has occurred when parsing Cookie
  1245. // header for Uri 'http://dev.test.mono.com/Whatever'
  1246. Assert.AreEqual (typeof (CookieException), ex.GetType (), "#B2");
  1247. Assert.IsNotNull (ex.InnerException, "#B3");
  1248. Assert.IsNotNull (ex.Message, "#B4");
  1249. Assert.IsTrue (ex.Message.IndexOf ("http://dev.test.mono.com/Whatever") != -1, "#B5");
  1250. // The 'Path'='/Whatever/Do' part of the Cookie
  1251. // is invalid
  1252. CookieException inner = ex.InnerException as CookieException;
  1253. Assert.AreEqual (typeof (CookieException), inner.GetType (), "#B6");
  1254. Assert.IsNull (inner.InnerException, "#B7");
  1255. Assert.IsNotNull (inner.Message, "#B8");
  1256. Assert.IsTrue (inner.Message.IndexOf ("'Path'='/Whatever/Do'") != -1 ||
  1257. inner.Message.IndexOf ("\"Path\"=\"/Whatever/Do\"") != 1, "#B9");
  1258. }
  1259. }
  1260. [Test]
  1261. public void SetCookies_Uri_Null ()
  1262. {
  1263. CookieContainer cc = new CookieContainer ();
  1264. try {
  1265. cc.SetCookies (null, "Age=26; path=/Whatever; domain=test.mono.com");
  1266. Assert.Fail ("#1");
  1267. }
  1268. catch (ArgumentNullException ex) {
  1269. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1270. Assert.IsNull (ex.InnerException, "#3");
  1271. Assert.IsNotNull (ex.Message, "#4");
  1272. Assert.AreEqual ("uri", ex.ParamName, "#5");
  1273. }
  1274. }
  1275. [Test]
  1276. public void SetCookies_DomainMatchesHost ()
  1277. {
  1278. CookieContainer cc = new CookieContainer ();
  1279. // domains looks identical - but "domain=test.mono.com" means "*.test.mono.com"
  1280. cc.SetCookies (new Uri ("http://test.mono.com/Whatever/Do"),
  1281. "Age=26; path=/Whatever; domain=test.mono.com");
  1282. CookieCollection cookies = cc.GetCookies (new Uri ("http://test.mono.com/Whatever/Do"));
  1283. Assert.IsNotNull (cookies, "#A1");
  1284. Assert.AreEqual (1, cookies.Count, "#A2");
  1285. cookies = cc.GetCookies (new Uri ("http://us.test.mono.com/Whatever/Do"));
  1286. Assert.IsNotNull (cookies, "#A3");
  1287. Assert.AreEqual (1, cookies.Count, "#A4");
  1288. }
  1289. #if FIXME
  1290. [Test]
  1291. public void SetCookies_Domain_Local ()
  1292. {
  1293. CookieContainer cc;
  1294. CookieCollection cookies;
  1295. string hostname = Dns.GetHostName ();
  1296. cc = new CookieContainer ();
  1297. cc.SetCookies (new Uri ("http://localhost/Whatever/Do"),
  1298. "Age=26; path=/Whatever; domain=.local");
  1299. cookies = cc.GetCookies (new Uri ("http://localhost/Whatever/Do"));
  1300. Assert.IsNotNull (cookies, "#A1");
  1301. Assert.AreEqual (0, cookies.Count, "#A2");
  1302. cookies = cc.GetCookies (new Uri ("http://127.0.0.1/Whatever/Do"));
  1303. Assert.IsNotNull (cookies, "#A3");
  1304. Assert.AreEqual (0, cookies.Count, "#A4");
  1305. cookies = cc.GetCookies (new Uri ("http://" + hostname + "/Whatever/Do"));
  1306. Assert.IsNotNull (cookies, "#A5");
  1307. Assert.AreEqual (hostname.EndsWith (".local") ? 1 : 0, cookies.Count, "#A6");
  1308. cc = new CookieContainer ();
  1309. cc.SetCookies (new Uri ("http://127.0.0.1/Whatever/Do"),
  1310. "Age=26; path=/Whatever; domain=.local");
  1311. cookies = cc.GetCookies (new Uri ("http://localhost/Whatever/Do"));
  1312. Assert.IsNotNull (cookies, "#B1");
  1313. Assert.AreEqual (0, cookies.Count, "#B2");
  1314. cookies = cc.GetCookies (new Uri ("http://127.0.0.1/Whatever/Do"));
  1315. Assert.IsNotNull (cookies, "#B3");
  1316. Assert.AreEqual (0, cookies.Count, "#B4");
  1317. cookies = cc.GetCookies (new Uri ("http://" + hostname + "/Whatever/Do"));
  1318. Assert.IsNotNull (cookies, "#B5");
  1319. Assert.AreEqual (hostname.EndsWith (".local") ? 1 : 0, cookies.Count, "#B6");
  1320. cc = new CookieContainer ();
  1321. cc.SetCookies (new Uri ("http://" + hostname + "/Whatever/Do"),
  1322. "Age=26; path=/Whatever; domain=.local");
  1323. cookies = cc.GetCookies (new Uri ("http://localhost/Whatever/Do"));
  1324. Assert.IsNotNull (cookies, "#C1");
  1325. Assert.AreEqual (0, cookies.Count, "#C2");
  1326. cookies = cc.GetCookies (new Uri ("http://127.0.0.1/Whatever/Do"));
  1327. Assert.IsNotNull (cookies, "#C3");
  1328. Assert.AreEqual (0, cookies.Count, "#C4");
  1329. cookies = cc.GetCookies (new Uri ("http://" + hostname + "/Whatever/Do"));
  1330. Assert.IsNotNull (cookies, "#C5");
  1331. Assert.AreEqual (hostname.EndsWith (".local") ? 1 : 0, cookies.Count, "#C6");
  1332. }
  1333. #endif
  1334. [Test]
  1335. public void bug421827 ()
  1336. {
  1337. CookieContainer container;
  1338. CookieCollection cookies;
  1339. Cookie cookie;
  1340. container = new CookieContainer ();
  1341. container.SetCookies (new Uri ("http://test.mono.com/Whatever/Do"),
  1342. "Country=Belgium; path=/Whatever; domain=mono.com");
  1343. cookies = container.GetCookies (new Uri ("http://dev.mono.com/Whatever"));
  1344. Assert.AreEqual (1, cookies.Count, "#A1");
  1345. cookie = cookies [0];
  1346. Assert.AreEqual ("Country", cookie.Name, "#A2");
  1347. Assert.AreEqual ("mono.com", cookie.Domain, "#A3");
  1348. Assert.AreEqual ("/Whatever", cookie.Path, "#A4");
  1349. Assert.AreEqual (0, cookie.Version, "#A5");
  1350. container = new CookieContainer ();
  1351. container.SetCookies (new Uri ("http://sub.mono.com/Whatever/Do"),
  1352. "Country=Belgium; path=/Whatever; domain=mono.com");
  1353. cookies = container.GetCookies (new Uri ("http://gomono.com/Whatever"));
  1354. Assert.AreEqual (0, cookies.Count, "#B");
  1355. container = new CookieContainer ();
  1356. container.SetCookies (new Uri ("http://sub.mono.com/Whatever/Do"),
  1357. "Country=Belgium; path=/Whatever; domain=mono.com");
  1358. cookies = container.GetCookies (new Uri ("http://amono.com/Whatever"));
  1359. Assert.AreEqual (0, cookies.Count, "#C");
  1360. }
  1361. [Test]
  1362. public void MoreThanDefaultDomainCookieLimit ()
  1363. {
  1364. CookieContainer cc = new CookieContainer ();
  1365. for (int i = 1; i <= CookieContainer.DefaultPerDomainCookieLimit; i++) {
  1366. Cookie c = new Cookie (i.ToString (), i.ToString (), "/", "mono.com");
  1367. cc.Add (c);
  1368. }
  1369. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, cc.Count, "Count");
  1370. Cookie c2 = new Cookie ("uho", "21", "/", "mono.com");
  1371. cc.Add (c2);
  1372. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, cc.Count, "Count");
  1373. // so one (yes '1' ;-) was removed
  1374. }
  1375. [Test]
  1376. public void MoreThanDefaultCookieLimit ()
  1377. {
  1378. CookieContainer cc = new CookieContainer ();
  1379. for (int i = 1; i <= CookieContainer.DefaultCookieLimit; i++) {
  1380. Cookie c = new Cookie (i.ToString (), i.ToString (), "/", "www" + i.ToString () + ".mono.com");
  1381. cc.Add (c);
  1382. }
  1383. Assert.AreEqual (CookieContainer.DefaultCookieLimit, cc.Count, "Count");
  1384. Cookie c2 = new Cookie ("uho", "301", "/", "www301.mono.com");
  1385. cc.Add (c2);
  1386. Assert.AreEqual (CookieContainer.DefaultCookieLimit, cc.Count, "Count");
  1387. // so one (yes '1' ;-) was removed
  1388. }
  1389. [Test]
  1390. public void SaveAndLoadViaAddUriCookie ()
  1391. {
  1392. Cookie cookie = new Cookie ("name", "value")
  1393. {
  1394. Domain = ".example.com",
  1395. Expires = new DateTime (2100, 1, 1, 0, 0, 0, DateTimeKind.Utc),
  1396. HttpOnly = true,
  1397. Secure = true,
  1398. };
  1399. Uri uri = new Uri ("https://www.example.com/path/file");
  1400. CookieContainer container = new CookieContainer ();
  1401. container.Add (uri, cookie);
  1402. CookieCollection collection = container.GetCookies (uri);
  1403. Assert.AreEqual (collection.Count, 1, "#A1");
  1404. Cookie cloned = collection [0];
  1405. Assert.AreEqual (cookie.Comment, cloned.Comment, "#A2");
  1406. Assert.AreEqual (cookie.CommentUri, cloned.CommentUri, "#A3");
  1407. Assert.AreEqual (cookie.Domain, cloned.Domain, "#A4");
  1408. Assert.AreEqual (cookie.Discard, cloned.Discard, "#A5");
  1409. Assert.AreEqual (cookie.Expired, cloned.Expired, "#A6");
  1410. Assert.AreEqual (cookie.Expires.ToUniversalTime (), cloned.Expires.ToUniversalTime (), "#A7");
  1411. Assert.AreEqual (cookie.HttpOnly, cloned.HttpOnly, "#A8");
  1412. Assert.AreEqual (cookie.Name, cloned.Name, "#A9");
  1413. Assert.AreEqual ("/path/file", cloned.Path, "#A10");
  1414. Assert.AreEqual (cookie.Port, cloned.Port, "#A11");
  1415. Assert.AreEqual (cookie.Value, cloned.Value, "#A12");
  1416. Assert.AreEqual (cookie.Version, cloned.Version, "#A13");
  1417. Assert.AreEqual (cookie.Secure, cloned.Secure, "#A14");
  1418. }
  1419. [Test]
  1420. public void SaveAndLoadViaSetCookies ()
  1421. {
  1422. Cookie cookie = new Cookie ("name", "value")
  1423. {
  1424. Domain = ".example.com",
  1425. Expires = new DateTime (2100, 1, 1, 0, 0, 0, DateTimeKind.Utc),
  1426. HttpOnly = true,
  1427. Secure = true,
  1428. };
  1429. Uri uri = new Uri ("https://www.example.com/path/file");
  1430. CookieContainer container = new CookieContainer ();
  1431. container.SetCookies (uri, "name=value; domain=.example.com; expires=Fri, 01-Jan-2100 00:00:00 GMT; HttpOnly; secure");
  1432. CookieCollection collection = container.GetCookies (uri);
  1433. Assert.AreEqual (collection.Count, 1, "#A1");
  1434. Cookie cloned = collection [0];
  1435. Assert.AreEqual (cookie.Comment, cloned.Comment, "#A2");
  1436. Assert.AreEqual (cookie.CommentUri, cloned.CommentUri, "#A3");
  1437. Assert.AreEqual (cookie.Domain, cloned.Domain, "#A4");
  1438. Assert.AreEqual (cookie.Discard, cloned.Discard, "#A5");
  1439. Assert.AreEqual (cookie.Expired, cloned.Expired, "#A6");
  1440. Assert.AreEqual (cookie.Expires.ToUniversalTime (), cloned.Expires.ToUniversalTime (), "#A7");
  1441. Assert.AreEqual (cookie.HttpOnly, cloned.HttpOnly, "#A8");
  1442. Assert.AreEqual (cookie.Name, cloned.Name, "#A9");
  1443. Assert.AreEqual (cookie.Path, "", "#A10");
  1444. Assert.AreEqual (cloned.Path, "/path/file", "#A11");
  1445. Assert.AreEqual (cookie.Port, cloned.Port, "#A12");
  1446. Assert.AreEqual (cookie.Value, cloned.Value, "#A13");
  1447. Assert.AreEqual (cookie.Version, cloned.Version, "#A14");
  1448. Assert.AreEqual (cookie.Secure, cloned.Secure, "#A15");
  1449. }
  1450. }
  1451. }