Skip to content

Commit

Permalink
AG-27226 Improve set-cookie — add more supported values. #375
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 4691d95
Merge: 8823108 eace811
Author: Adam Wróblewski <adam@adguard.com>
Date:   Wed Nov 1 12:40:39 2023 +0100

    Merge branch 'master' into fix/AG-27226

commit 8823108
Author: Adam Wróblewski <adam@adguard.com>
Date:   Wed Nov 1 10:49:42 2023 +0100

    Update changelog

commit 04f6e78
Author: Adam Wróblewski <adam@adguard.com>
Date:   Tue Oct 31 18:39:23 2023 +0100

    Add new values to set-cookie scriptlet
    `on`, `off`, `accepted`, `notaccepted`, `rejected`, `allowed`, `disallow`, `enable`, `enabled`, `disable`, `disabled`
  • Loading branch information
AdamWr committed Nov 1, 2023
1 parent eace811 commit 02e3e4e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- new values to `set-cookie` scriptlet: `on`, `off`, `accepted`, `notaccepted`, `rejected`, `allowed`,
`disallow`, `enable`, `enabled`, `disable`, `disabled` [#375](https://github.com/AdguardTeam/Scriptlets/issues/375)
- new values to `set-local-storage-item` and `set-session-storage-item` scriptlets: `on`, `off`
[#366](https://github.com/AdguardTeam/Scriptlets/issues/366)

Expand Down
11 changes: 11 additions & 0 deletions src/helpers/cookie-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,21 @@ export const getLimitedCookieValue = (value: string): string | number | null =>
'no',
'n',
'ok',
'on',
'off',
'accept',
'accepted',
'notaccepted',
'reject',
'rejected',
'allow',
'allowed',
'disallow',
'deny',
'enable',
'enabled',
'disable',
'disabled',
]);

let validValue;
Expand Down
9 changes: 7 additions & 2 deletions src/scriptlets/set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ import {
* - `yes` / `y`
* - `no` / `n`
* - `ok`
* - `accept`/ `reject`
* - `allow` / `deny`
* - `on` / `off`
* - `accept`/ `accepted` / `notaccepted`
* - `reject` / `rejected`
* - `allow` / `allowed`
* - `disallow` / `deny`
* - `enable` / `enabled`
* - `disable` / `disabled`
* - `path` — optional, cookie path, defaults to `/`; possible values:
* - `/` — root path
* - `none` — to set no path at all
Expand Down
77 changes: 77 additions & 0 deletions tests/scriptlets/set-cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,83 @@ test('Set cookie with valid value', (assert) => {
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_on';
cValue = 'on';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_off';
cValue = 'off';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_accepted';
cValue = 'accepted';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_notaccepted';
cValue = 'notaccepted';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_rejected';
cValue = 'rejected';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_allowed';
cValue = 'allowed';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_disallow';
cValue = 'disallow';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_enable';
cValue = 'enable';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_enabled';
cValue = 'enabled';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_disable';
cValue = 'disable';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);

cName = '__test-cookie_disabled';
cValue = 'disabled';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(document.cookie.includes(cName) && document.cookie.includes(cValue), true, 'Cookie is set');
clearCookie(cName);
});

test('Set cookie with invalid value', (assert) => {
Expand Down

0 comments on commit 02e3e4e

Please sign in to comment.