Diligent web site
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Required for exposing test results to the Sauce Labs API.
  2. // Can be removed when the following issue is fixed:
  3. // https://github.com/axemclion/grunt-saucelabs/issues/84
  4. QUnit.done(function (details) {
  5. window.global_test_results = details;
  6. });
  7. var lifecycle = {
  8. teardown: function () {
  9. $.cookie.defaults = {};
  10. delete $.cookie.raw;
  11. delete $.cookie.json;
  12. $.each($.cookie(), $.removeCookie);
  13. }
  14. };
  15. module('read', lifecycle);
  16. test('simple value', function () {
  17. expect(1);
  18. document.cookie = 'c=v';
  19. strictEqual($.cookie('c'), 'v', 'should return value');
  20. });
  21. test('empty value', function () {
  22. expect(1);
  23. // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, which
  24. // resulted in a bug while reading such a cookie.
  25. $.cookie('c', '');
  26. strictEqual($.cookie('c'), '', 'should return value');
  27. });
  28. test('not existing', function () {
  29. expect(1);
  30. strictEqual($.cookie('whatever'), undefined, 'return undefined');
  31. });
  32. test('RFC 2068 quoted string', function () {
  33. expect(1);
  34. document.cookie = 'c="v@address.com\\"\\\\\\""';
  35. strictEqual($.cookie('c'), 'v@address.com"\\"', 'should decode RFC 2068 quoted string');
  36. });
  37. test('decode', function () {
  38. expect(1);
  39. document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');
  40. strictEqual($.cookie(' c'), ' v', 'should decode key and value');
  41. });
  42. test('decode pluses to space for server side written cookie', function () {
  43. expect(1);
  44. document.cookie = 'c=foo+bar';
  45. strictEqual($.cookie('c'), 'foo bar', 'should convert pluses back to space');
  46. });
  47. test('raw = true', function () {
  48. expect(2);
  49. $.cookie.raw = true;
  50. document.cookie = 'c=%20v';
  51. strictEqual($.cookie('c'), '%20v', 'should not decode value');
  52. // see https://github.com/carhartl/jquery-cookie/issues/50
  53. $.cookie('c', 'foo=bar');
  54. strictEqual($.cookie('c'), 'foo=bar', 'should include the entire value');
  55. });
  56. test('json = true', function () {
  57. expect(1);
  58. if ('JSON' in window) {
  59. $.cookie.json = true;
  60. $.cookie('c', { foo: 'bar' });
  61. deepEqual($.cookie('c'), { foo: 'bar' }, 'should parse JSON');
  62. } else {
  63. ok(true);
  64. }
  65. });
  66. test('not existing with json = true', function () {
  67. expect(1);
  68. if ('JSON' in window) {
  69. $.cookie.json = true;
  70. strictEqual($.cookie('whatever'), undefined, "won't throw exception");
  71. } else {
  72. ok(true);
  73. }
  74. });
  75. test('string with json = true', function () {
  76. expect(1);
  77. if ('JSON' in window) {
  78. $.cookie.json = true;
  79. $.cookie('c', 'v');
  80. strictEqual($.cookie('c'), 'v', 'should return value');
  81. } else {
  82. ok(true);
  83. }
  84. });
  85. test('invalid JSON string with json = true', function () {
  86. expect(1);
  87. if ('JSON' in window) {
  88. $.cookie('c', 'v');
  89. $.cookie.json = true;
  90. strictEqual($.cookie('c'), undefined, "won't throw exception, returns undefined");
  91. } else {
  92. ok(true);
  93. }
  94. });
  95. test('invalid URL encoding', function () {
  96. expect(1);
  97. document.cookie = 'bad=foo%';
  98. strictEqual($.cookie('bad'), undefined, "won't throw exception, returns undefined");
  99. // Delete manually here because it requires raw === true...
  100. $.cookie.raw = true;
  101. $.removeCookie('bad');
  102. });
  103. asyncTest('malformed cookie value in IE (#88, #117)', function () {
  104. expect(1);
  105. // Sandbox in an iframe so that we can poke around with document.cookie.
  106. var iframe = $('<iframe src="malformed_cookie.html"></iframe>')[0];
  107. $(iframe).on('load', function () {
  108. start();
  109. if (iframe.contentWindow.ok) {
  110. strictEqual(iframe.contentWindow.testValue, 'two', 'reads all cookie values, skipping duplicate occurences of "; "');
  111. } else {
  112. // Skip the test where we can't stub document.cookie using
  113. // Object.defineProperty. Seems to work fine in
  114. // Chrome, Firefox and IE 8+.
  115. ok(true, 'N/A');
  116. }
  117. });
  118. document.body.appendChild(iframe);
  119. });
  120. test('Call to read all when there are cookies', function () {
  121. $.cookie('c', 'v');
  122. $.cookie('foo', 'bar');
  123. deepEqual($.cookie(), { c: 'v', foo: 'bar' }, 'returns object containing all cookies');
  124. });
  125. test('Call to read all when there are no cookies at all', function () {
  126. deepEqual($.cookie(), {}, 'returns empty object');
  127. });
  128. test('Call to read all with json: true', function () {
  129. $.cookie.json = true;
  130. $.cookie('c', { foo: 'bar' });
  131. deepEqual($.cookie(), { c: { foo: 'bar' } }, 'returns JSON parsed cookies');
  132. });
  133. test('Call to read all with a badly encoded cookie', function () {
  134. expect(1);
  135. document.cookie = 'bad=foo%';
  136. document.cookie = 'good=foo';
  137. deepEqual($.cookie(), { good: 'foo' }, 'returns object containing all decodable cookies');
  138. // Delete manually here because it requires raw === true...
  139. $.cookie.raw = true;
  140. $.removeCookie('bad');
  141. });
  142. module('write', lifecycle);
  143. test('String primitive', function () {
  144. expect(1);
  145. $.cookie('c', 'v');
  146. strictEqual($.cookie('c'), 'v', 'should write value');
  147. });
  148. test('String object', function () {
  149. expect(1);
  150. $.cookie('c', new String('v'));
  151. strictEqual($.cookie('c'), 'v', 'should write value');
  152. });
  153. test('value "[object Object]"', function () {
  154. expect(1);
  155. $.cookie('c', '[object Object]');
  156. strictEqual($.cookie('c'), '[object Object]', 'should write value');
  157. });
  158. test('number', function () {
  159. expect(1);
  160. $.cookie('c', 1234);
  161. strictEqual($.cookie('c'), '1234', 'should write value');
  162. });
  163. test('null', function () {
  164. expect(1);
  165. $.cookie('c', null);
  166. strictEqual($.cookie('c'), 'null', 'should write value');
  167. });
  168. test('undefined', function () {
  169. expect(1);
  170. $.cookie('c', undefined);
  171. strictEqual($.cookie('c'), 'undefined', 'should write value');
  172. });
  173. test('expires option as days from now', function () {
  174. expect(1);
  175. var sevenDaysFromNow = new Date();
  176. sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 21);
  177. strictEqual($.cookie('c', 'v', { expires: 21 }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
  178. 'should write the cookie string with expires');
  179. });
  180. test('expires option as fraction of a day', function () {
  181. expect(1);
  182. var now = new Date().getTime();
  183. var expires = Date.parse($.cookie('c', 'v', { expires: 0.5 }).replace(/.+expires=/, ''));
  184. // When we were using Date.setDate() fractions have been ignored
  185. // and expires resulted in the current date. Allow 1000 milliseconds
  186. // difference for execution time.
  187. ok(expires > now + 1000, 'should write expires attribute with the correct date');
  188. });
  189. test('expires option as Date instance', function () {
  190. expect(1);
  191. var sevenDaysFromNow = new Date();
  192. sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
  193. strictEqual($.cookie('c', 'v', { expires: sevenDaysFromNow }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
  194. 'should write the cookie string with expires');
  195. });
  196. test('return value', function () {
  197. expect(1);
  198. strictEqual($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
  199. });
  200. test('defaults', function () {
  201. expect(2);
  202. $.cookie.defaults.path = '/foo';
  203. ok($.cookie('c', 'v').match(/path=\/foo/), 'should use options from defaults');
  204. ok($.cookie('c', 'v', { path: '/bar' }).match(/path=\/bar/), 'options argument has precedence');
  205. });
  206. test('raw = true', function () {
  207. expect(1);
  208. $.cookie.raw = true;
  209. strictEqual($.cookie('c[1]', 'v[1]'), 'c[1]=v[1]', 'should not encode');
  210. // Delete manually here because it requires raw === true...
  211. $.removeCookie('c[1]');
  212. });
  213. test('json = true', function () {
  214. expect(1);
  215. $.cookie.json = true;
  216. if ('JSON' in window) {
  217. $.cookie('c', { foo: 'bar' });
  218. strictEqual(document.cookie, 'c=' + encodeURIComponent(JSON.stringify({ foo: 'bar' })), 'should stringify JSON');
  219. } else {
  220. ok(true);
  221. }
  222. });
  223. module('removeCookie', lifecycle);
  224. test('deletion', function () {
  225. expect(1);
  226. $.cookie('c', 'v');
  227. $.removeCookie('c');
  228. strictEqual(document.cookie, '', 'should delete the cookie');
  229. });
  230. test('when sucessfully deleted', function () {
  231. expect(1);
  232. $.cookie('c', 'v');
  233. strictEqual($.removeCookie('c'), true, 'returns true');
  234. });
  235. test('when cookie does not exist', function () {
  236. expect(1);
  237. strictEqual($.removeCookie('c'), true, 'returns true');
  238. });
  239. test('when deletion failed', function () {
  240. expect(1);
  241. $.cookie('c', 'v');
  242. var originalCookie = $.cookie;
  243. $.cookie = function () {
  244. // Stub deletion...
  245. if (arguments.length === 1) {
  246. return originalCookie.apply(null, arguments);
  247. }
  248. };
  249. strictEqual($.removeCookie('c'), false, 'returns false');
  250. $.cookie = originalCookie;
  251. });
  252. test('with options', function () {
  253. expect(1);
  254. var options = { path: '/' };
  255. $.cookie('c', 'v', options);
  256. $.removeCookie('c', options);
  257. strictEqual(document.cookie, '', 'should delete the cookie');
  258. });
  259. test('passing options reference', function () {
  260. expect(1);
  261. var options = { path: '/' };
  262. $.cookie('c', 'v', options);
  263. $.removeCookie('c', options);
  264. deepEqual(options, { path: '/' }, "won't alter options object");
  265. });
  266. test('[] used in name', function () {
  267. expect(1);
  268. $.cookie.raw = true;
  269. document.cookie = 'c[1]=foo';
  270. $.removeCookie('c[1]');
  271. strictEqual(document.cookie, '', 'delete the cookie');
  272. });
  273. module('conversion', lifecycle);
  274. test('read converter', function() {
  275. expect(1);
  276. $.cookie('c', '1');
  277. strictEqual($.cookie('c', Number), 1, 'converts read value');
  278. });
  279. test('read converter with raw = true', function() {
  280. expect(1);
  281. $.cookie.raw = true;
  282. $.cookie('c', '1');
  283. strictEqual($.cookie('c', Number), 1, 'does not decode, but converts read value');
  284. });