PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/yui/tests/cookie/tests/cookie-tests.js

https://bitbucket.org/mangel290/cars
JavaScript | 1500 lines | 1056 code | 354 blank | 90 comment | 7 complexity | 14721df2cc3788df718670e662bfd6c5 MD5 | raw file
Possible License(s): Apache-2.0
  1. YUI.add('cookie-tests', function(Y) {
  2. var Assert = Y.Assert,
  3. ObjectAssert = Y.ObjectAssert;
  4. //utility function
  5. function deleteCookie(name, detail){
  6. this.stubDoc.cookie = name + "=blah; " + (detail || "") + " expires=Thu, 01 Jan 1970 00:00:00 GMT";
  7. }
  8. function setCookie(name, value){
  9. this.stubDoc.cookie = (name) + "=" + (value);
  10. }
  11. //-------------------------------------------------------------------------
  12. // Base Test Suite
  13. //-------------------------------------------------------------------------
  14. var suite = new Y.Test.Suite("Cookie Tests");
  15. //-------------------------------------------------------------------------
  16. // Test Case for parsing capabilities
  17. //-------------------------------------------------------------------------
  18. suite.add(new Y.Test.Case({
  19. name : "Cookie Parsing Tests",
  20. //---------------------------------------------------------------------
  21. // Tests
  22. //---------------------------------------------------------------------
  23. testParseCookieStringEmpty : function(){
  24. var cookies = Y.Cookie._parseCookieString("");
  25. },
  26. testParseCookieStringNull : function(){
  27. var cookies = Y.Cookie._parseCookieString(null);
  28. },
  29. testParseCookieStringBoolean : function(){
  30. var cookies = Y.Cookie._parseCookieString(true);
  31. },
  32. testParseCookieStringBoolean : function(){
  33. var cookies = Y.Cookie._parseCookieString(true);
  34. },
  35. testParseCookieStringUndefined : function(){
  36. var cookies = Y.Cookie._parseCookieString();
  37. },
  38. testParseCookieStringInvalid : function(){
  39. var cookies = Y.Cookie._parseCookieString("a");
  40. },
  41. testParseCookieStringSimple : function(){
  42. var cookieString = "a=b";
  43. var cookies = Y.Cookie._parseCookieString(cookieString);
  44. ObjectAssert.hasKey("a", cookies, "Cookie 'a' is missing.");
  45. Assert.areEqual("b", cookies.a, "Cookie 'a' should have value 'b'.");
  46. },
  47. testParseCookieStringNumber : function(){
  48. var cookieString = "12345=b";
  49. var cookies = Y.Cookie._parseCookieString(cookieString);
  50. ObjectAssert.hasKey("12345", cookies, "Cookie '12345' is missing.");
  51. Assert.areEqual("b", cookies["12345"], "Cookie '12345' should have value 'b'.");
  52. },
  53. testParseCookieStringSimpleMulti : function(){
  54. var cookieString = "a=b; c=d; e=f; g=h";
  55. var cookies = Y.Cookie._parseCookieString(cookieString);
  56. ObjectAssert.hasKey("a", cookies, "Cookie 'a' is missing.");
  57. ObjectAssert.hasKey("c", cookies, "Cookie 'c' is missing.");
  58. ObjectAssert.hasKey("e", cookies, "Cookie 'e' is missing.");
  59. ObjectAssert.hasKey("g", cookies, "Cookie 'g' is missing.");
  60. Assert.areEqual("b", cookies.a, "Cookie 'a' should have value 'b'.");
  61. Assert.areEqual("d", cookies.c, "Cookie 'c' should have value 'd'.");
  62. Assert.areEqual("f", cookies.e, "Cookie 'e' should have value 'f'.");
  63. Assert.areEqual("h", cookies.g, "Cookie 'g' should have value 'h'.");
  64. },
  65. testParseCookieStringComplex : function(){
  66. var cookieString = "name=Nicholas%20Zakas; title=front%20end%20engineer";
  67. var cookies = Y.Cookie._parseCookieString(cookieString);
  68. ObjectAssert.hasKey("name", cookies, "Cookie 'name' is missing.");
  69. ObjectAssert.hasKey("title", cookies, "Cookie 'title' is missing.");
  70. Assert.areEqual("Nicholas Zakas", cookies.name, "Cookie 'name' should have value 'Nicholas Zakas'.");
  71. Assert.areEqual("front end engineer", cookies.title, "Cookie 'title' should have value 'front end engineer'.");
  72. },
  73. testParseCookieStringNetwork : function(){
  74. 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";
  75. var cookies = Y.Cookie._parseCookieString(cookieString);
  76. ObjectAssert.hasKey("B", cookies, "Cookie 'B' is missing.");
  77. ObjectAssert.hasKey("LYC", cookies, "Cookie 'LYC' is missing.");
  78. ObjectAssert.hasKey("l_PD3", cookies, "Cookie 'l_PD3' is missing.");
  79. },
  80. testParseCookieStringWithEscapedCharactersInCookieName: function(){
  81. var cookieName = "something[1]";
  82. var cookieValue = "123";
  83. var cookieString = encodeURIComponent(cookieName) + "=" + encodeURIComponent(cookieValue);
  84. var cookies = Y.Cookie._parseCookieString(cookieString);
  85. ObjectAssert.hasKey(cookieName, cookies, "Cookie '" + cookieName + "' is missing.");
  86. Assert.areEqual(cookieValue, cookies[cookieName], "Cookie value for '" + cookieName + "' is wrong.");
  87. },
  88. testParseCookieStringIncorrectFormat: function(){
  89. var cookieString = "SESSION=27bedbdf3d35252d0db07f34d81dcca6; STATS=OK; SCREEN=1280x1024; undefined; ys-bottom-preview=o%3Aheight%3Dn%253A389";
  90. var cookies = Y.Cookie._parseCookieString(cookieString);
  91. ObjectAssert.hasKey("SCREEN", cookies, "Cookie 'SCREEN' is missing.");
  92. ObjectAssert.hasKey("STATS", cookies, "Cookie 'STATS' is missing.");
  93. ObjectAssert.hasKey("SESSION", cookies, "Cookie 'SESSION' is missing.");
  94. ObjectAssert.hasKey("ys-bottom-preview", cookies, "Cookie 'ys-bottom-preview' is missing.");
  95. ObjectAssert.hasKey("undefined", cookies, "Cookie 'undefined' is missing.");
  96. },
  97. /*
  98. * Tests that the cookie utility deals with cookies that contain
  99. * an invalid encoding. It shouldn't error, but should treat the cookie
  100. * as if it doesn't exist (return null).
  101. */
  102. testParseCookieStringInvalidEncoding: function(){
  103. var cookieString = "DetailInfoList=CPN03022194=@|@=CPN03#|#%B4%EB%C3%B5%C7%D8%BC%F6%BF%E5%C0%E5#|#1016026000#|#%BD%C5%C8%E6%B5%BF#|##|#";
  104. var cookies = Y.Cookie._parseCookieString(cookieString);
  105. Assert.isNull(cookies["DetailInfoList"], "Cookie 'DetailInfoList' should not have a value.");
  106. },
  107. /*
  108. * Tests that a Boolean cookie, one without an equals sign of value,
  109. * is represented as an empty string.
  110. */
  111. testParseCookieStringBooleanCookie: function(){
  112. var cookieString = "info";
  113. var cookies = Y.Cookie._parseCookieString(cookieString);
  114. Assert.areEqual("", cookies["info"], "Cookie 'info' should be an empty string.");
  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 = Y.Cookie._parseCookieString(cookieString);
  119. ObjectAssert.hasKey("name", cookies, "Cookie 'name' is missing.");
  120. ObjectAssert.hasKey("hash", cookies, "Cookie 'hash' is missing.");
  121. ObjectAssert.hasKey("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 = Y.Cookie._parseCookieHash(cookieHash);
  129. ObjectAssert.hasKey("a", hash, "Hash 'a' is missing.");
  130. ObjectAssert.hasKey("c", hash, "Hash 'c' is missing.");
  131. ObjectAssert.hasKey("e", hash, "Hash 'e' is missing.");
  132. ObjectAssert.hasKey("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 = Y.Cookie._parseCookieHash(cookieHash);
  143. ObjectAssert.hasKey(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 = Y.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 Y.Test.Case({
  155. name : "Cookie String Creation Tests",
  156. //---------------------------------------------------------------------
  157. // Tests
  158. //---------------------------------------------------------------------
  159. testCreateCookieStringSimple : function(){
  160. var text = Y.Cookie._createCookieString("name", "value", true);
  161. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  162. },
  163. testCreateCookieStringSimpleWithPath : function(){
  164. var text = Y.Cookie._createCookieString("name", "value", true, { path: "/" });
  165. Assert.areEqual("name=value; path=/", text, "Cookie string is incorrect.");
  166. },
  167. testCreateCookieStringSimpleWithInvalidPath1 : function(){
  168. var text = Y.Cookie._createCookieString("name", "value", true, { path: 25 });
  169. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  170. },
  171. testCreateCookieStringSimpleWithInvalidPath2 : function(){
  172. var text = Y.Cookie._createCookieString("name", "value", true, { path: "" });
  173. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  174. },
  175. testCreateCookieStringSimpleWithDomain : function(){
  176. var text = Y.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 = Y.Cookie._createCookieString("name", "value", true, { domain: true });
  181. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  182. },
  183. testCreateCookieStringSimpleWithInvalidDomain2 : function(){
  184. var text = Y.Cookie._createCookieString("name", "value", true, { domain: "" });
  185. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  186. },
  187. testCreateCookieStringSimpleWithSecure : function(){
  188. var text = Y.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 = Y.Cookie._createCookieString("name", "value", true, { secure: false });
  193. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  194. },
  195. testCreateCookieStringSimpleWithInvalidSecure2 : function(){
  196. var text = Y.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 = Y.Cookie._createCookieString("name", "value", true, { expires: expires });
  202. Assert.areEqual("name=value; expires=" + expires.toUTCString(), text, "Cookie string is incorrect.");
  203. },
  204. testCreateCookieStringSimpleWithInvalidExpiry : function(){
  205. var text = Y.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 = Y.Cookie._createCookieString("name", "value", true, { expires: expires, domain : "yahoo.com", path: "/", secure: true });
  211. Assert.areEqual("name=value; expires=" + expires.toUTCString() + "; 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 = Y.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 = Y.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 = Y.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 = Y.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 = Y.Cookie._createCookieString(name, value, true, { expires : expires });
  242. Assert.areEqual(encodeURIComponent(name) + "=" + encodeURIComponent(value) + "; expires=" + expires.toUTCString(), 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 = Y.Cookie._createCookieString(name, value, true, { expires: expires, domain : "yahoo.com", path: "/", secure: true });
  249. Assert.areEqual(encodeURIComponent(name) + "=" + encodeURIComponent(value) + "; expires=" + expires.toUTCString() + "; 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 = Y.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 = Y.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 Y.Test.Case({
  272. name : "Get Cookie Tests",
  273. _should : {
  274. error : {
  275. testGetInvalidName1 : new TypeError("Cookie name must be a non-empty string."),
  276. testGetInvalidName2 : new TypeError("Cookie name must be a non-empty string."),
  277. testGetInvalidName3 : new TypeError("Cookie name must be a non-empty string."),
  278. testGetInvalidName4 : new TypeError("Cookie name must be a non-empty string."),
  279. testGetInvalidName5 : new TypeError("Cookie name must be a non-empty string.")
  280. }
  281. },
  282. setUp: function(){
  283. this.stubDoc = {cookie:""};
  284. Y.Cookie._setDoc(this.stubDoc);
  285. },
  286. tearDown: function(){
  287. Y.Cookie._setDoc(Y.config.doc);
  288. delete this.stubDoc;
  289. },
  290. //---------------------------------------------------------------------
  291. // Tests
  292. //---------------------------------------------------------------------
  293. testGetSimple : function(){
  294. this.stubDoc.cookie = "name=Nicholas%20Zakas";
  295. var value = Y.Cookie.get("name");
  296. Assert.areEqual("Nicholas Zakas", value, "Retrieved cookie value is incorrect.");
  297. },
  298. testGetUnknown : function(){
  299. this.stubDoc.cookie = "name=Nicholas%20Zakas";
  300. var value = Y.Cookie.get("name2");
  301. Assert.isNull(value, "Retrieved cookie value is should be null.");
  302. },
  303. testGetComplex : function(){
  304. this.stubDoc.cookie = "name=Nicholas%20Zakas; title=Front%20End%20Engineer; component=Cookie%20Utility";
  305. var value1 = Y.Cookie.get("name");
  306. var value2 = Y.Cookie.get("title");
  307. var value3 = Y.Cookie.get("component");
  308. var value4 = Y.Cookie.get("nonexistent");
  309. Assert.areEqual("Nicholas Zakas", value1, "Retrieved cookie value is incorrect.");
  310. Assert.areEqual("Front End Engineer", value2, "Retrieved cookie value is incorrect.");
  311. Assert.areEqual("Cookie Utility", value3, "Retrieved cookie value is incorrect.");
  312. Assert.isNull(value4, "Retrieved cookie value should be null.");
  313. },
  314. testGetInvalidName1 : function(){
  315. Y.Cookie.get(12);
  316. },
  317. testGetInvalidName2 : function(){
  318. Y.Cookie.get(true);
  319. },
  320. testGetInvalidName3 : function(){
  321. Y.Cookie.get("");
  322. },
  323. testGetInvalidName4 : function(){
  324. Y.Cookie.get();
  325. },
  326. testGetInvalidName5 : function(){
  327. Y.Cookie.get(null);
  328. },
  329. testGetWithBooleanConverter : function(){
  330. this.stubDoc.cookie = "found=true";
  331. var value = Y.Cookie.get("found", Boolean);
  332. Assert.isBoolean(value, "Retrieved value should be a boolean.");
  333. Assert.isTrue(value, "Retrieved cookie value should be true.");
  334. },
  335. testGetWithNumberConverter : function(){
  336. this.stubDoc.cookie = "count=11";
  337. var value = Y.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. },
  341. testGetWithCustomConverter : function(){
  342. this.stubDoc.cookie = "count=11";
  343. var value = Y.Cookie.get("count", function(value){
  344. if (value === "11"){
  345. return true;
  346. } else {
  347. return false;
  348. }
  349. });
  350. Assert.isBoolean(value, "Retrieved value should be a boolean.");
  351. Assert.isTrue(value, "Retrieved cookie value should be true.");
  352. },
  353. testGetWithInvalidConverter : function(){
  354. this.stubDoc.cookie = "count=11";
  355. var value = Y.Cookie.get("count", true);
  356. Assert.isString(value, "Retrieved value should be a string.");
  357. Assert.areEqual("11", value, "Retrieved cookie value should be 11.");
  358. },
  359. testGetWithConverterAndUnknownCookie : function(){
  360. this.stubDoc.cookie = "count=11";
  361. var value = Y.Cookie.get("count2", Number);
  362. Assert.isNull(value, "Retrieved value should be null.");
  363. },
  364. testGetWithBooleanConverterOption : function(){
  365. this.stubDoc.cookie = "found=true";
  366. var value = Y.Cookie.get("found", { converter: Boolean });
  367. Assert.isBoolean(value, "Retrieved value should be a boolean.");
  368. Assert.isTrue(value, "Retrieved cookie value should be true.");
  369. },
  370. testGetWithNumberConverterOption : function(){
  371. this.stubDoc.cookie = "count=11";
  372. var value = Y.Cookie.get("count", { converter: Number });
  373. Assert.isNumber(value, "Retrieved value should be a number.");
  374. Assert.areEqual(11, value, "Retrieved cookie value should be 11.");
  375. },
  376. testGetWithCustomConverterOption : function(){
  377. this.stubDoc.cookie = "count=11";
  378. var value = Y.Cookie.get("count", { converter: function(value){
  379. if (value === "11"){
  380. return true;
  381. } else {
  382. return false;
  383. }
  384. } });
  385. Assert.isBoolean(value, "Retrieved value should be a boolean.");
  386. Assert.isTrue(value, "Retrieved cookie value should be true.");
  387. },
  388. testGetWithInvalidConverterOption : function(){
  389. this.stubDoc.cookie = "count=11";
  390. var value = Y.Cookie.get("count", { converter: true });
  391. Assert.isString(value, "Retrieved value should be a string.");
  392. Assert.areEqual("11", value, "Retrieved cookie value should be 11.");
  393. },
  394. testGetWithConverterOptionAndUnknownCookie : function(){
  395. this.stubDoc.cookie = "count=11";
  396. var value = Y.Cookie.get("count2", { converter: Number });
  397. Assert.isNull(value, "Retrieved value should be null.");
  398. },
  399. testGetWithEmptyOptions : function(){
  400. this.stubDoc.cookie = "name=Nicholas%20Zakas";
  401. var value = Y.Cookie.get("name", {});
  402. Assert.areEqual("Nicholas Zakas", value, "Retrieved cookie value is incorrect.");
  403. },
  404. testGetSimpleWithFalseRaw : function(){
  405. this.stubDoc.cookie = "name=Nicholas%20Zakas";
  406. var value = Y.Cookie.get("name", { raw: false });
  407. Assert.areEqual("Nicholas Zakas", value, "Retrieved cookie value is incorrect.");
  408. },
  409. testGetSimpleWithTrueRaw : function(){
  410. this.stubDoc.cookie = "name=Nicholas%20Zakas";
  411. var value = Y.Cookie.get("name", { raw: true });
  412. Assert.areEqual("Nicholas%20Zakas", value, "Retrieved cookie value is incorrect.");
  413. },
  414. testGetComplexWithFalseRaw : function(){
  415. this.stubDoc.cookie = "name=" + encodeURIComponent("as.bd ed|ieh,~!!@#$%^*=098345|}{<>?");
  416. var value = Y.Cookie.get("name", { raw: false });
  417. Assert.areEqual("as.bd ed|ieh,~!!@#$%^*=098345|}{<>?", value, "Retrieved cookie value is incorrect.");
  418. },
  419. testGetComplexWithTrueRaw : function(){
  420. this.stubDoc.cookie = "name=as.bd ed|ieh,~!!@#$%^*=098345|}{<>?; name2=" + encodeURIComponent("as.bd ed|ieh,~!!@#$%^*=098345|}{<>?");
  421. var value = Y.Cookie.get("name", { raw: true }),
  422. value2 = Y.Cookie.get("name2", { raw: true });
  423. Assert.areEqual("as.bd ed|ieh,~!!@#$%^*=098345|}{<>?", value, "Retrieved cookie value is incorrect.");
  424. Assert.areEqual(encodeURIComponent("as.bd ed|ieh,~!!@#$%^*=098345|}{<>?"), value2, "Retrieved cookie value is incorrect.");
  425. }
  426. }));
  427. //-------------------------------------------------------------------------
  428. // Test Case for testing cookie existence
  429. //-------------------------------------------------------------------------
  430. suite.add(new Y.Test.Case({
  431. name : "Cookie Exists Tests",
  432. setUp: function(){
  433. this.stubDoc = {cookie:""};
  434. Y.Cookie._setDoc(this.stubDoc);
  435. },
  436. tearDown: function(){
  437. Y.Cookie._setDoc(Y.config.doc);
  438. delete this.stubDoc;
  439. },
  440. //---------------------------------------------------------------------
  441. // Tests
  442. //---------------------------------------------------------------------
  443. testExistsSimple : function(){
  444. this.stubDoc.cookie = "name=Nicholas%20Zakas";
  445. var value = Y.Cookie.exists("name");
  446. Assert.isTrue(value, "Cookie 'name' should exist.");
  447. },
  448. testExistsUnknown : function(){
  449. var value = Y.Cookie.exists("name3");
  450. Assert.isFalse(value, "Cookie 'name3' should not exist.");
  451. },
  452. testExistsBooleanCookie : function(){
  453. this.stubDoc.cookie = "info";
  454. var value = Y.Cookie.exists("info");
  455. Assert.isTrue(value, "Cookie 'info' should exist.");
  456. }
  457. }));
  458. //-------------------------------------------------------------------------
  459. // Test Case for getting cookie hashes
  460. //-------------------------------------------------------------------------
  461. suite.add(new Y.Test.Case({
  462. name : "Get Cookie Subs Tests",
  463. setUp: function(){
  464. this.stubDoc = {cookie:""};
  465. Y.Cookie._setDoc(this.stubDoc);
  466. },
  467. tearDown: function(){
  468. Y.Cookie._setDoc(Y.config.doc);
  469. delete this.stubDoc;
  470. },
  471. //---------------------------------------------------------------------
  472. // Tests
  473. //---------------------------------------------------------------------
  474. testGetSubsSimple : function(){
  475. this.stubDoc.cookie = "data=a=b&c=d&e=f&g=h";
  476. var hash = Y.Cookie.getSubs("data");
  477. ObjectAssert.hasKey("a", hash, "Hash 'a' is missing.");
  478. ObjectAssert.hasKey("c", hash, "Hash 'c' is missing.");
  479. ObjectAssert.hasKey("e", hash, "Hash 'e' is missing.");
  480. ObjectAssert.hasKey("g", hash, "Hash 'g' is missing.");
  481. Assert.areEqual("b", hash.a, "Hash 'a' should have value 'b'.");
  482. Assert.areEqual("d", hash.c, "Hash 'c' should have value 'd'.");
  483. Assert.areEqual("f", hash.e, "Hash 'e' should have value 'f'.");
  484. Assert.areEqual("h", hash.g, "Hash 'g' should have value 'h'.");
  485. },
  486. testGetSubsUnknown : function(){
  487. this.stubDoc.cookie = "name=Nicholas&20Zakas";
  488. var hash = Y.Cookie.getSubs("name2");
  489. Assert.isNull(hash, "Retrieved cookie value is should be null.");
  490. },
  491. testGetSubsComplex : function(){
  492. this.stubDoc.cookie = "name=Nicholas&20Zakas; data=age=29&title=f2e&stuff=no%20way; component=Cookie%20Utility";
  493. var hash = Y.Cookie.getSubs("data");
  494. ObjectAssert.hasKey("age", hash, "Hash 'age' is missing.");
  495. ObjectAssert.hasKey("title", hash, "Hash 'title' is missing.");
  496. ObjectAssert.hasKey("stuff", hash, "Hash 'stuff' is missing.");
  497. Assert.areEqual("29", hash.age, "Hash 'a' should have value 'b'.");
  498. Assert.areEqual("f2e", hash.title, "Hash 'c' should have value 'd'.");
  499. Assert.areEqual("no way", hash.stuff, "Hash 'e' should have value 'f'.");
  500. }
  501. }));
  502. //-------------------------------------------------------------------------
  503. // Test Case for getting individual cookie sub.
  504. //-------------------------------------------------------------------------
  505. suite.add(new Y.Test.Case({
  506. name : "Get Cookie Sub Tests",
  507. _should : {
  508. error : {
  509. testGetSubInvalidName1 : new TypeError("Cookie name must be a non-empty string."),
  510. testGetSubInvalidName2 : new TypeError("Cookie name must be a non-empty string."),
  511. testGetSubInvalidName3 : new TypeError("Cookie name must be a non-empty string."),
  512. testGetSubInvalidName4 : new TypeError("Cookie name must be a non-empty string."),
  513. testGetSubInvalidName5 : new TypeError("Cookie name must be a non-empty string."),
  514. testGetSubInvalidSubName1 : new TypeError("Subcookie name must be a non-empty string."),
  515. testGetSubInvalidSubName2 : new TypeError("Subcookie name must be a non-empty string."),
  516. testGetSubInvalidSubName3 : new TypeError("Subcookie name must be a non-empty string."),
  517. testGetSubInvalidSubName4 : new TypeError("Subcookie name must be a non-empty string."),
  518. testGetSubInvalidSubName5 : new TypeError("Subcookie name must be a non-empty string.")
  519. }
  520. },
  521. setUp: function(){
  522. 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" };
  523. Y.Cookie._setDoc(this.stubDoc);
  524. },
  525. tearDown: function(){
  526. Y.Cookie._setDoc(Y.config.doc);
  527. delete this.stubDoc;
  528. },
  529. //---------------------------------------------------------------------
  530. // Tests
  531. //---------------------------------------------------------------------
  532. testGetSubSimple : function(){
  533. var value = Y.Cookie.getSub("data", "c");
  534. Assert.areEqual("d", value, "Subcookie value is incorrect.");
  535. },
  536. testGetSubUnknown : function(){
  537. var hash = Y.Cookie.getSub("data", "i");
  538. Assert.isNull(hash, "Retrieved cookie value is should be null.");
  539. },
  540. testGetSubComplex : function(){
  541. var value = Y.Cookie.getSub("data", "stuff");
  542. Assert.areEqual("no way", value, "Subcookie value is wrong.");
  543. },
  544. testGetSubInvalidName1 : function(){
  545. Y.Cookie.getSub(12);
  546. },
  547. testGetSubInvalidName2 : function(){
  548. Y.Cookie.getSub(true);
  549. },
  550. testGetSubInvalidName3 : function(){
  551. Y.Cookie.getSub("");
  552. },
  553. testGetSubInvalidName4 : function(){
  554. Y.Cookie.getSub();
  555. },
  556. testGetSubInvalidName5 : function(){
  557. Y.Cookie.getSub(null);
  558. },
  559. testGetSubInvalidSubName1 : function(){
  560. Y.Cookie.getSub("data", 12);
  561. },
  562. testGetSubInvalidSubName2 : function(){
  563. Y.Cookie.getSub("data", true);
  564. },
  565. testGetSubInvalidSubName3 : function(){
  566. Y.Cookie.getSub("data", "");
  567. },
  568. testGetSubInvalidSubName4 : function(){
  569. Y.Cookie.getSub("data");
  570. },
  571. testGetSubInvalidSubName5 : function(){
  572. Y.Cookie.getSub("data", null);
  573. },
  574. testGetSubOnNonExistantCookie : function(){
  575. var hash = Y.Cookie.getSub("invalid", "i");
  576. Assert.isNull(hash, "Retrieved cookie value is should be null.");
  577. },
  578. testGetSubWithBooleanConverter : function(){
  579. var value = Y.Cookie.getSub("data", "found", Boolean);
  580. Assert.isBoolean(value, "Retrieved subcookie value should be a boolean.");
  581. Assert.isTrue(value, "Retrieved subcookie value should be true.");
  582. },
  583. testGetSubWithNumberConverter : function(){
  584. var value = Y.Cookie.getSub("data", "count", Number);
  585. Assert.isNumber(value, "Retrieved subcookie value should be a number.");
  586. Assert.areEqual(11, value, "Retrieved subcookie value should be 11.");
  587. },
  588. testGetSubWithCustomConverter : function(){
  589. var value = Y.Cookie.getSub("data", "count", function(value){
  590. if (value === "11"){
  591. return true;
  592. } else {
  593. return false;
  594. }
  595. });
  596. Assert.isBoolean(value, "Retrieved subcookie value should be a boolean.");
  597. Assert.isTrue(value, "Retrieved subcookie value should be true.");
  598. },
  599. testGetSubWithInvalidConverter : function(){
  600. var value = Y.Cookie.getSub("data", "count", true);
  601. Assert.isString(value, "Retrieved subcookie value should be a string.");
  602. Assert.areEqual("11", value, "Retrieved subcookie value should be 11.");
  603. },
  604. testSubGetWithConverterAndUnknownCookie : function(){
  605. var value = Y.Cookie.getSub("data", "count2", Number);
  606. Assert.isNull(value, "Retrieved subcookie value should be null.");
  607. },
  608. testSubGetSpecial : function(){
  609. var value = Y.Cookie.getSub("data", "special");
  610. Assert.areEqual("Something with & and =", value, "Sub cookie string is incorrect.");
  611. }
  612. }));
  613. //-------------------------------------------------------------------------
  614. // Test Case for removing individual cookie sub.
  615. //-------------------------------------------------------------------------
  616. suite.add(new Y.Test.Case({
  617. name : "Remove Cookie Sub Tests",
  618. _should : {
  619. error : {
  620. testRemoveSubInvalidName1 : new TypeError("Cookie name must be a non-empty string."),
  621. testRemoveSubInvalidName2 : new TypeError("Cookie name must be a non-empty string."),
  622. testRemoveSubInvalidName3 : new TypeError("Cookie name must be a non-empty string."),
  623. testRemoveSubInvalidName4 : new TypeError("Cookie name must be a non-empty string."),
  624. testRemoveSubInvalidName5 : new TypeError("Cookie name must be a non-empty string."),
  625. testRemoveSubInvalidSubName1 : new TypeError("Subcookie name must be a non-empty string."),
  626. testRemoveSubInvalidSubName2 : new TypeError("Subcookie name must be a non-empty string."),
  627. testRemoveSubInvalidSubName3 : new TypeError("Subcookie name must be a non-empty string."),
  628. testRemoveSubInvalidSubName4 : new TypeError("Subcookie name must be a non-empty string."),
  629. testRemoveSubInvalidSubName5 : new TypeError("Subcookie name must be a non-empty string.")
  630. }
  631. },
  632. setUp: function(){
  633. 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" };
  634. Y.Cookie._setDoc(this.stubDoc);
  635. },
  636. tearDown: function(){
  637. Y.Cookie._setDoc(Y.config.doc);
  638. delete this.stubDoc;
  639. },
  640. //---------------------------------------------------------------------
  641. // Tests
  642. //---------------------------------------------------------------------
  643. testRemoveSubSimple : function(){
  644. var value = Y.Cookie.removeSub("data", "c");
  645. 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.");
  646. },
  647. testRemoveSubUnknown : function(){
  648. var value = Y.Cookie.removeSub("data", "i");
  649. Assert.areEqual("", value, "Cookie string is incorrect.");
  650. },
  651. testRemoveSubInvalidName1 : function(){
  652. Y.Cookie.removeSub(12);
  653. },
  654. testRemoveSubInvalidName2 : function(){
  655. Y.Cookie.removeSub(true);
  656. },
  657. testRemoveSubInvalidName3 : function(){
  658. Y.Cookie.removeSub("");
  659. },
  660. testRemoveSubInvalidName4 : function(){
  661. Y.Cookie.removeSub();
  662. },
  663. testRemoveSubInvalidName5 : function(){
  664. Y.Cookie.removeSub(null);
  665. },
  666. testRemoveSubInvalidSubName1 : function(){
  667. Y.Cookie.removeSub("data", 12);
  668. },
  669. testRemoveSubInvalidSubName2 : function(){
  670. Y.Cookie.removeSub("data", true);
  671. },
  672. testRemoveSubInvalidSubName3 : function(){
  673. Y.Cookie.removeSub("data", "");
  674. },
  675. testRemoveSubInvalidSubName4 : function(){
  676. Y.Cookie.removeSub("data");
  677. },
  678. testRemoveSubInvalidSubName5 : function(){
  679. Y.Cookie.removeSub("data", null);
  680. },
  681. testRemoveSubOnNonExistantCookie : function(){
  682. var value = Y.Cookie.removeSub("invalid", "i");
  683. Assert.areEqual("",value, "Cookie string is incorrect.");
  684. },
  685. testRemoveLastSub : function(){
  686. Y.Cookie.removeSub("info", "a");
  687. var value = Y.Cookie.get("info");
  688. Assert.areEqual("", value, "Cookie string is incorrect.");
  689. },
  690. testRemoveLastSubWithTrueRemoveIfEmpty : function(){
  691. Y.Cookie.removeSub("info", "a", { removeIfEmpty: true });
  692. Assert.areEqual("info=; expires=" + (new Date(0)).toUTCString(),
  693. this.stubDoc.cookie,
  694. "The 'info' cookie should be removed.");
  695. //var value = Y.Cookie.get("info");
  696. //Assert.isNull(value, "Cookie value should be null.");
  697. },
  698. testRemoveLastSubWithFalseRemoveIfEmpty : function() {
  699. Y.Cookie.removeSub("info", "a", { removeIfEmpty: false });
  700. var value = Y.Cookie.get("info");
  701. Assert.areEqual("", value, "Cookie string is incorrect.");
  702. },
  703. testRemoveNotLastSubWithTrueRemoveIfEmpty : function(){
  704. var value = Y.Cookie.removeSub("data", "c", { removeIfEmpty: true });
  705. 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.");
  706. },
  707. testRemoveNotLastSubWithFalseRemoveIfEmpty : function() {
  708. var value = Y.Cookie.removeSub("data", "c", { removeIfEmpty: false });
  709. 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.");
  710. },
  711. testRemoveNotLastSubWithInvalidRemoveIfEmpty : function() {
  712. var value = Y.Cookie.removeSub("data", "c", { removeIfEmpty: "blah" });
  713. 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.");
  714. }
  715. }));
  716. //-------------------------------------------------------------------------
  717. // Test Case for removing cookies
  718. //-------------------------------------------------------------------------
  719. suite.add(new Y.Test.Case({
  720. name : "Remove Cookie Tests",
  721. _should : {
  722. error : {
  723. testRemoveInvalidName1 : new TypeError("Cookie name must be a non-empty string."),
  724. testRemoveInvalidName2 : new TypeError("Cookie name must be a non-empty string."),
  725. testRemoveInvalidName3 : new TypeError("Cookie name must be a non-empty string."),
  726. testRemoveInvalidName4 : new TypeError("Cookie name must be a non-empty string."),
  727. testRemoveInvalidName5 : new TypeError("Cookie name must be a non-empty string.")
  728. }
  729. },
  730. setUp: function(){
  731. this.stubDoc = {cookie:"data=1234"};
  732. Y.Cookie._setDoc(this.stubDoc);
  733. },
  734. tearDown: function(){
  735. Y.Cookie._setDoc(Y.config.doc);
  736. delete this.stubDoc;
  737. },
  738. //---------------------------------------------------------------------
  739. // Tests
  740. //---------------------------------------------------------------------
  741. testRemoveSimple : function(){
  742. Y.Cookie.remove("data");
  743. Assert.areEqual("data=; expires=" + (new Date(0)).toUTCString(), this.stubDoc.cookie, "The 'data' cookie should be removed.");
  744. },
  745. /*
  746. * These next five tests pass because they throw an error.
  747. */
  748. testRemoveInvalidName1 : function(){
  749. Y.Cookie.remove();
  750. },
  751. testRemoveInvalidName2 : function(){
  752. Y.Cookie.remove("");
  753. },
  754. testRemoveInvalidName3 : function(){
  755. Y.Cookie.remove(25);
  756. },
  757. testRemoveInvalidName4 : function(){
  758. Y.Cookie.remove(true);
  759. },
  760. testRemoveInvalidName5 : function(){
  761. Y.Cookie.remove(null);
  762. },
  763. /*
  764. * Tests that remove() doesn't change the options object that was passed in.
  765. */
  766. testRemoveWithOptionsIntact: function(){
  767. var options = {};
  768. Y.Cookie.remove("name", options);
  769. Assert.isUndefined(options.expires, "Options should not have an expires property.");
  770. }
  771. }));
  772. //-------------------------------------------------------------------------
  773. // Test Case for setting cookies
  774. //-------------------------------------------------------------------------
  775. suite.add(new Y.Test.Case({
  776. name : "Set Cookie Tests",
  777. _should : {
  778. error : {
  779. testRemoveInvalidName1 : new TypeError("Cookie name must be a non-empty string."),
  780. testRemoveInvalidName2 : new TypeError("Cookie name must be a non-empty string."),
  781. testRemoveInvalidName3 : new TypeError("Cookie name must be a non-empty string."),
  782. testRemoveInvalidName4 : new TypeError("Cookie name must be a non-empty string."),
  783. testRemoveInvalidName5 : new TypeError("Cookie name must be a non-empty string.")
  784. }
  785. },
  786. setUp: function(){
  787. this.stubDoc = {cookie:""};
  788. Y.Cookie._setDoc(this.stubDoc);
  789. },
  790. tearDown: function(){
  791. Y.Cookie._setDoc(Y.config.doc);
  792. delete this.stubDoc;
  793. },
  794. //---------------------------------------------------------------------
  795. // Tests
  796. //---------------------------------------------------------------------
  797. testSetSimple : function(){
  798. var output = Y.Cookie.set("data", "1234");
  799. Assert.areEqual("data=1234", output, "Cookie string format is wrong.");
  800. Assert.areEqual("data=1234", this.stubDoc.cookie, "Cookie was not set onto document.");
  801. },
  802. testSetSimpleWithFalseRaw : function(){
  803. var output = Y.Cookie.set("data", "1234", { raw: false });
  804. Assert.areEqual("data=1234", output, "Cookie string format is wrong.");
  805. Assert.areEqual("data=1234", this.stubDoc.cookie, "Cookie was not set onto document.");
  806. },
  807. testSetSimpleWithTrueRaw : function(){
  808. var output = Y.Cookie.set("data", "1234", { raw: true });
  809. Assert.areEqual("data=1234", output, "Cookie string format is wrong.");
  810. Assert.areEqual("data=1234", this.stubDoc.cookie, "Cookie was not set onto document.");
  811. },
  812. testSetSimpleWithInvalidRaw : function(){
  813. var output = Y.Cookie.set("data", "1234", { raw: "blah" });
  814. Assert.areEqual("data=1234", output, "Cookie string format is wrong.");
  815. Assert.areEqual("data=1234", this.stubDoc.cookie, "Cookie was not set onto document.");
  816. },
  817. testSetSimpleWithPath : function(){
  818. var text = Y.Cookie.set("name", "value", { path: "/" });
  819. Assert.areEqual("name=value; path=/", text, "Cookie string is incorrect.");
  820. Assert.areEqual("name=value; path=/", this.stubDoc.cookie, "Cookie was not set to document.");
  821. },
  822. testSetSimpleWithInvalidPath1 : function(){
  823. var text = Y.Cookie.set("name", "value", { path: 25 });
  824. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  825. Assert.areEqual("name=value", this.stubDoc.cookie, "Cookie was not set to document.");
  826. },
  827. testSetSimpleWithInvalidPath2 : function(){
  828. var text = Y.Cookie.set("name", "value", { path: "" });
  829. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  830. Assert.areEqual("name=value", this.stubDoc.cookie, "Cookie was not set to document.");
  831. },
  832. testSetSimpleWithDomain : function(){
  833. var text = Y.Cookie.set("name", "value", { domain: "yahoo.com" });
  834. Assert.areEqual("name=value; domain=yahoo.com", text, "Cookie string is incorrect.");
  835. Assert.areEqual("name=value; domain=yahoo.com", this.stubDoc.cookie, "Cookie was not set to document.");
  836. },
  837. testSetSimpleWithInvalidDomain1 : function(){
  838. var text = Y.Cookie.set("name", "value", { domain: true });
  839. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  840. Assert.areEqual("name=value", this.stubDoc.cookie, "Cookie was not set to document.");
  841. },
  842. testSetSimpleWithInvalidDomain2 : function(){
  843. var text = Y.Cookie.set("name", "value", { domain: "" });
  844. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  845. //won't work unless the page is loaded from that domain
  846. //Assert.areEqual("name=value", this.stubDoc.cookie, "Cookie was not set to document.");
  847. },
  848. testSetSimpleWithSecure : function(){
  849. var text = Y.Cookie.set("name", "value", { secure: true });
  850. Assert.areEqual("name=value; secure", text, "Cookie string is incorrect.");
  851. Assert.areEqual("name=value; secure", this.stubDoc.cookie, "Cookie was not set to document.");
  852. },
  853. testSetSimpleWithInvalidSecure1 : function(){
  854. var text = Y.Cookie.set("name", "value", { secure: false });
  855. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  856. Assert.areEqual("name=value", this.stubDoc.cookie, "Cookie was not set to document.");
  857. },
  858. testSetSimpleWithInvalidSecure2 : function(){
  859. var text = Y.Cookie.set("name", "value", { secure: "blah" });
  860. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  861. Assert.areEqual("name=value", this.stubDoc.cookie, "Cookie was not set to document.");
  862. },
  863. testSetSimpleWithExpiry : function(){
  864. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  865. var text = Y.Cookie.set("name", "value", { expires: expires });
  866. Assert.areEqual("name=value; expires=" + expires.toUTCString(), text, "Cookie string is incorrect.");
  867. Assert.areEqual("name=value; expires=" + expires.toUTCString(), this.stubDoc.cookie, "Cookie was not set to document.");
  868. },
  869. testSetSimpleWithInvalidExpiry : function(){
  870. var text = Y.Cookie.set("name", "value", { expires: "blah" });
  871. Assert.areEqual("name=value", text, "Cookie string is incorrect.");
  872. Assert.areEqual("name=value", this.stubDoc.cookie, "Cookie was not set to document.");
  873. },
  874. testSetSimpleWithAll : function(){
  875. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  876. var text = Y.Cookie.set("name", "value", { expires: expires, domain : "yahoo.com", path: "/", secure: true });
  877. Assert.areEqual("name=value; expires=" + expires.toUTCString() + "; path=/; domain=yahoo.com; secure", text, "Cookie string is incorrect.");
  878. Assert.areEqual("name=value; expires=" + expires.toUTCString() + "; path=/; domain=yahoo.com; secure", this.stubDoc.cookie, "Cookie was not set to document.");
  879. },
  880. testSetComplex : function(){
  881. var name = "c.f name";
  882. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  883. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  884. var text = Y.Cookie.set(name, value);
  885. Assert.areEqual(result, text, "Cookie string is incorrect.");
  886. Assert.areEqual(result, this.stubDoc.cookie, "Cookie was not set to document.");
  887. },
  888. testSetComplexWithFalseRaw : function(){
  889. var name = "c.f name";
  890. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  891. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  892. var text = Y.Cookie.set(name, value, { raw: false });
  893. Assert.areEqual(result, text, "Cookie string is incorrect.");
  894. Assert.areEqual(result, this.stubDoc.cookie, "Cookie was not set to document.");
  895. },
  896. testSetComplexWithTrueRaw : function(){
  897. var name = "c.f name";
  898. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  899. var result = encodeURIComponent(name) + "=" + value;
  900. var text = Y.Cookie.set(name, value, { raw: true });
  901. Assert.areEqual(result, text, "Cookie string is incorrect.");
  902. Assert.areEqual(result, this.stubDoc.cookie, "Cookie was not set to document.");
  903. },
  904. testSetComplexWithPath : function(){
  905. var name = "c.f name";
  906. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  907. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  908. var text = Y.Cookie.set(name, value, { path : "/" });
  909. Assert.areEqual(result + "; path=/", text, "Cookie string is incorrect.");
  910. Assert.areEqual(result + "; path=/", this.stubDoc.cookie, "Cookie was not set to document.");
  911. },
  912. testSetComplexWithDomain : function(){
  913. var name = "c.f name";
  914. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  915. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  916. var text = Y.Cookie.set(name, value, { domain: "yahoo.com" });
  917. Assert.areEqual(result + "; domain=yahoo.com", text, "Cookie string is incorrect.");
  918. Assert.areEqual(result + "; domain=yahoo.com", this.stubDoc.cookie, "Cookie was not set to document.");
  919. },
  920. testSetComplexWithSecure : function(){
  921. var name = "c.f name";
  922. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  923. var text = Y.Cookie.set(name, value, { secure: true });
  924. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  925. Assert.areEqual(result + "; secure", text, "Cookie string is incorrect.");
  926. Assert.areEqual(result + "; secure", this.stubDoc.cookie, "Cookie was not set to document.");
  927. },
  928. testSetComplexWithExpiry : function(){
  929. var name = "c.f name";
  930. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  931. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  932. var text = Y.Cookie.set(name, value, { expires : expires });
  933. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  934. Assert.areEqual(result + "; expires=" + expires.toUTCString(), text, "Cookie string is incorrect.");
  935. Assert.areEqual(result + "; expires=" + expires.toUTCString(), this.stubDoc.cookie, "Cookie was not set to document.");
  936. },
  937. testSetComplexWithAll : function(){
  938. var name = "c.f name";
  939. var value = "as.bd ed|ieh,~!!@#$%^*=098345|}{<>?";
  940. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  941. var text = Y.Cookie.set(name, value, { expires: expires, domain : "yahoo.com", path: "/", secure: true });
  942. var result = encodeURIComponent(name) + "=" + encodeURIComponent(value);
  943. Assert.areEqual(result + "; expires=" + expires.toUTCString() + "; path=/; domain=yahoo.com; secure", text, "Cookie string is incorrect.");
  944. Assert.areEqual(result + "; expires=" + expires.toUTCString() + "; path=/; domain=yahoo.com; secure", this.stubDoc.cookie, "Cookie was not set to document.");
  945. }
  946. }));
  947. //-------------------------------------------------------------------------
  948. // Test Case for setting subcookies
  949. //-------------------------------------------------------------------------
  950. suite.add(new Y.Test.Case({
  951. name : "Set Subcookie Tests",
  952. _should : {
  953. error : {
  954. testRemoveInvalidName1 : new TypeError("Cookie name must be a non-empty string."),
  955. testRemoveInvalidName2 : new TypeError("Cookie name must be a non-empty string."),
  956. testRemoveInvalidName3 : new TypeError("Cookie name must be a non-empty string."),
  957. testRemoveInvalidName4 : new TypeError("Cookie name must be a non-empty string."),
  958. testRemoveInvalidName5 : new TypeError("Cookie name must be a non-empty string.")
  959. }
  960. },
  961. setUp: function(){
  962. this.stubDoc = {cookie:""};
  963. Y.Cookie._setDoc(this.stubDoc);
  964. },
  965. tearDown: function(){
  966. Y.Cookie._setDoc(Y.config.doc);
  967. delete this.stubDoc;
  968. },
  969. //---------------------------------------------------------------------
  970. // Tests
  971. //---------------------------------------------------------------------
  972. testSetSubSimple : function(){
  973. var output = Y.Cookie.setSub("data", "value", "1234");
  974. Assert.areEqual("data=value=1234", output, "Cookie string format is wrong.");
  975. Assert.areEqual("data=value=1234", this.stubDoc.cookie, "Cookie was not set onto document.");
  976. },
  977. testSetSubSimpleWithPath : function(){
  978. var text = Y.Cookie.setSub("name", "sub", "value", { path: "/" });
  979. Assert.areEqual("name=sub=value; path=/", text, "Cookie string is incorrect.");
  980. Assert.areEqual("name=sub=value; path=/", this.stubDoc.cookie, "Cookie was not set to document.");
  981. },
  982. testSetSubSimpleWithInvalidPath1 : function(){
  983. var text = Y.Cookie.setSub("name", "sub", "value", { path: 25 });
  984. Assert.areEqual("name=sub=value", text, "Cookie string is incorrect.");
  985. Assert.areEqual("name=sub=value", this.stubDoc.cookie, "Cookie was not set to document.");
  986. },
  987. testSetSubSimpleWithInvalidPath2 : function(){
  988. var text = Y.Cookie.setSub("name", "sub", "value", { path: "" });
  989. Assert.areEqual("name=sub=value", text, "Cookie string is incorrect.");
  990. Assert.areEqual("name=sub=value", this.stubDoc.cookie, "Cookie was not set to document.");
  991. },
  992. testSetSubSimpleWithDomain : function(){
  993. var text = Y.Cookie.setSub("name", "sub", "value", { domain: "yahoo.com" });
  994. Assert.areEqual("name=sub=value; domain=yahoo.com", text, "Cookie string is incorrect.");
  995. Assert.areEqual("name=sub=value; domain=yahoo.com", this.stubDoc.cookie, "Cookie was not set to document.");
  996. },
  997. testSetSubSimpleWithInvalidDomain1 : function(){
  998. var text = Y.Cookie.setSub("name", "sub", "value", { domain: true });
  999. Assert.areEqual("name=sub=value", text, "Cookie string is incorrect.");
  1000. Assert.areEqual("name=sub=value", this.stubDoc.cookie, "Cookie was not set to document.");
  1001. },
  1002. testSetSubSimpleWithInvalidDomain2 : function(){
  1003. var text = Y.Cookie.setSub("name", "sub", "value", { domain: "" });
  1004. Assert.areEqual("name=sub=value", text, "Cookie string is incorrect.");
  1005. Assert.areEqual("name=sub=value", this.stubDoc.cookie, "Cookie was not set to document.");
  1006. },
  1007. testSetSubSimpleWithSecure : function(){
  1008. var text = Y.Cookie.setSub("name", "sub", "value", { secure: true });
  1009. Assert.areEqual("name=sub=value; secure", text, "Cookie string is incorrect.");
  1010. Assert.areEqual("name=sub=value; secure", this.stubDoc.cookie, "Cookie was not set to document.");
  1011. },
  1012. testSetSubSimpleWithInvalidSecure1 : function(){
  1013. var text = Y.Cookie.setSub("name", "sub", "value", { secure: false });
  1014. Assert.areEqual("name=sub=value", text, "Cookie string is incorrect.");
  1015. Assert.areEqual("name=sub=value", this.stubDoc.cookie, "Cookie was not set to document.");
  1016. },
  1017. testSetSubSimpleWithInvalidSecure2 : function(){
  1018. var text = Y.Cookie.setSub("name", "sub", "value", { secure: "blah" });
  1019. Assert.areEqual("name=sub=value", text, "Cookie string is incorrect.");
  1020. Assert.areEqual("name=sub=value", this.stubDoc.cookie, "Cookie was not set to document.");
  1021. },
  1022. testSetSubSimpleWithExpiry : function(){
  1023. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  1024. var text = Y.Cookie.setSub("name", "sub", "value", { expires: expires });
  1025. Assert.areEqual("name=sub=value; expires=" + expires.toUTCString(), text, "Cookie string is incorrect.");
  1026. Assert.areEqual("name=sub=value; expires=" + expires.toUTCString(), this.stubDoc.cookie, "Cookie was not set to document.");
  1027. },
  1028. testSetSubSimpleWithInvalidExpiry : function(){
  1029. var text = Y.Cookie.setSub("name", "sub", "value", { expires: "blah" });
  1030. Assert.areEqual("name=sub=value", text, "Cookie string is incorrect.");
  1031. Assert.areEqual("name=sub=value", this.stubDoc.cookie, "Cookie was not set to document.");
  1032. },
  1033. testSetSubSimpleWithAll : function(){
  1034. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  1035. var text = Y.Cookie.setSub("name", "sub", "value", { expires: expires, domain : "yahoo.com", path: "/", secure: true });
  1036. Assert.areEqual("name=sub=value; expires=" + expires.toUTCString() + "; path=/; domain=yahoo.com; secure", text, "Cookie string is incorrect.");
  1037. Assert.areEqual("name=sub=value; expires=" + expires.toUTCString() + "; path=/; domain=yahoo.com; secure", this.stubDoc.cookie, "Cookie was not set to document.");
  1038. },
  1039. testSetSubSpecial : function(){
  1040. var value = "Something with & and =";
  1041. var cookieText = Y.Cookie.setSub("name", "sub", value);
  1042. Assert.areEqual("name=sub=" + encodeURIComponent(value), cookieText, "Sub cookie string is incorrect.");
  1043. }
  1044. }));
  1045. //-------------------------------------------------------------------------
  1046. // Test Case for setting complete subcookies
  1047. //-------------------------------------------------------------------------
  1048. suite.add(new Y.Test.Case({
  1049. name : "Set Complete Subcookie Tests",
  1050. _should : {
  1051. error : {
  1052. }
  1053. },
  1054. setUp: function(){
  1055. this.stubDoc = {cookie:""};
  1056. Y.Cookie._setDoc(this.stubDoc);
  1057. },
  1058. tearDown: function(){
  1059. Y.Cookie._setDoc(Y.config.doc);
  1060. delete this.stubDoc;
  1061. },
  1062. //---------------------------------------------------------------------
  1063. // Tests
  1064. //---------------------------------------------------------------------
  1065. testSetSubsSimple : function(){
  1066. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"});
  1067. Assert.areEqual("data=a=b&c=d&e=f&g=h", text, "Cookie string format is wrong.");
  1068. Assert.areEqual("data=a=b&c=d&e=f&g=h", this.stubDoc.cookie, "Cookie was not set onto document.");
  1069. },
  1070. testSetSubsSimpleWithPath : function(){
  1071. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { path : "/" });
  1072. Assert.areEqual("data=a=b&c=d&e=f&g=h; path=/", text, "Cookie string is incorrect.");
  1073. Assert.areEqual("data=a=b&c=d&e=f&g=h; path=/", this.stubDoc.cookie, "Cookie was not set to document.");
  1074. },
  1075. testSetSubsSimpleWithInvalidPath1 : function(){
  1076. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { path : 25 });
  1077. Assert.areEqual("data=a=b&c=d&e=f&g=h", text, "Cookie string is incorrect.");
  1078. Assert.areEqual("data=a=b&c=d&e=f&g=h", this.stubDoc.cookie, "Cookie was not set to document.");
  1079. },
  1080. testSetSubsSimpleWithInvalidPath2 : function(){
  1081. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { path : "" });
  1082. Assert.areEqual("data=a=b&c=d&e=f&g=h", text, "Cookie string is incorrect.");
  1083. Assert.areEqual("data=a=b&c=d&e=f&g=h", this.stubDoc.cookie, "Cookie was not set to document.");
  1084. },
  1085. testSetSubsSimpleWithDomain : function(){
  1086. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { domain: "yahoo.com" });
  1087. Assert.areEqual("data=a=b&c=d&e=f&g=h; domain=yahoo.com", text, "Cookie string is incorrect.");
  1088. Assert.areEqual("data=a=b&c=d&e=f&g=h; domain=yahoo.com", this.stubDoc.cookie, "Cookie was not set to document.");
  1089. },
  1090. testSetSubsSimpleWithInvalidDomain1 : function(){
  1091. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { domain: true });
  1092. Assert.areEqual("data=a=b&c=d&e=f&g=h", text, "Cookie string is incorrect.");
  1093. Assert.areEqual("data=a=b&c=d&e=f&g=h", this.stubDoc.cookie, "Cookie was not set to document.");
  1094. },
  1095. testSetSubsSimpleWithInvalidDomain2 : function(){
  1096. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { domain: "" });
  1097. Assert.areEqual("data=a=b&c=d&e=f&g=h", text, "Cookie string is incorrect.");
  1098. Assert.areEqual("data=a=b&c=d&e=f&g=h", this.stubDoc.cookie, "Cookie was not set to document.");
  1099. },
  1100. testSetSubsSimpleWithSecure : function(){
  1101. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { secure: true });
  1102. Assert.areEqual("data=a=b&c=d&e=f&g=h; secure", text, "Cookie string is incorrect.");
  1103. Assert.areEqual("data=a=b&c=d&e=f&g=h; secure", this.stubDoc.cookie, "Cookie was not set to document.");
  1104. },
  1105. testSetSubsSimpleWithInvalidSecure1 : function(){
  1106. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { secure: false });
  1107. Assert.areEqual("data=a=b&c=d&e=f&g=h", text, "Cookie string is incorrect.");
  1108. Assert.areEqual("data=a=b&c=d&e=f&g=h", this.stubDoc.cookie, "Cookie was not set to document.");
  1109. },
  1110. testSetSubsSimpleWithInvalidSecure2 : function(){
  1111. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { secure: "blah" });
  1112. Assert.areEqual("data=a=b&c=d&e=f&g=h", text, "Cookie string is incorrect.");
  1113. Assert.areEqual("data=a=b&c=d&e=f&g=h", this.stubDoc.cookie, "Cookie was not set to document.");
  1114. },
  1115. testSetSubsSimpleWithExpiry : function(){
  1116. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  1117. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { expires: expires });
  1118. Assert.areEqual("data=a=b&c=d&e=f&g=h; expires=" + expires.toUTCString(), text, "Cookie string is incorrect.");
  1119. Assert.areEqual("data=a=b&c=d&e=f&g=h; expires=" + expires.toUTCString(), this.stubDoc.cookie, "Cookie was not set to document.");
  1120. },
  1121. testSetSubsSimpleWithInvalidExpiry : function(){
  1122. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { expires: "blah" });
  1123. Assert.areEqual("data=a=b&c=d&e=f&g=h", text, "Cookie string is incorrect.");
  1124. Assert.areEqual("data=a=b&c=d&e=f&g=h", this.stubDoc.cookie, "Cookie was not set to document.");
  1125. },
  1126. testSetSubsSimpleWithAll : function(){
  1127. var expires = new Date("Wed, 01 Jan 2070 00:00:00 GMT");
  1128. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"}, { expires: expires, domain : "yahoo.com", path: "/", secure: true });
  1129. Assert.areEqual("data=a=b&c=d&e=f&g=h; expires=" + expires.toUTCString() + "; path=/; domain=yahoo.com; secure", text, "Cookie string is incorrect.");
  1130. Assert.areEqual("data=a=b&c=d&e=f&g=h; expires=" + expires.toUTCString() + "; path=/; domain=yahoo.com; secure", this.stubDoc.cookie, "Cookie was not set to document.");
  1131. },
  1132. testSetSubsValues : function(){
  1133. var text = Y.Cookie.setSubs("data", {a: "b",c: "d",e: "f",g: "h"});
  1134. //try to get the values
  1135. var a = Y.Cookie.getSub("data", "a");
  1136. var c = Y.Cookie.getSub("data", "c");
  1137. var e = Y.Cookie.getSub("data", "e");
  1138. var g = Y.Cookie.getSub("data", "g");
  1139. Assert.areEqual("b", a, "Value of 'a' subcookie is wrong.");
  1140. Assert.areEqual("d", c, "Value of 'c' subcookie is wrong.");
  1141. Assert.areEqual("f", e, "Value of 'e' subcookie is wrong.");
  1142. Assert.areEqual("h", g, "Value of 'g' subcookie is wrong.");
  1143. }
  1144. }));
  1145. Y.Test.Runner.add(suite);
  1146. });