| 1 |
( function () { |
| 2 |
|
| 3 |
let jqcookie; |
| 4 |
const NOW = 9012, |
| 5 |
DEFAULT_DURATION = 5678, |
| 6 |
defaults = { |
| 7 |
prefix: 'mywiki', |
| 8 |
domain: 'example.org', |
| 9 |
path: '/path', |
| 10 |
expires: DEFAULT_DURATION, |
| 11 |
secure: false |
| 12 |
}, |
| 13 |
mwCookie = require( 'mediawiki.cookie' ), |
| 14 |
setDefaults = mwCookie.setDefaults, |
| 15 |
expiryDate = new Date(); |
| 16 |
|
| 17 |
expiryDate.setTime( NOW + ( DEFAULT_DURATION * 1000 ) ); |
| 18 |
|
| 19 |
QUnit.module( 'mediawiki.cookie', { |
| 20 |
beforeEach: function () { |
| 21 |
jqcookie = sinon.stub( mwCookie.jar, 'cookie' ).returns( null ); |
| 22 |
this.clock = sinon.useFakeTimers( NOW ); |
| 23 |
this.savedDefaults = setDefaults( defaults ); |
| 24 |
}, |
| 25 |
afterEach: function () { |
| 26 |
jqcookie.restore(); |
| 27 |
this.clock.restore(); |
| 28 |
setDefaults( this.savedDefaults ); |
| 29 |
} |
| 30 |
} ); |
| 31 |
|
| 32 |
QUnit.test( 'set( key, value )', ( assert ) => { |
| 33 |
let call; |
| 34 |
|
| 35 |
|
| 36 |
mw.cookie.set( 'foo', 'bar' ); |
| 37 |
|
| 38 |
call = jqcookie.lastCall.args; |
| 39 |
assert.strictEqual( call[ 0 ], 'mywikifoo' ); |
| 40 |
assert.strictEqual( call[ 1 ], 'bar' ); |
| 41 |
assert.deepEqual( call[ 2 ], { |
| 42 |
expires: expiryDate, |
| 43 |
domain: 'example.org', |
| 44 |
path: '/path', |
| 45 |
secure: false |
| 46 |
} ); |
| 47 |
|
| 48 |
mw.cookie.set( 'foo', null ); |
| 49 |
call = jqcookie.lastCall.args; |
| 50 |
assert.strictEqual( call[ 1 ], null, 'null removes cookie' ); |
| 51 |
|
| 52 |
mw.cookie.set( 'foo', undefined ); |
| 53 |
call = jqcookie.lastCall.args; |
| 54 |
assert.strictEqual( call[ 1 ], 'undefined', 'undefined is value' ); |
| 55 |
|
| 56 |
mw.cookie.set( 'foo', false ); |
| 57 |
call = jqcookie.lastCall.args; |
| 58 |
assert.strictEqual( call[ 1 ], 'false', 'false is a value' ); |
| 59 |
|
| 60 |
mw.cookie.set( 'foo', 0 ); |
| 61 |
call = jqcookie.lastCall.args; |
| 62 |
assert.strictEqual( call[ 1 ], '0', '0 is value' ); |
| 63 |
} ); |
| 64 |
|
| 65 |
QUnit.test( 'set( key, value, expires )', ( assert ) => { |
| 66 |
let date, options; |
| 67 |
|
| 68 |
date = new Date(); |
| 69 |
date.setTime( 1234 ); |
| 70 |
|
| 71 |
mw.cookie.set( 'foo', 'bar' ); |
| 72 |
options = jqcookie.lastCall.args[ 2 ]; |
| 73 |
assert.deepEqual( options.expires, expiryDate, 'default expiration' ); |
| 74 |
|
| 75 |
mw.cookie.set( 'foo', 'bar', date ); |
| 76 |
options = jqcookie.lastCall.args[ 2 ]; |
| 77 |
assert.strictEqual( options.expires, date, 'custom expiration as Date' ); |
| 78 |
|
| 79 |
date = new Date(); |
| 80 |
date.setDate( date.getDate() + 1 ); |
| 81 |
|
| 82 |
mw.cookie.set( 'foo', 'bar', 86400 ); |
| 83 |
options = jqcookie.lastCall.args[ 2 ]; |
| 84 |
assert.deepEqual( options.expires, date, 'custom expiration as lifetime in seconds' ); |
| 85 |
|
| 86 |
mw.cookie.set( 'foo', 'bar', null ); |
| 87 |
options = jqcookie.lastCall.args[ 2 ]; |
| 88 |
assert.strictEqual( options.expires, undefined, 'null forces session cookie' ); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
setDefaults( Object.assign( {}, defaults, { expires: 0 } ) ); |
| 93 |
|
| 94 |
mw.cookie.set( 'foo', 'bar' ); |
| 95 |
options = jqcookie.lastCall.args[ 2 ]; |
| 96 |
assert.strictEqual( options.expires, undefined, 'wgCookieExpiration=0 results in session cookies by default' ); |
| 97 |
|
| 98 |
mw.cookie.set( 'foo', 'bar', date ); |
| 99 |
options = jqcookie.lastCall.args[ 2 ]; |
| 100 |
assert.strictEqual( options.expires, date, 'custom expiration (with wgCookieExpiration=0)' ); |
| 101 |
} ); |
| 102 |
|
| 103 |
QUnit.test( 'set( key, value, options )', ( assert ) => { |
| 104 |
mw.cookie.set( 'foo', 'bar', { |
| 105 |
prefix: 'myPrefix', |
| 106 |
domain: 'myDomain', |
| 107 |
path: 'myPath', |
| 108 |
secure: true |
| 109 |
} ); |
| 110 |
|
| 111 |
let call = jqcookie.lastCall.args; |
| 112 |
assert.strictEqual( call[ 0 ], 'myPrefixfoo' ); |
| 113 |
assert.deepEqual( call[ 2 ], { |
| 114 |
expires: expiryDate, |
| 115 |
domain: 'myDomain', |
| 116 |
path: 'myPath', |
| 117 |
secure: true |
| 118 |
}, 'Options (without expires)' ); |
| 119 |
|
| 120 |
const date = new Date(); |
| 121 |
date.setTime( 1234 ); |
| 122 |
|
| 123 |
mw.cookie.set( 'foo', 'bar', { |
| 124 |
expires: date, |
| 125 |
prefix: 'myPrefix', |
| 126 |
domain: 'myDomain', |
| 127 |
path: 'myPath', |
| 128 |
secure: true |
| 129 |
} ); |
| 130 |
|
| 131 |
assert.strictEqual( jqcookie.callCount, 2 ); |
| 132 |
call = jqcookie.lastCall.args; |
| 133 |
assert.strictEqual( call[ 0 ], 'myPrefixfoo' ); |
| 134 |
assert.deepEqual( call[ 2 ], { |
| 135 |
expires: date, |
| 136 |
domain: 'myDomain', |
| 137 |
path: 'myPath', |
| 138 |
secure: true |
| 139 |
}, 'Options (incl. expires)' ); |
| 140 |
} ); |
| 141 |
|
| 142 |
QUnit.test( 'get( key ) - no values', ( assert ) => { |
| 143 |
let key, value; |
| 144 |
|
| 145 |
mw.cookie.get( 'foo' ); |
| 146 |
|
| 147 |
key = jqcookie.lastCall.args[ 0 ]; |
| 148 |
assert.strictEqual( key, 'mywikifoo', 'Default prefix' ); |
| 149 |
|
| 150 |
mw.cookie.get( 'foo', undefined ); |
| 151 |
key = jqcookie.lastCall.args[ 0 ]; |
| 152 |
assert.strictEqual( key, 'mywikifoo', 'Use default prefix for undefined' ); |
| 153 |
|
| 154 |
mw.cookie.get( 'foo', null ); |
| 155 |
key = jqcookie.lastCall.args[ 0 ]; |
| 156 |
assert.strictEqual( key, 'mywikifoo', 'Use default prefix for null' ); |
| 157 |
|
| 158 |
mw.cookie.get( 'foo', '' ); |
| 159 |
key = jqcookie.lastCall.args[ 0 ]; |
| 160 |
assert.strictEqual( key, 'foo', 'Don\'t use default prefix for empty string' ); |
| 161 |
|
| 162 |
value = mw.cookie.get( 'foo' ); |
| 163 |
assert.strictEqual( value, null, 'Return null by default' ); |
| 164 |
|
| 165 |
value = mw.cookie.get( 'foo', null, 'bar' ); |
| 166 |
assert.strictEqual( value, 'bar', 'Custom default value' ); |
| 167 |
} ); |
| 168 |
|
| 169 |
QUnit.test( 'get( key ) - with value', ( assert ) => { |
| 170 |
jqcookie.returns( 'bar' ); |
| 171 |
|
| 172 |
const value = mw.cookie.get( 'foo' ); |
| 173 |
assert.strictEqual( value, 'bar', 'Return value of cookie' ); |
| 174 |
} ); |
| 175 |
|
| 176 |
QUnit.test( 'get( key, prefix )', ( assert ) => { |
| 177 |
mw.cookie.get( 'foo', 'bar' ); |
| 178 |
|
| 179 |
const key = jqcookie.lastCall.args[ 0 ]; |
| 180 |
assert.strictEqual( key, 'barfoo' ); |
| 181 |
} ); |
| 182 |
|
| 183 |
}() ); |