PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/default_site/library/yui/tests/cookie.html

https://github.com/gisanfu/project-and-template-manage-system
HTML | 1415 lines | 1092 code | 323 blank | 0 comment | 0 complexity | 769cb1211df7511639fc7d07525c8b9f MD5 | raw file
Possible License(s): LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <html>
  2. <head>
  3. <title>cookie tests</title>
  4. <link type="text/css" rel="stylesheet" href="../build/logger/assets/logger.css" />
  5. <link type="text/css" rel="stylesheet" href="../build/yuitest/assets/testlogger.css" />
  6. <script type="text/javascript" src="../build/yahoo-dom-event/yahoo-dom-event.js"></script>
  7. <script type="text/javascript" src="../build/cookie/cookie-beta.js"></script>
  8. <script type="text/javascript" src="../build/profiler/profiler-beta.js"></script>
  9. <script type="text/javascript" src="../build/logger/logger.js"></script>
  10. <script type="text/javascript" src="../build/yuitest/yuitest.js"></script>
  11. </head>
  12. <body>
  13. <h1>cookie tests</h1>
  14. <p><input type="button" value="Run Tests" id="btnRun" /></p>
  15. <script type="text/javascript">
  16. YAHOO.namespace("test");
  17. YAHOO.test.cookie_test = (function(){
  18. var Assert = YAHOO.util.Assert;
  19. var ObjectAssert = YAHOO.util.ObjectAssert;
  20. var Profiler = YAHOO.tool.Profiler;
  21. var YUE /*:Object*/ = YAHOO.util.Event;
  22. var YUD /*:Object*/ = YAHOO.util.Dom;
  23. //utility function
  24. function deleteCookie(name, detail){
  25. document.cookie = name + "=blah; " + (detail || "") + " expires=Thu, 01 Jan 1970 00:00:00 GMT";
  26. }
  27. function setCookie(name, value){
  28. document.cookie = (name) + "=" + (value);
  29. }
  30. //-------------------------------------------------------------------------
  31. // Base Test Suite
  32. //-------------------------------------------------------------------------
  33. var suite = new YAHOO.tool.TestSuite("Cookie Tests");
  34. //-------------------------------------------------------------------------
  35. // Test Case for parsing capabilities
  36. //-------------------------------------------------------------------------
  37. suite.add(new YAHOO.tool.TestCase({
  38. name : "Cookie Parsing Tests",
  39. //---------------------------------------------------------------------
  40. // Tests
  41. //---------------------------------------------------------------------
  42. testParseCookieStringEmpty : function(){
  43. var cookies = YAHOO.util.Cookie._parseCookieString("");
  44. },
  45. testParseCookieStringNull : function(){
  46. var cookies = YAHOO.util.Cookie._parseCookieString(null);
  47. },
  48. testParseCookieStringBoolean : function(){
  49. var cookies = YAHOO.util.Cookie._parseCookieString(true);
  50. },
  51. testParseCookieStringBoolean : function(){
  52. var cookies = YAHOO.util.Cookie._parseCookieString(true);
  53. },
  54. testParseCookieStringUndefined : function(){
  55. var cookies = YAHOO.util.Cookie._parseCookieString();
  56. },
  57. testParseCookieStringInvalid : function(){
  58. var cookies = YAHOO.util.Cookie._parseCookieString("a");
  59. },
  60. testParseCookieStringSimple : function(){
  61. var cookieString = "a=b";
  62. var cookies = YAHOO.util.Cookie._parseCookieString(cookieString);
  63. ObjectAssert.hasProperty("a", cookies, "Cookie 'a' is missing.");
  64. Assert.areEqual("b", cookies.a, "Cookie 'a' should have value 'b'.");
  65. },
  66. testParseCookieStringNumber : function(){
  67. var cookieString = "12345=b";
  68. var cookies = YAHOO.util.Cookie._parseCookieString(cookieString);
  69. ObjectAssert.hasProperty("12345", cookies, "Cookie '12345' is missing.");
  70. Assert.areEqual("b", cookies["12345"], "Cookie '12345' should have value 'b'.");
  71. },
  72. testParseCookieStringSimpleMulti : function(){
  73. var cookieString = "a=b; c=d; e=f; g=h";
  74. var cookies = YAHOO.util.Cookie._parseCookieString(cookieString);
  75. ObjectAssert.hasProperty("a", cookies, "Cookie 'a' is missing.");
  76. ObjectAssert.hasProperty("c", cookies, "Cookie 'c' is missing.");
  77. ObjectAssert.hasProperty("e", cookies, "Cookie 'e' is missing.");
  78. ObjectAssert.hasProperty("g", cookies, "Cookie 'g' is missing.");
  79. Assert.areEqual("b", cookies.a, "Cookie 'a' should have value 'b'.");
  80. Assert.areEqual("d", cookies.c, "Cookie 'c' should have value 'd'.");
  81. Assert.areEqual("f", cookies.e, "Cookie 'e' should have value 'f'.");
  82. Assert.areEqual("h", cookies.g, "Cookie 'g' should have value 'h'.");
  83. },
  84. testParseCookieStringComplex : function(){
  85. var cookieString = "name=Nicholas%20Zakas; title=front%20end%20engineer";
  86. var cookies = YAHOO.util.Cookie._parseCookieString(cookieString);
  87. ObjectAssert.hasProperty("name", cookies, "Cookie 'name' is missing.");
  88. ObjectAssert.hasProperty("title", cookies, "Cookie 'title' is missing.");
  89. Assert.areEqual("Nicholas Zakas", cookies.name, "Cookie 'name' should have value 'Nicholas Zakas'.");
  90. Assert.areEqual("front end engineer", cookies.title, "Cookie 'title' should have value 'front end engineer'.");
  91. },
  92. testParseCookieStringNetwork : function(){
  93. var cookieString = "B=2nk0a3t3lj7cr&b=3&s=13; LYC=l_v=2&l_lv=10&l_l=94ddoa70d&l_s=qz54t4qwrsqquyv51w0z4xxwtx31x1t0&l_lid=146p1u6&l_r=4q&l_lc=0_0_0_0_0&l_mpr=50_0_0&l_um=0_0_1_0_0;YMRAD=1215072198*0_0_7318647_1_0_40123839_1; l%5FPD3=840";
  94. var cookies = YAHOO.util.Cookie._parseCookieString(cookieString);
  95. ObjectAssert.hasProperty("B", cookies, "Cookie 'B' is missing.");
  96. ObjectAssert.hasProperty("LYC", cookies, "Cookie 'LYC' is missing.");
  97. ObjectAssert.hasProperty("l_PD3", cookies, "Cookie 'l_PD3' is missing.");
  98. },
  99. testParseCookieStringWithEscapedCharactersInCookieName: function(){
  100. var cookieName = "something[1]";
  101. var cookieValue = "123";
  102. var cookieString = encodeURIComponent(cookieName) + "=" + encodeURIComponent(cookieValue);
  103. var cookies = YAHOO.util.Cookie._parseCookieString(cookieString);
  104. ObjectAssert.hasProperty(cookieName, cookies, "Cookie '" + cookieName + "' is missing.");
  105. Assert.areEqual(cookieValue, cookies[cookieName], "Cookie value for '" + cookieName + "' is wrong.");
  106. },
  107. testParseCookieStringIncorrectFormat: function(){
  108. var cookieString = "SESSION=27bedbdf3d35252d0db07f34d81dcca6; STATS=OK; SCREEN=1280x1024; undefined; ys-bottom-preview=o%3Aheight%3Dn%253A389"
  109. var cookies = YAHOO.util.Cookie._parseCookieString(cookieString);
  110. ObjectAssert.hasProperty("SCREEN", cookies, "Cookie 'SCREEN' is missing.");
  111. ObjectAssert.hasProperty("STATS", cookies, "Cookie 'STATS' is missing.");
  112. ObjectAssert.hasProperty("SESSION", cookies, "Cookie 'SESSION' is missing.");
  113. ObjectAssert.hasProperty("ys-bottom-preview", cookies, "Cookie 'ys-bottom-preview' is missing.");
  114. ObjectAssert.hasProperty("undefined", cookies, "Cookie 'undefined' is missing.");
  115. },
  116. testParseCookieStringWithHash : function(){
  117. var cookieString = "name=Nicholas%20Zakas; hash=a=b&c=d&e=f&g=h; title=front%20end%20engineer";
  118. var cookies = YAHOO.util.Cookie._parseCookieString(cookieString);
  119. ObjectAssert.hasProperty("name", cookies, "Cookie 'name' is missing.");
  120. ObjectAssert.hasProperty("hash", cookies, "Cookie 'hash' is missing.");
  121. ObjectAssert.hasProperty("title", cookies, "Cookie 'title' is missing.");
  122. Assert.areEqual("Nicholas Zakas", cookies.name, "Cookie 'name' should have value 'Nicholas Zakas'.");
  123. Assert.areEqual("a=b&c=d&e=f&g=h", cookies.hash, "Cookie 'hash' should have value 'a=b&c=d&e=f&g=h'.");
  124. Assert.areEqual("front end engineer", cookies.title, "Cookie 'title' should have value 'front end engineer'.");
  125. },
  126. testParseCookieHash : function () {
  127. var cookieHash = "a=b&c=d&e=f&g=h";
  128. var hash = YAHOO.util.Cookie._parseCookieHash(cookieHash);
  129. ObjectAssert.hasProperty("a", hash, "Hash 'a' is missing.");
  130. ObjectAssert.hasProperty("c", hash, "Hash 'c' is missing.");
  131. ObjectAssert.hasProperty("e", hash, "Hash 'e' is missing.");
  132. ObjectAssert.hasProperty("g", hash, "Hash 'g' is missing.");
  133. Assert.areEqual("b", hash.a, "Hash 'a' should have value 'b'.");
  134. Assert.areEqual("d", hash.c, "Hash 'c' should have value 'd'.");
  135. Assert.areEqual("f", hash.e, "Hash 'e' should have value 'f'.");
  136. Assert.areEqual("h", hash.g, "Hash 'g' should have value 'h'.");
  137. },
  138. testParseCookieHashComplex : function () {
  139. var cookieName = "something[1]";
  140. var cookieValue = "123";
  141. var cookieHash = encodeURIComponent(cookieName) + "=" + encodeURIComponent(cookieValue);
  142. var hash = YAHOO.util.Cookie._parseCookieHash(cookieHash);
  143. ObjectAssert.hasProperty(cookieName, hash, "Hash 'something[1]' is missing.");
  144. Assert.areEqual(cookieValue, hash[cookieName], "Hash 'a' should have value 'b'.");
  145. },
  146. testParseCookieHashEmpty: function(){
  147. var hash = YAHOO.util.Cookie._parseCookieHash("");
  148. Assert.isFalse("" in hash, "Hash shouldn't have an empty string property.");
  149. }
  150. }));
  151. //-------------------------------------------------------------------------
  152. // Test Case for string formatting capabilities
  153. //-------------------------------------------------------------------------
  154. suite.add(new YAHOO.tool.TestCase({
  155. name : "Cookie String Creation Tests",
  156. //---------------------------------------------------------------------
  157. // Tests
  158. //---------------------------------------------------------------------
  159. testCreateCookieStringSimple : function(){
  160. var text = YAHOO.util.Cookie._createCookieString("name", "value", true);
  161. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  162. },
  163. testCreateCookieStringSimpleWithPath : function(){
  164. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { path: "/" });
  165. Assert.areEqual("name=value; path=/", text, "Cookie string is incorrect.");
  166. },
  167. testCreateCookieStringSimpleWithInvalidPath1 : function(){
  168. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { path: 25 });
  169. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  170. },
  171. testCreateCookieStringSimpleWithInvalidPath2 : function(){
  172. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { path: "" });
  173. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  174. },
  175. testCreateCookieStringSimpleWithDomain : function(){
  176. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { domain: "yahoo.com" });
  177. Assert.areEqual("name=value; domain=yahoo.com", text, "Cookie string is incorrect.");
  178. },
  179. testCreateCookieStringSimpleWithInvalidDomain1 : function(){
  180. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { domain: true });
  181. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  182. },
  183. testCreateCookieStringSimpleWithInvalidDomain2 : function(){
  184. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { domain: "" });
  185. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  186. },
  187. testCreateCookieStringSimpleWithSecure : function(){
  188. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { secure: true });
  189. Assert.areEqual("name=value; secure", text, "Cookie string is incorrect.");
  190. },
  191. testCreateCookieStringSimpleWithInvalidSecure1 : function(){
  192. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { secure: false });
  193. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  194. },
  195. testCreateCookieStringSimpleWithInvalidSecure2 : function(){
  196. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { secure: "blah" });
  197. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  198. },
  199. testCreateCookieStringSimpleWithExpiry : function(){
  200. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  201. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { expires: expires });
  202. Assert.areEqual("name=value; expires=" + expires.toGMTString(), text, "Cookie string is incorrect.");
  203. },
  204. testCreateCookieStringSimpleWithInvalidExpiry : function(){
  205. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { expires: "blah" });
  206. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  207. },
  208. testCreateCookieStringSimpleWithAll : function(){
  209. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  210. var text = YAHOO.util.Cookie._createCookieString("name", "value", true, { expires: expires, domain : "yahoo.com", path: "/", secure: true });
  211. Assert.areEqual("name=value; expires=" + expires.toGMTString() + "; path=/; domain=yahoo.com; secure", text, "Cookie string is incorrect.");
  212. },
  213. testCreateCookieStringComplex : function(){
  214. var name = "c.f name";
  215. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  216. var text = YAHOO.util.Cookie._createCookieString(name, value, true);
  217. Assert.areEqual(encodeURIComponent(name) + "=" + encodeURIComponent(value), text, "Cookie string is incorrect.");
  218. },
  219. testCreateCookieStringComplexWithPath : function(){
  220. var name = "c.f name";
  221. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  222. var text = YAHOO.util.Cookie._createCookieString(name, value, true, { path : "/" });
  223. Assert.areEqual(encodeURIComponent(name) + "=" + encodeURIComponent(value) + "; path=/", text, "Cookie string is incorrect.");
  224. },
  225. testCreateCookieStringComplexWithDomain : function(){
  226. var name = "c.f name";
  227. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  228. var text = YAHOO.util.Cookie._createCookieString(name, value, true, { domain: "yahoo.com" });
  229. Assert.areEqual(encodeURIComponent(name) + "=" + encodeURIComponent(value) + "; domain=yahoo.com", text, "Cookie string is incorrect.");
  230. },
  231. testCreateCookieStringComplexWithSecure : function(){
  232. var name = "c.f name";
  233. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  234. var text = YAHOO.util.Cookie._createCookieString(name, value, true, { secure: true });
  235. Assert.areEqual(encodeURIComponent(name) + "=" + encodeURIComponent(value) + "; secure", text, "Cookie string is incorrect.");
  236. },
  237. testCreateCookieStringComplexWithExpiry : function(){
  238. var name = "c.f name";
  239. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  240. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  241. var text = YAHOO.util.Cookie._createCookieString(name, value, true, { expires : expires });
  242. Assert.areEqual(encodeURIComponent(name) + "=" + encodeURIComponent(value) + "; expires=" + expires.toGMTString(), text, "Cookie string is incorrect.");
  243. },
  244. testCreateCookieStringComplexWithAll : function(){
  245. var name = "c.f name";
  246. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  247. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  248. var text = YAHOO.util.Cookie._createCookieString(name, value, true, { expires: expires, domain : "yahoo.com", path: "/", secure: true });
  249. Assert.areEqual(encodeURIComponent(name) + "=" + encodeURIComponent(value) + "; expires=" + expires.toGMTString() + "; path=/; domain=yahoo.com; secure", text, "Cookie string is incorrect.");
  250. },
  251. testCreateCookieHashString1 : function (){
  252. var hash = {
  253. name: "Nicholas Zakas",
  254. title: "Front End Engineer",
  255. "something else" : "hiya"
  256. };
  257. var text = YAHOO.util.Cookie._createCookieHashString(hash);
  258. Assert.areEqual("name=Nicholas%20Zakas&title=Front%20End%20Engineer&something%20else=hiya", text, "Cookie hash string is incorrect.");
  259. },
  260. testCreateCookieHashString2 : function (){
  261. var hash = {
  262. name: "Nicholas Zakas"
  263. };
  264. var text = YAHOO.util.Cookie._createCookieHashString(hash);
  265. Assert.areEqual("name=Nicholas%20Zakas", text, "Cookie hash string is incorrect.");
  266. }
  267. }));
  268. //-------------------------------------------------------------------------
  269. // Test Case for getting cookies
  270. //-------------------------------------------------------------------------
  271. suite.add(new YAHOO.tool.TestCase({
  272. name : "Get Cookie Tests",
  273. _should : {
  274. error : {
  275. testGetInvalidName1 : new TypeError("Cookie.get(): Cookie name must be a non-empty string."),
  276. testGetInvalidName2 : new TypeError("Cookie.get(): Cookie name must be a non-empty string."),
  277. testGetInvalidName3 : new TypeError("Cookie.get(): Cookie name must be a non-empty string."),
  278. testGetInvalidName4 : new TypeError("Cookie.get(): Cookie name must be a non-empty string."),
  279. testGetInvalidName5 : new TypeError("Cookie.get(): Cookie name must be a non-empty string.")
  280. }
  281. },
  282. //---------------------------------------------------------------------
  283. // Tests
  284. //---------------------------------------------------------------------
  285. testGetSimple : function(){
  286. setCookie("name", "Nicholas%20Zakas");
  287. var value = YAHOO.util.Cookie.get("name");
  288. Assert.areEqual("Nicholas Zakas", value, "Retrieved cookie value is incorrect.");
  289. deleteCookie("name");
  290. },
  291. testGetUnknown : function(){
  292. setCookie("name", "Nicholas%20Zakas");
  293. var value = YAHOO.util.Cookie.get("name2");
  294. Assert.isNull(value, "Retrieved cookie value is should be null.");
  295. deleteCookie("name");
  296. },
  297. testGetComplex : function(){
  298. setCookie("name", "Nicholas%20Zakas");
  299. setCookie("title", "Front%20End%20Engineer");
  300. setCookie("component", "Cookie%20Utility");
  301. var value1 = YAHOO.util.Cookie.get("name");
  302. var value2 = YAHOO.util.Cookie.get("title");
  303. var value3 = YAHOO.util.Cookie.get("component");
  304. var value4 = YAHOO.util.Cookie.get("nonexistent");
  305. Assert.areEqual("Nicholas Zakas", value1, "Retrieved cookie value is incorrect.");
  306. Assert.areEqual("Front End Engineer", value2, "Retrieved cookie value is incorrect.");
  307. Assert.areEqual("Cookie Utility", value3, "Retrieved cookie value is incorrect.");
  308. Assert.isNull(value4, "Retrieved cookie value should be null.");
  309. deleteCookie("name");
  310. deleteCookie("title");
  311. deleteCookie("component");
  312. },
  313. testGetInvalidName1 : function(){
  314. YAHOO.util.Cookie.get(12);
  315. },
  316. testGetInvalidName2 : function(){
  317. YAHOO.util.Cookie.get(true);
  318. },
  319. testGetInvalidName3 : function(){
  320. YAHOO.util.Cookie.get("");
  321. },
  322. testGetInvalidName4 : function(){
  323. YAHOO.util.Cookie.get();
  324. },
  325. testGetInvalidName5 : function(){
  326. YAHOO.util.Cookie.get(null);
  327. },
  328. testGetWithBooleanConverter : function(){
  329. setCookie("found", "true");
  330. var value = YAHOO.util.Cookie.get("found", Boolean);
  331. Assert.isBoolean(value, "Retrieved value should be a boolean.");
  332. Assert.isTrue(value, "Retrieved cookie value should be true.");
  333. deleteCookie("found");
  334. },
  335. testGetWithNumberConverter : function(){
  336. setCookie("count", "11");
  337. var value = YAHOO.util.Cookie.get("count", Number);
  338. Assert.isNumber(value, "Retrieved value should be a number.");
  339. Assert.areEqual(11, value, "Retrieved cookie value should be 11.");
  340. deleteCookie("number");
  341. },
  342. testGetWithCustomConverter : function(){
  343. setCookie("count", "11");
  344. var value = YAHOO.util.Cookie.get("count", function(value){
  345. if (value === "11"){
  346. return true;
  347. } else {
  348. return false;
  349. }
  350. });
  351. Assert.isBoolean(value, "Retrieved value should be a boolean.");
  352. Assert.isTrue(value, "Retrieved cookie value should be true.");
  353. deleteCookie("count");
  354. },
  355. testGetWithInvalidConverter : function(){
  356. setCookie("count", "11");
  357. var value = YAHOO.util.Cookie.get("count", true);
  358. Assert.isString(value, "Retrieved value should be a string.");
  359. Assert.areEqual("11", value, "Retrieved cookie value should be 11.");
  360. deleteCookie("count");
  361. },
  362. testGetWithConverterAndUnknownCookie : function(){
  363. setCookie("count", "11");
  364. var value = YAHOO.util.Cookie.get("count2", Number);
  365. Assert.isNull(value, "Retrieved value should be null.");
  366. deleteCookie("count");
  367. }
  368. }));
  369. //-------------------------------------------------------------------------
  370. // Test Case for getting cookie hashes
  371. //-------------------------------------------------------------------------
  372. suite.add(new YAHOO.tool.TestCase({
  373. name : "Get Cookie Subs Tests",
  374. //---------------------------------------------------------------------
  375. // Tests
  376. //---------------------------------------------------------------------
  377. testGetSubsSimple : function(){
  378. setCookie("data", "a=b&c=d&e=f&g=h");
  379. var hash = YAHOO.util.Cookie.getSubs("data");
  380. ObjectAssert.hasProperty("a", hash, "Hash 'a' is missing.");
  381. ObjectAssert.hasProperty("c", hash, "Hash 'c' is missing.");
  382. ObjectAssert.hasProperty("e", hash, "Hash 'e' is missing.");
  383. ObjectAssert.hasProperty("g", hash, "Hash 'g' is missing.");
  384. Assert.areEqual("b", hash.a, "Hash 'a' should have value 'b'.");
  385. Assert.areEqual("d", hash.c, "Hash 'c' should have value 'd'.");
  386. Assert.areEqual("f", hash.e, "Hash 'e' should have value 'f'.");
  387. Assert.areEqual("h", hash.g, "Hash 'g' should have value 'h'.");
  388. deleteCookie("data");
  389. },
  390. testGetSubsUnknown : function(){
  391. setCookie("name", "Nicholas%20Zakas");
  392. var hash = YAHOO.util.Cookie.getSubs("name2");
  393. Assert.isNull(hash, "Retrieved cookie value is should be null.");
  394. deleteCookie("name");
  395. },
  396. testGetSubsComplex : function(){
  397. setCookie("name", "Nicholas%20Zakas");
  398. setCookie("data", "age=29&title=f2e&stuff=no%20way");
  399. setCookie("component", "Cookie%20Utility");
  400. var hash = YAHOO.util.Cookie.getSubs("data");
  401. ObjectAssert.hasProperty("age", hash, "Hash 'age' is missing.");
  402. ObjectAssert.hasProperty("title", hash, "Hash 'title' is missing.");
  403. ObjectAssert.hasProperty("stuff", hash, "Hash 'stuff' is missing.");
  404. Assert.areEqual("29", hash.age, "Hash 'a' should have value 'b'.");
  405. Assert.areEqual("f2e", hash.title, "Hash 'c' should have value 'd'.");
  406. Assert.areEqual("no way", hash.stuff, "Hash 'e' should have value 'f'.");
  407. deleteCookie("name");
  408. deleteCookie("data");
  409. deleteCookie("component");
  410. }
  411. }));
  412. //-------------------------------------------------------------------------
  413. // Test Case for getting individual cookie sub.
  414. //-------------------------------------------------------------------------
  415. suite.add(new YAHOO.tool.TestCase({
  416. name : "Get Cookie Sub Tests",
  417. _should : {
  418. error : {
  419. testGetSubInvalidName1 : new TypeError("Cookie.getSubs(): Cookie name must be a non-empty string."),
  420. testGetSubInvalidName2 : new TypeError("Cookie.getSubs(): Cookie name must be a non-empty string."),
  421. testGetSubInvalidName3 : new TypeError("Cookie.getSubs(): Cookie name must be a non-empty string."),
  422. testGetSubInvalidName4 : new TypeError("Cookie.getSubs(): Cookie name must be a non-empty string."),
  423. testGetSubInvalidName5 : new TypeError("Cookie.getSubs(): Cookie name must be a non-empty string."),
  424. testGetSubInvalidSubName1 : new TypeError("Cookie.getSub(): Subcookie name must be a non-empty string."),
  425. testGetSubInvalidSubName2 : new TypeError("Cookie.getSub(): Subcookie name must be a non-empty string."),
  426. testGetSubInvalidSubName3 : new TypeError("Cookie.getSub(): Subcookie name must be a non-empty string."),
  427. testGetSubInvalidSubName4 : new TypeError("Cookie.getSub(): Subcookie name must be a non-empty string."),
  428. testGetSubInvalidSubName5 : new TypeError("Cookie.getSub(): Subcookie name must be a non-empty string.")
  429. }
  430. },
  431. setUp : function () {
  432. setCookie("data", "a=b&c=d&e=f&g=h&found=true&count=11&age=29&title=f2e&stuff=no%20way&special=" + encodeURIComponent("Something with & and ="));
  433. setCookie("name", "Nicholas%20Zakas");
  434. setCookie("component", "Cookie%20Utility");
  435. },
  436. tearDown : function () {
  437. deleteCookie("data");
  438. deleteCookie("name");
  439. deleteCookie("component");
  440. },
  441. //---------------------------------------------------------------------
  442. // Tests
  443. //---------------------------------------------------------------------
  444. testGetSubSimple : function(){
  445. var value = YAHOO.util.Cookie.getSub("data", "c");
  446. Assert.areEqual("d", value, "Subcookie value is incorrect.");
  447. },
  448. testGetSubUnknown : function(){
  449. var hash = YAHOO.util.Cookie.getSub("data", "i");
  450. Assert.isNull(hash, "Retrieved cookie value is should be null.");
  451. },
  452. testGetSubComplex : function(){
  453. var value = YAHOO.util.Cookie.getSub("data", "stuff");
  454. Assert.areEqual("no way", value, "Subcookie value is wrong.");
  455. },
  456. testGetSubInvalidName1 : function(){
  457. YAHOO.util.Cookie.getSub(12);
  458. },
  459. testGetSubInvalidName2 : function(){
  460. YAHOO.util.Cookie.getSub(true);
  461. },
  462. testGetSubInvalidName3 : function(){
  463. YAHOO.util.Cookie.getSub("");
  464. },
  465. testGetSubInvalidName4 : function(){
  466. YAHOO.util.Cookie.getSub();
  467. },
  468. testGetSubInvalidName5 : function(){
  469. YAHOO.util.Cookie.getSub(null);
  470. },
  471. testGetSubInvalidSubName1 : function(){
  472. YAHOO.util.Cookie.getSub("data", 12);
  473. },
  474. testGetSubInvalidSubName2 : function(){
  475. YAHOO.util.Cookie.getSub("data", true);
  476. },
  477. testGetSubInvalidSubName3 : function(){
  478. YAHOO.util.Cookie.getSub("data", "");
  479. },
  480. testGetSubInvalidSubName4 : function(){
  481. YAHOO.util.Cookie.getSub("data");
  482. },
  483. testGetSubInvalidSubName5 : function(){
  484. YAHOO.util.Cookie.getSub("data", null);
  485. },
  486. testGetSubOnNonExistantCookie : function(){
  487. var hash = YAHOO.util.Cookie.getSub("invalid", "i");
  488. Assert.isNull(hash, "Retrieved cookie value is should be null.");
  489. },
  490. testGetSubWithBooleanConverter : function(){
  491. var value = YAHOO.util.Cookie.getSub("data", "found", Boolean);
  492. Assert.isBoolean(value, "Retrieved subcookie value should be a boolean.");
  493. Assert.isTrue(value, "Retrieved subcookie value should be true.");
  494. },
  495. testGetSubWithNumberConverter : function(){
  496. var value = YAHOO.util.Cookie.getSub("data", "count", Number);
  497. Assert.isNumber(value, "Retrieved subcookie value should be a number.");
  498. Assert.areEqual(11, value, "Retrieved subcookie value should be 11.");
  499. },
  500. testGetSubWithCustomConverter : function(){
  501. var value = YAHOO.util.Cookie.getSub("data", "count", function(value){
  502. if (value === "11"){
  503. return true;
  504. } else {
  505. return false;
  506. }
  507. });
  508. Assert.isBoolean(value, "Retrieved subcookie value should be a boolean.");
  509. Assert.isTrue(value, "Retrieved subcookie value should be true.");
  510. },
  511. testGetSubWithInvalidConverter : function(){
  512. var value = YAHOO.util.Cookie.getSub("data", "count", true);
  513. Assert.isString(value, "Retrieved subcookie value should be a string.");
  514. Assert.areEqual("11", value, "Retrieved subcookie value should be 11.");
  515. },
  516. testSubGetWithConverterAndUnknownCookie : function(){
  517. var value = YAHOO.util.Cookie.getSub("data", "count2", Number);
  518. Assert.isNull(value, "Retrieved subcookie value should be null.");
  519. },
  520. testSubGetSpecial : function(){
  521. var value = YAHOO.util.Cookie.getSub("data", "special");
  522. Assert.areEqual("Something with & and =", value, "Sub cookie string is incorrect.");
  523. }
  524. }));
  525. //-------------------------------------------------------------------------
  526. // Test Case for removing individual cookie sub.
  527. //-------------------------------------------------------------------------
  528. suite.add(new YAHOO.tool.TestCase({
  529. name : "Remove Cookie Sub Tests",
  530. _should : {
  531. error : {
  532. testRemoveSubInvalidName1 : new TypeError("Cookie.removeSub(): Cookie name must be a non-empty string."),
  533. testRemoveSubInvalidName2 : new TypeError("Cookie.removeSub(): Cookie name must be a non-empty string."),
  534. testRemoveSubInvalidName3 : new TypeError("Cookie.removeSub(): Cookie name must be a non-empty string."),
  535. testRemoveSubInvalidName4 : new TypeError("Cookie.removeSub(): Cookie name must be a non-empty string."),
  536. testRemoveSubInvalidName5 : new TypeError("Cookie.removeSub(): Cookie name must be a non-empty string."),
  537. testRemoveSubInvalidSubName1 : new TypeError("Cookie.removeSub(): Subcookie name must be a non-empty string."),
  538. testRemoveSubInvalidSubName2 : new TypeError("Cookie.removeSub(): Subcookie name must be a non-empty string."),
  539. testRemoveSubInvalidSubName3 : new TypeError("Cookie.removeSub(): Subcookie name must be a non-empty string."),
  540. testRemoveSubInvalidSubName4 : new TypeError("Cookie.removeSub(): Subcookie name must be a non-empty string."),
  541. testRemoveSubInvalidSubName5 : new TypeError("Cookie.removeSub(): Subcookie name must be a non-empty string.")
  542. }
  543. },
  544. setUp : function () {
  545. setCookie("data", "a=b&c=d&e=f&g=h&found=true&count=11&age=29&title=f2e&stuff=no%20way&special=" + encodeURIComponent("Something with & and ="));
  546. setCookie("name", "Nicholas%20Zakas");
  547. setCookie("component", "Cookie%20Utility");
  548. },
  549. tearDown : function () {
  550. deleteCookie("data");
  551. deleteCookie("name");
  552. deleteCookie("component");
  553. },
  554. //---------------------------------------------------------------------
  555. // Tests
  556. //---------------------------------------------------------------------
  557. testRemoveSubSimple : function(){
  558. var value = YAHOO.util.Cookie.removeSub("data", "c");
  559. Assert.areEqual("data=a=b&e=f&g=h&found=true&count=11&age=29&title=f2e&stuff=no%20way&special=" + encodeURIComponent("Something with & and ="), value, "Cookie string is incorrect.");
  560. },
  561. testRemoveSubUnknown : function(){
  562. var value = YAHOO.util.Cookie.removeSub("data", "i");
  563. Assert.areEqual("", value, "Cookie string is incorrect.");
  564. },
  565. testRemoveSubInvalidName1 : function(){
  566. YAHOO.util.Cookie.removeSub(12);
  567. },
  568. testRemoveSubInvalidName2 : function(){
  569. YAHOO.util.Cookie.removeSub(true);
  570. },
  571. testRemoveSubInvalidName3 : function(){
  572. YAHOO.util.Cookie.removeSub("");
  573. },
  574. testRemoveSubInvalidName4 : function(){
  575. YAHOO.util.Cookie.removeSub();
  576. },
  577. testRemoveSubInvalidName5 : function(){
  578. YAHOO.util.Cookie.removeSub(null);
  579. },
  580. testRemoveSubInvalidSubName1 : function(){
  581. YAHOO.util.Cookie.removeSub("data", 12);
  582. },
  583. testRemoveSubInvalidSubName2 : function(){
  584. YAHOO.util.Cookie.removeSub("data", true);
  585. },
  586. testRemoveSubInvalidSubName3 : function(){
  587. YAHOO.util.Cookie.removeSub("data", "");
  588. },
  589. testRemoveSubInvalidSubName4 : function(){
  590. YAHOO.util.Cookie.removeSub("data");
  591. },
  592. testRemoveSubInvalidSubName5 : function(){
  593. YAHOO.util.Cookie.removeSub("data", null);
  594. },
  595. testRemoveSubOnNonExistantCookie : function(){
  596. var value = YAHOO.util.Cookie.removeSub("invalid", "i");
  597. Assert.areEqual("",value, "Cookie string is incorrect.");
  598. }
  599. }));
  600. //-------------------------------------------------------------------------
  601. // Test Case for removing cookies
  602. //-------------------------------------------------------------------------
  603. suite.add(new YAHOO.tool.TestCase({
  604. name : "Remove Cookie Tests",
  605. _should : {
  606. error : {
  607. testRemoveInvalidName1 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string."),
  608. testRemoveInvalidName2 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string."),
  609. testRemoveInvalidName3 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string."),
  610. testRemoveInvalidName4 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string."),
  611. testRemoveInvalidName5 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string.")
  612. }
  613. },
  614. setUp : function(){
  615. setCookie("data", "1234");
  616. },
  617. tearDown : function(){
  618. deleteCookie("data");
  619. },
  620. //---------------------------------------------------------------------
  621. // Tests
  622. //---------------------------------------------------------------------
  623. testRemoveSimple : function(){
  624. YAHOO.util.Cookie.remove("data");
  625. var value = YAHOO.util.Cookie.get("data");
  626. Assert.isNull(value, "Cookie value should be null.");
  627. },
  628. testRemoveInvalidName1 : function(){
  629. YAHOO.util.Cookie.remove();
  630. },
  631. testRemoveInvalidName2 : function(){
  632. YAHOO.util.Cookie.remove("");
  633. },
  634. testRemoveInvalidName3 : function(){
  635. YAHOO.util.Cookie.remove(25);
  636. },
  637. testRemoveInvalidName4 : function(){
  638. YAHOO.util.Cookie.remove(true);
  639. },
  640. testRemoveInvalidName5 : function(){
  641. YAHOO.util.Cookie.remove(null);
  642. }
  643. }));
  644. //-------------------------------------------------------------------------
  645. // Test Case for setting cookies
  646. //-------------------------------------------------------------------------
  647. suite.add(new YAHOO.tool.TestCase({
  648. name : "Set Cookie Tests",
  649. _should : {
  650. error : {
  651. testRemoveInvalidName1 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string."),
  652. testRemoveInvalidName2 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string."),
  653. testRemoveInvalidName3 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string."),
  654. testRemoveInvalidName4 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string."),
  655. testRemoveInvalidName5 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string.")
  656. }
  657. },
  658. tearDown : function (){
  659. deleteCookie("data");
  660. deleteCookie("name");
  661. deleteCookie("name", "path=/;");
  662. deleteCookie("name", "path=/; domain=yahoo.com;");
  663. deleteCookie("name", "path=/; domain=yahoo.com; secure;");
  664. deleteCookie(encodeURIComponent("c.f name"));
  665. deleteCookie(encodeURIComponent("c.f name"), "path=/;");
  666. deleteCookie(encodeURIComponent("c.f name"), "path=/; domain=yahoo.com;");
  667. deleteCookie(encodeURIComponent("c.f name"), "path=/; domain=yahoo.com; secure;");
  668. },
  669. //---------------------------------------------------------------------
  670. // Tests
  671. //---------------------------------------------------------------------
  672. testSetSimple : function(){
  673. var output = YAHOO.util.Cookie.set("data", "1234");
  674. Assert.areEqual("data=1234", output, "Cookie string format is wrong.");
  675. Assert.areEqual("data=1234", document.cookie, "Cookie was not set onto document.");
  676. },
  677. testSetMulti : function(){
  678. var output = YAHOO.util.Cookie.set("data", "1234");
  679. output += "; " + YAHOO.util.Cookie.set("name", "Nicholas C. Zakas");
  680. Assert.areEqual("data=1234; name=Nicholas%20C.%20Zakas", document.cookie, "Cookies were not set to document.");
  681. },
  682. testSetSimpleWithPath : function(){
  683. var text = YAHOO.util.Cookie.set("name", "value", { path: "/" });
  684. Assert.areEqual("name=value; path=/", text, "Cookie string is incorrect.");
  685. Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  686. },
  687. testSetSimpleWithInvalidPath1 : function(){
  688. var text = YAHOO.util.Cookie.set("name", "value", { path: 25 });
  689. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  690. Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  691. },
  692. testSetSimpleWithInvalidPath2 : function(){
  693. var text = YAHOO.util.Cookie.set("name", "value", { path: "" });
  694. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  695. Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  696. },
  697. testSetSimpleWithDomain : function(){
  698. var text = YAHOO.util.Cookie.set("name", "value", { domain: "yahoo.com" });
  699. Assert.areEqual("name=value; domain=yahoo.com", text, "Cookie string is incorrect.");
  700. //won't work unless the page is loaded from that domain
  701. //Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  702. },
  703. testSetSimpleWithInvalidDomain1 : function(){
  704. var text = YAHOO.util.Cookie.set("name", "value", { domain: true });
  705. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  706. //won't work unless the page is loaded from that domain
  707. //Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  708. },
  709. testSetSimpleWithInvalidDomain2 : function(){
  710. var text = YAHOO.util.Cookie.set("name", "value", { domain: "" });
  711. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  712. //won't work unless the page is loaded from that domain
  713. //Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  714. },
  715. testSetSimpleWithSecure : function(){
  716. var text = YAHOO.util.Cookie.set("name", "value", { secure: true });
  717. Assert.areEqual("name=value; secure", text, "Cookie string is incorrect.");
  718. //won't work unless the page is loaded over SSL
  719. //Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  720. },
  721. testSetSimpleWithInvalidSecure1 : function(){
  722. var text = YAHOO.util.Cookie.set("name", "value", { secure: false });
  723. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  724. Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  725. },
  726. testSetSimpleWithInvalidSecure2 : function(){
  727. var text = YAHOO.util.Cookie.set("name", "value", { secure: "blah" });
  728. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  729. Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  730. },
  731. testSetSimpleWithExpiry : function(){
  732. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  733. var text = YAHOO.util.Cookie.set("name", "value", { expires: expires });
  734. Assert.areEqual("name=value; expires=" + expires.toGMTString(), text, "Cookie string is incorrect.");
  735. Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  736. },
  737. testSetSimpleWithInvalidExpiry : function(){
  738. var text = YAHOO.util.Cookie.set("name", "value", { expires: "blah" });
  739. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  740. Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  741. },
  742. testSetSimpleWithAll : function(){
  743. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  744. var text = YAHOO.util.Cookie.set("name", "value", { expires: expires, domain : "yahoo.com", path: "/", secure: true });
  745. Assert.areEqual("name=value; expires=" + expires.toGMTString() + "; path=/; domain=yahoo.com; secure", text, "Cookie string is incorrect.");
  746. //won't work unless the page is loaded from that domain
  747. //Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  748. },
  749. testSetComplex : function(){
  750. var name = "c.f name";
  751. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  752. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  753. var text = YAHOO.util.Cookie.set(name, value);
  754. Assert.areEqual(result, text, "Cookie string is incorrect.");
  755. Assert.areEqual(result, document.cookie, "Cookie was not set to document.");
  756. },
  757. testSetComplexWithPath : function(){
  758. var name = "c.f name";
  759. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  760. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  761. var text = YAHOO.util.Cookie.set(name, value, { path : "/" });
  762. Assert.areEqual(result + "; path=/", text, "Cookie string is incorrect.");
  763. Assert.areEqual(result, document.cookie, "Cookie was not set to document.");
  764. },
  765. testSetComplexWithDomain : function(){
  766. var name = "c.f name";
  767. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  768. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  769. var text = YAHOO.util.Cookie.set(name, value, { domain: "yahoo.com" });
  770. Assert.areEqual(result + "; domain=yahoo.com", text, "Cookie string is incorrect.");
  771. //won't work unless the page is loaded from that domain
  772. //Assert.areEqual(result, document.cookie, "Cookie was not set to document.");
  773. },
  774. testSetComplexWithSecure : function(){
  775. var name = "c.f name";
  776. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  777. var text = YAHOO.util.Cookie.set(name, value, { secure: true });
  778. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  779. Assert.areEqual(result + "; secure", text, "Cookie string is incorrect.");
  780. //won't work unless the page is loaded over SSL
  781. //Assert.areEqual(result, document.cookie, "Cookie was not set to document.");
  782. },
  783. testSetComplexWithExpiry : function(){
  784. var name = "c.f name";
  785. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  786. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  787. var text = YAHOO.util.Cookie.set(name, value, { expires : expires });
  788. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  789. Assert.areEqual(result + "; expires=" + expires.toGMTString(), text, "Cookie string is incorrect.");
  790. Assert.areEqual(result, document.cookie, "Cookie was not set to document.");
  791. },
  792. testSetComplexWithAll : function(){
  793. var name = "c.f name";
  794. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  795. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  796. var text = YAHOO.util.Cookie.set(name, value, { expires: expires, domain : "yahoo.com", path: "/", secure: true });
  797. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  798. Assert.areEqual(result + "; expires=" + expires.toGMTString() + "; path=/; domain=yahoo.com; secure", text, "Cookie string is incorrect.");
  799. //won't work unless the page is loaded from that domain
  800. //Assert.areEqual("name=value", document.cookie, "Cookie was not set to document.");
  801. }
  802. }));
  803. //-------------------------------------------------------------------------
  804. // Test Case for setting subcookies
  805. //-------------------------------------------------------------------------
  806. suite.add(new YAHOO.tool.TestCase({
  807. name : "Set Subcookie Tests",
  808. _should : {
  809. error : {
  810. testRemoveInvalidName1 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string."),
  811. testRemoveInvalidName2 : new TypeError("Cookie.remove(): Cookie name must be a non-empty string."),
  812. testRemoveInvalidName3 : new TypeError("Cookie.remove(): Cookie name must be a non-empty strin…

Large files files are truncated, but you can click here to view the full file