PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/yui/tests/cookie/tests/cookie.html

http://github.com/WilDoane/TDD-JavaScript-Demo
HTML | 1549 lines | 1181 code | 368 blank | 0 comment | 0 complexity | 42127f994cb2dde2727a4009ace81419 MD5 | raw file

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

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