Skip to content

Commit

Permalink
add tests for concatCookieNameValuePath()
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed Mar 12, 2024
1 parent 74fe7d1 commit 2e334e4
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions tests/helpers/cookie-utils.spec.js
@@ -0,0 +1,76 @@
import { concatCookieNameValuePath } from '../../src/helpers/cookie-utils';

describe('concatCookieNameValuePath', () => {
describe('encode cookie value', () => {
test.each([
{
actual: ['name', 'value', ''],
expected: 'name=value;',
},
{
actual: ['name', 'value', '/'],
expected: 'name=value; path=/',
},
{
actual: ['pop::138', '138', ''],
// do not encode cookie name
// https://github.com/AdguardTeam/Scriptlets/issues/408
expected: 'pop::138=138;',
},
{
actual: ['aa::bb::cc', '1', ''],
expected: 'aa::bb::cc=1;',
},
// invalid path
{
actual: ['name', 'value', '/docs'],
// no path is set if unsupported path values passed
expected: 'name=value;',
},
// invalid name because of ';'
{
actual: ['a;bc', 'def', ''],
expected: null,
},
// value with ';' but it should be encoded so its ok
{
actual: ['abc', 'de;f', ''],
expected: 'abc=de%3Bf;',
},
])('$actual -> $expected', ({ actual, expected }) => {
expect(concatCookieNameValuePath(...actual)).toBe(expected);
});
});

describe('no cookie value encoding', () => {
test.each([
{
actual: ['name', 'value', ''],
expected: 'name=value;',
},
{
actual: ['aa::bb::cc', '1', ''],
expected: 'aa::bb::cc=1;',
},
{
actual: ['__w_cc11', '{%22cookies_statistical%22:false%2C%22cookies_ad%22:true}', ''],
// do not encode cookie value
// https://github.com/AdguardTeam/Scriptlets/issues/311
expected: '__w_cc11={%22cookies_statistical%22:false%2C%22cookies_ad%22:true};',
},
// invalid name because of ';'
{
actual: ['a;bc', 'def', ''],
expected: null,
},
// invalid value because of ';' and it is not being encoded
{
actual: ['abc', 'de;f', ''],
expected: null,
},
])('$actual -> $expected', ({ actual, expected }) => {
// explicit 'false' to disable encoding
expect(concatCookieNameValuePath(...actual, false)).toBe(expected);
});
});
});

0 comments on commit 2e334e4

Please sign in to comment.