CookieContainerTest.cs 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  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. [Test]
  577. public void Add6_Insecure ()
  578. {
  579. var cc = new CookieContainer ();
  580. var cookie = new Cookie ("Foo", "Bar", string.Empty, ".mono.com");
  581. cookie.Secure = true;
  582. // FIXME: This should throw an exception - but .NET 4.5 does not.
  583. cc.Add (new Uri ("http://mono.com/"), cookie);
  584. var coll = cc.GetCookies (new Uri ("http://mono.com/"));
  585. Assert.AreEqual (0, coll.Count, "#A1");
  586. }
  587. [Test]
  588. public void TestAdd_Cookie ()
  589. {
  590. CookieContainer cc = new CookieContainer ();
  591. Uri uri = new Uri ("http://www.contoso.com");
  592. cc.Add (uri, new CookieCollection ());
  593. DateTime timestamp = DateTime.Now;
  594. cc.Add (uri, new Cookie ("hola", "Adios"));
  595. CookieCollection coll = cc.GetCookies (uri);
  596. Cookie cookie = coll [0];
  597. Assert.AreEqual ("", cookie.Comment, "#1");
  598. Assert.IsNull (cookie.CommentUri, "#2");
  599. Assert.AreEqual ("www.contoso.com", cookie.Domain, "#3");
  600. Assert.IsFalse (cookie.Expired, "#4");
  601. Assert.AreEqual (DateTime.MinValue, cookie.Expires, "#5");
  602. Assert.AreEqual ("hola", cookie.Name, "#6");
  603. Assert.AreEqual ("/", cookie.Path, "#7");
  604. Assert.AreEqual ("", cookie.Port, "#8");
  605. Assert.IsFalse (cookie.Secure, "#9");
  606. // FIX the next test
  607. TimeSpan ts = cookie.TimeStamp - timestamp;
  608. if (ts.TotalMilliseconds > 500)
  609. Assert.Fail ("#10");
  610. Assert.AreEqual ("Adios", cookie.Value, "#11");
  611. Assert.AreEqual (0, cookie.Version, "#12");
  612. }
  613. [Test]
  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. [Test]
  1290. public void bug421827 ()
  1291. {
  1292. CookieContainer container;
  1293. CookieCollection cookies;
  1294. Cookie cookie;
  1295. container = new CookieContainer ();
  1296. container.SetCookies (new Uri ("http://test.mono.com/Whatever/Do"),
  1297. "Country=Belgium; path=/Whatever; domain=mono.com");
  1298. cookies = container.GetCookies (new Uri ("http://dev.mono.com/Whatever"));
  1299. Assert.AreEqual (1, cookies.Count, "#A1");
  1300. cookie = cookies [0];
  1301. Assert.AreEqual ("Country", cookie.Name, "#A2");
  1302. Assert.AreEqual ("mono.com", cookie.Domain, "#A3");
  1303. Assert.AreEqual ("/Whatever", cookie.Path, "#A4");
  1304. Assert.AreEqual (0, cookie.Version, "#A5");
  1305. container = new CookieContainer ();
  1306. container.SetCookies (new Uri ("http://sub.mono.com/Whatever/Do"),
  1307. "Country=Belgium; path=/Whatever; domain=mono.com");
  1308. cookies = container.GetCookies (new Uri ("http://gomono.com/Whatever"));
  1309. Assert.AreEqual (0, cookies.Count, "#B");
  1310. container = new CookieContainer ();
  1311. container.SetCookies (new Uri ("http://sub.mono.com/Whatever/Do"),
  1312. "Country=Belgium; path=/Whatever; domain=mono.com");
  1313. cookies = container.GetCookies (new Uri ("http://amono.com/Whatever"));
  1314. Assert.AreEqual (0, cookies.Count, "#C");
  1315. }
  1316. [Test]
  1317. public void MoreThanDefaultDomainCookieLimit ()
  1318. {
  1319. CookieContainer cc = new CookieContainer ();
  1320. for (int i = 1; i <= CookieContainer.DefaultPerDomainCookieLimit; i++) {
  1321. Cookie c = new Cookie (i.ToString (), i.ToString (), "/", "mono.com");
  1322. cc.Add (c);
  1323. }
  1324. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, cc.Count, "Count");
  1325. Cookie c2 = new Cookie ("uho", "21", "/", "mono.com");
  1326. cc.Add (c2);
  1327. Assert.AreEqual (CookieContainer.DefaultPerDomainCookieLimit, cc.Count, "Count");
  1328. // so one (yes '1' ;-) was removed
  1329. }
  1330. [Test]
  1331. public void MoreThanDefaultCookieLimit ()
  1332. {
  1333. CookieContainer cc = new CookieContainer ();
  1334. for (int i = 1; i <= CookieContainer.DefaultCookieLimit; i++) {
  1335. Cookie c = new Cookie (i.ToString (), i.ToString (), "/", "www" + i.ToString () + ".mono.com");
  1336. cc.Add (c);
  1337. }
  1338. Assert.AreEqual (CookieContainer.DefaultCookieLimit, cc.Count, "Count");
  1339. Cookie c2 = new Cookie ("uho", "301", "/", "www301.mono.com");
  1340. cc.Add (c2);
  1341. Assert.AreEqual (CookieContainer.DefaultCookieLimit, cc.Count, "Count");
  1342. // so one (yes '1' ;-) was removed
  1343. }
  1344. [Test]
  1345. public void SaveAndLoadViaAddUriCookie ()
  1346. {
  1347. Cookie cookie = new Cookie ("name", "value")
  1348. {
  1349. Domain = ".example.com",
  1350. Expires = new DateTime (2100, 1, 1, 0, 0, 0, DateTimeKind.Utc),
  1351. HttpOnly = true,
  1352. Secure = true,
  1353. };
  1354. Uri uri = new Uri ("https://www.example.com/path/file");
  1355. CookieContainer container = new CookieContainer ();
  1356. container.Add (uri, cookie);
  1357. CookieCollection collection = container.GetCookies (uri);
  1358. Assert.AreEqual (collection.Count, 1, "#A1");
  1359. Cookie cloned = collection [0];
  1360. Assert.AreEqual (cookie.Comment, cloned.Comment, "#A2");
  1361. Assert.AreEqual (cookie.CommentUri, cloned.CommentUri, "#A3");
  1362. Assert.AreEqual (cookie.Domain, cloned.Domain, "#A4");
  1363. Assert.AreEqual (cookie.Discard, cloned.Discard, "#A5");
  1364. Assert.AreEqual (cookie.Expired, cloned.Expired, "#A6");
  1365. Assert.AreEqual (cookie.Expires.ToUniversalTime (), cloned.Expires.ToUniversalTime (), "#A7");
  1366. Assert.AreEqual (cookie.HttpOnly, cloned.HttpOnly, "#A8");
  1367. Assert.AreEqual (cookie.Name, cloned.Name, "#A9");
  1368. Assert.AreEqual ("/path/file", cloned.Path, "#A10");
  1369. Assert.AreEqual (cookie.Port, cloned.Port, "#A11");
  1370. Assert.AreEqual (cookie.Value, cloned.Value, "#A12");
  1371. Assert.AreEqual (cookie.Version, cloned.Version, "#A13");
  1372. Assert.AreEqual (cookie.Secure, cloned.Secure, "#A14");
  1373. }
  1374. [Test]
  1375. public void SaveAndLoadViaSetCookies ()
  1376. {
  1377. Cookie cookie = new Cookie ("name", "value")
  1378. {
  1379. Domain = ".example.com",
  1380. Expires = new DateTime (2100, 1, 1, 0, 0, 0, DateTimeKind.Utc),
  1381. HttpOnly = true,
  1382. Secure = true,
  1383. };
  1384. Uri uri = new Uri ("https://www.example.com/path/file");
  1385. CookieContainer container = new CookieContainer ();
  1386. container.SetCookies (uri, "name=value; domain=.example.com; expires=Fri, 01-Jan-2100 00:00:00 GMT; HttpOnly; secure");
  1387. CookieCollection collection = container.GetCookies (uri);
  1388. Assert.AreEqual (collection.Count, 1, "#A1");
  1389. Cookie cloned = collection [0];
  1390. Assert.AreEqual (cookie.Comment, cloned.Comment, "#A2");
  1391. Assert.AreEqual (cookie.CommentUri, cloned.CommentUri, "#A3");
  1392. Assert.AreEqual (cookie.Domain, cloned.Domain, "#A4");
  1393. Assert.AreEqual (cookie.Discard, cloned.Discard, "#A5");
  1394. Assert.AreEqual (cookie.Expired, cloned.Expired, "#A6");
  1395. Assert.AreEqual (cookie.Expires.ToUniversalTime (), cloned.Expires.ToUniversalTime (), "#A7");
  1396. Assert.AreEqual (cookie.HttpOnly, cloned.HttpOnly, "#A8");
  1397. Assert.AreEqual (cookie.Name, cloned.Name, "#A9");
  1398. Assert.AreEqual (cookie.Path, "", "#A10");
  1399. Assert.AreEqual (cloned.Path, "/path/file", "#A11");
  1400. Assert.AreEqual (cookie.Port, cloned.Port, "#A12");
  1401. Assert.AreEqual (cookie.Value, cloned.Value, "#A13");
  1402. Assert.AreEqual (cookie.Version, cloned.Version, "#A14");
  1403. Assert.AreEqual (cookie.Secure, cloned.Secure, "#A15");
  1404. }
  1405. }
  1406. }