@@ -182,31 +182,46 @@ QUnit.test('expires option as days from now', function (assert) {
182
182
assert . expect ( 1 ) ;
183
183
var sevenDaysFromNow = new Date ( ) ;
184
184
sevenDaysFromNow . setDate ( sevenDaysFromNow . getDate ( ) + 21 ) ;
185
- var expected = 'c=v; expires=' + sevenDaysFromNow . toUTCString ( ) ;
186
- var actual = Cookies . set ( 'c' , 'v' , { expires : 21 } ) . substring ( 0 , expected . length ) ;
187
- assert . strictEqual ( actual , expected , 'should write the cookie string with expires' ) ;
185
+ var expected = 'expires=' + sevenDaysFromNow . toUTCString ( ) ;
186
+ var actual = Cookies . set ( 'c' , 'v' , { expires : 21 } ) ;
187
+ assert . ok ( actual . indexOf ( expected ) !== - 1 , quoted ( actual ) + ' includes ' + quoted ( expected ) ) ;
188
188
} ) ;
189
189
190
+ // github.com/carhartl/jquery-cookie/issues/246
190
191
QUnit . test ( 'expires option as fraction of a day' , function ( assert ) {
191
192
assert . expect ( 1 ) ;
192
193
193
- var now = new Date ( ) . getTime ( ) ;
194
- var stringifiedDate = Cookies . set ( 'c' , 'v' , { expires : 0.5 } ) . split ( '; ' ) [ 1 ] . split ( '=' ) [ 1 ] ;
195
- var expires = Date . parse ( stringifiedDate ) ;
194
+ var findValueForAttributeName = function ( createdCookie , attributeName ) {
195
+ var pairs = createdCookie . split ( '; ' ) ;
196
+ var foundAttributeValue ;
197
+ pairs . forEach ( function ( pair ) {
198
+ if ( pair . split ( '=' ) [ 0 ] === attributeName ) {
199
+ foundAttributeValue = pair . split ( '=' ) [ 1 ] ;
200
+ }
201
+ } ) ;
202
+ return foundAttributeValue ;
203
+ } ;
204
+ var now = new Date ( ) ;
205
+ var stringifiedDate = findValueForAttributeName ( Cookies . set ( 'c' , 'v' , { expires : 0.5 } ) , 'expires' ) ;
206
+ var expires = new Date ( stringifiedDate ) ;
196
207
197
208
// When we were using Date.setDate() fractions have been ignored
198
209
// and expires resulted in the current date. Allow 1000 milliseconds
199
- // difference for execution time.
200
- assert . ok ( expires > now + 1000 , 'should write expires attribute with the correct date' ) ;
210
+ // difference for execution time because new Date() can be different,
211
+ // even when it's run synchronously.
212
+ // See https://github.com/js-cookie/js-cookie/commit/ecb597b65e4c477baa2b30a2a5a67fdaee9870ea#commitcomment-20146048.
213
+ var assertion = expires . getTime ( ) > now . getTime ( ) + 1000 ;
214
+ var message = quoted ( expires . getTime ( ) ) + ' should be greater than ' + quoted ( now . getTime ( ) ) ;
215
+ assert . ok ( assertion , message ) ;
201
216
} ) ;
202
217
203
218
QUnit . test ( 'expires option as Date instance' , function ( assert ) {
204
219
assert . expect ( 1 ) ;
205
220
var sevenDaysFromNow = new Date ( ) ;
206
221
sevenDaysFromNow . setDate ( sevenDaysFromNow . getDate ( ) + 7 ) ;
207
- var expected = 'c=v; expires=' + sevenDaysFromNow . toUTCString ( ) ;
208
- var actual = Cookies . set ( 'c' , 'v' , { expires : sevenDaysFromNow } ) . substring ( 0 , expected . length ) ;
209
- assert . strictEqual ( actual , expected , 'should write the cookie string with expires' ) ;
222
+ var expected = 'expires=' + sevenDaysFromNow . toUTCString ( ) ;
223
+ var actual = Cookies . set ( 'c' , 'v' , { expires : sevenDaysFromNow } ) ;
224
+ assert . ok ( actual . indexOf ( expected ) !== - 1 , quoted ( actual ) + ' includes ' + quoted ( expected ) ) ;
210
225
} ) ;
211
226
212
227
QUnit . test ( 'return value' , function ( assert ) {
@@ -235,6 +250,13 @@ QUnit.test('API for changing defaults', function (assert) {
235
250
assert . ok ( Cookies . set ( 'c' , 'v' ) . match ( / p a t h = \/ / ) , 'should roll back to the default path' ) ;
236
251
} ) ;
237
252
253
+ QUnit . test ( 'true secure value' , function ( assert ) {
254
+ assert . expect ( 1 ) ;
255
+ var expected = 'c=v; path=/; secure' ;
256
+ var actual = Cookies . set ( 'c' , 'v' , { secure : true } ) ;
257
+ assert . strictEqual ( actual , expected , 'should add secure attribute' ) ;
258
+ } ) ;
259
+
238
260
// github.com/js-cookie/js-cookie/pull/54
239
261
QUnit . test ( 'false secure value' , function ( assert ) {
240
262
assert . expect ( 1 ) ;
@@ -243,8 +265,18 @@ QUnit.test('false secure value', function (assert) {
243
265
assert . strictEqual ( actual , expected , 'false should not modify path in cookie string' ) ;
244
266
} ) ;
245
267
268
+ // github.com/js-cookie/js-cookie/issues/276
269
+ QUnit . test ( 'unofficial attribute' , function ( assert ) {
270
+ assert . expect ( 1 ) ;
271
+ var expected = 'c=v; path=/; unofficial=anything' ;
272
+ var actual = Cookies . set ( 'c' , 'v' , {
273
+ unofficial : 'anything'
274
+ } ) ;
275
+ assert . strictEqual ( expected , actual , 'should write the cookie string with unofficial attribute' ) ;
276
+ } ) ;
277
+
246
278
QUnit . test ( 'undefined attribute value' , function ( assert ) {
247
- assert . expect ( 4 ) ;
279
+ assert . expect ( 5 ) ;
248
280
assert . strictEqual ( Cookies . set ( 'c' , 'v' , {
249
281
expires : undefined
250
282
} ) , 'c=v; path=/' , 'should not write undefined expires attribute' ) ;
@@ -257,6 +289,9 @@ QUnit.test('undefined attribute value', function (assert) {
257
289
assert . strictEqual ( Cookies . set ( 'c' , 'v' , {
258
290
secure : undefined
259
291
} ) , 'c=v; path=/' , 'should not write undefined secure attribute' ) ;
292
+ assert . strictEqual ( Cookies . set ( 'c' , 'v' , {
293
+ unofficial : undefined
294
+ } ) , 'c=v; path=/' , 'should not write undefined unofficial attribute' ) ;
260
295
} ) ;
261
296
262
297
QUnit . module ( 'remove' , lifecycle ) ;
0 commit comments