tests.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. var lifecycle = {
  2. teardown: function () {
  3. $.cookie.defaults = {};
  4. delete $.cookie.raw;
  5. delete $.cookie.json;
  6. $.each($.cookie(), $.removeCookie);
  7. }
  8. };
  9. module('read', lifecycle);
  10. test('simple value', function () {
  11. expect(1);
  12. document.cookie = 'c=v';
  13. equal($.cookie('c'), 'v', 'should return value');
  14. });
  15. test('empty value', function () {
  16. expect(1);
  17. // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, which
  18. // resulted in a bug while reading such a cookie.
  19. $.cookie('c', '');
  20. equal($.cookie('c'), '', 'should return value');
  21. });
  22. test('not existing', function () {
  23. expect(1);
  24. strictEqual($.cookie('whatever'), undefined, 'return undefined');
  25. });
  26. test('RFC 2068 quoted string', function () {
  27. expect(1);
  28. document.cookie = 'c="v@address.com\\"\\\\\\""';
  29. equal($.cookie('c'), 'v@address.com"\\"', 'should decode RFC 2068 quoted string');
  30. });
  31. test('decode', function () {
  32. expect(1);
  33. document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');
  34. equal($.cookie(' c'), ' v', 'should decode key and value');
  35. });
  36. test('decode pluses to space for server side written cookie', function () {
  37. expect(1);
  38. document.cookie = 'c=foo+bar';
  39. equal($.cookie('c'), 'foo bar', 'should convert pluses back to space');
  40. });
  41. test('raw = true', function () {
  42. expect(2);
  43. $.cookie.raw = true;
  44. document.cookie = 'c=%20v';
  45. equal($.cookie('c'), '%20v', 'should not decode value');
  46. // see https://github.com/carhartl/jquery-cookie/issues/50
  47. $.cookie('c', 'foo=bar');
  48. equal($.cookie('c'), 'foo=bar', 'should include the entire value');
  49. });
  50. test('json = true', function () {
  51. expect(1);
  52. $.cookie.json = true;
  53. if ('JSON' in window) {
  54. $.cookie('c', { foo: 'bar' });
  55. deepEqual($.cookie('c'), { foo: 'bar'}, 'should parse JSON');
  56. } else {
  57. ok(true);
  58. }
  59. });
  60. test('not existing with json = true', function () {
  61. expect(1);
  62. $.cookie.json = true;
  63. if ('JSON' in window) {
  64. equal($.cookie('whatever'), null, 'should return null');
  65. } else {
  66. ok(true);
  67. }
  68. });
  69. asyncTest('malformed cookie value in IE (#88, #117)', function() {
  70. expect(1);
  71. // Sandbox in an iframe so that we can poke around with document.cookie.
  72. var iframe = $('<iframe src="sandbox.html"></iframe>')[0];
  73. $(iframe).on('load', function() {
  74. start();
  75. if (iframe.contentWindow.ok) {
  76. equal(iframe.contentWindow.testValue, 'two', 'reads all cookie values, skipping duplicate occurences of "; "');
  77. } else {
  78. // Skip the test where we can't stub document.cookie using
  79. // Object.defineProperty. Seems to work fine in
  80. // Chrome, Firefox and IE 8+.
  81. ok(true, 'N/A');
  82. }
  83. });
  84. document.body.appendChild(iframe);
  85. });
  86. test('return all cookies', function() {
  87. $.cookie('c', 'v');
  88. $.cookie('foo', 'bar');
  89. deepEqual($.cookie(), {
  90. c: 'v',
  91. foo: 'bar'
  92. }, 'should return all cookies');
  93. $.each($.cookie(), $.removeCookie);
  94. $.cookie.json = true;
  95. $.cookie('c', { foo: 'bar' });
  96. deepEqual($.cookie(), {
  97. c: { foo: 'bar' }
  98. }, 'should return all cookies with JSON parsed');
  99. });
  100. module('write', lifecycle);
  101. test('String primitive', function () {
  102. expect(1);
  103. $.cookie('c', 'v');
  104. equal($.cookie('c'), 'v', 'should write value');
  105. });
  106. test('String object', function () {
  107. expect(1);
  108. $.cookie('c', new String('v'));
  109. equal($.cookie('c'), 'v', 'should write value');
  110. });
  111. test('value "[object Object]"', function () {
  112. expect(1);
  113. $.cookie('c', '[object Object]');
  114. equal($.cookie('c'), '[object Object]', 'should write value');
  115. });
  116. test('number', function () {
  117. expect(1);
  118. $.cookie('c', 1234);
  119. equal($.cookie('c'), '1234', 'should write value');
  120. });
  121. test('expires option as days from now', function() {
  122. expect(1);
  123. var sevenDaysFromNow = new Date();
  124. sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
  125. equal($.cookie('c', 'v', { expires: 7 }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
  126. 'should write the cookie string with expires');
  127. });
  128. test('expires option as Date instance', function() {
  129. expect(1);
  130. var sevenDaysFromNow = new Date();
  131. sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
  132. equal($.cookie('c', 'v', { expires: sevenDaysFromNow }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
  133. 'should write the cookie string with expires');
  134. });
  135. test('invalid expires option (in the past)', function() {
  136. expect(1);
  137. var yesterday = new Date();
  138. yesterday.setDate(yesterday.getDate() - 1);
  139. $.cookie('c', 'v', { expires: yesterday });
  140. equal($.cookie('c'), null, 'should not save already expired cookie');
  141. });
  142. test('return value', function () {
  143. expect(1);
  144. equal($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
  145. });
  146. test('defaults', function () {
  147. expect(2);
  148. $.cookie.defaults.path = '/foo';
  149. ok($.cookie('c', 'v').match(/path=\/foo/), 'should use options from defaults');
  150. ok($.cookie('c', 'v', { path: '/bar' }).match(/path=\/bar/), 'options argument has precedence');
  151. });
  152. test('raw = true', function () {
  153. expect(1);
  154. $.cookie.raw = true;
  155. equal($.cookie('c', ' v').split('=')[1], ' v', 'should not encode');
  156. });
  157. test('json = true', function () {
  158. expect(1);
  159. $.cookie.json = true;
  160. if ('JSON' in window) {
  161. $.cookie('c', { foo: 'bar' });
  162. equal(document.cookie, 'c=' + encodeURIComponent(JSON.stringify({ foo: 'bar' })), 'should stringify JSON');
  163. } else {
  164. ok(true);
  165. }
  166. });
  167. module('removeCookie', lifecycle);
  168. test('deletion', function() {
  169. expect(1);
  170. $.cookie('c', 'v');
  171. $.removeCookie('c');
  172. equal(document.cookie, '', 'should delete the cookie');
  173. });
  174. test('return', function() {
  175. expect(2);
  176. strictEqual($.removeCookie('c'), false, "return false if the cookie wasn't found");
  177. $.cookie('c', 'v');
  178. strictEqual($.removeCookie('c'), true, 'return true if the cookie was found');
  179. });
  180. test('with options', function() {
  181. expect(3);
  182. var originalCookie = $.cookie;
  183. var callCount = 0;
  184. $.cookie = function() {
  185. callCount++;
  186. if (callCount === 1) {
  187. // see https://github.com/carhartl/jquery-cookie/issues/99
  188. equal(arguments.length, 1, 'look up cookie instead of accidently writing a new');
  189. return 'cookie'; // act as if a cookie was found...
  190. }
  191. if (callCount === 2) {
  192. equal(arguments[2].foo, 'bar', 'pass along options when deleting cookie');
  193. }
  194. };
  195. $.removeCookie('c', { foo: 'bar' });
  196. equal(callCount, 2);
  197. $.cookie = originalCookie;
  198. });