Skip to content

Commit

Permalink
part 11 - CookieStore API - Extra tests for document URLs with fragments
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D221412

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1475599
gecko-commit: 8283b857e25f518b2a372ea2242c08d7c9df9800
gecko-reviewers: smaug
  • Loading branch information
bakulf authored and moz-wptsync-bot committed Sep 13, 2024
1 parent cc35f23 commit 6159022
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 2 deletions.
36 changes: 36 additions & 0 deletions cookie-store/cookieStore_getAll_arguments.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,39 @@ promise_test(async testCase => {
await promise_rejects_js(testCase, TypeError, cookieStore.getAll(
{ url: invalid_url }));
}, 'cookieStore.getAll with invalid url host in options');

promise_test(async testCase => {
await cookieStore.set('cookie-name', 'cookie-value');
testCase.add_cleanup(async () => {
await cookieStore.delete('cookie-name');
});

let target_url = self.location.href;
if (self.GLOBAL.isWorker()) {
target_url = target_url + '/path/within/scope';
}

target_url = target_url + "#foo";

const cookies = await cookieStore.getAll({ url: target_url });
assert_equals(cookies.length, 1);
assert_equals(cookies[0].name, 'cookie-name');
assert_equals(cookies[0].value, 'cookie-value');
}, 'cookieStore.getAll with absolute url with fragment in options');

promise_test(async testCase => {
if (!self.GLOBAL.isWorker()) {
await cookieStore.set('cookie-name', 'cookie-value');
testCase.add_cleanup(async () => {
await cookieStore.delete('cookie-name');
});

self.location = "#foo";
let target_url = self.location.href;

const cookies = await cookieStore.getAll({ url: target_url });
assert_equals(cookies.length, 1);
assert_equals(cookies[0].name, 'cookie-name');
assert_equals(cookies[0].value, 'cookie-value');
}
}, 'cookieStore.getAll with absolute different url in options');
34 changes: 34 additions & 0 deletions cookie-store/cookieStore_get_arguments.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,37 @@ promise_test(async testCase => {
await promise_rejects_js(testCase, TypeError, cookieStore.get(
{ url: invalid_url }));
}, 'cookieStore.get with invalid url host in options');

promise_test(async testCase => {
await cookieStore.set('cookie-name', 'cookie-value');
testCase.add_cleanup(async () => {
await cookieStore.delete('cookie-name');
});

let target_url = self.location.href;
if (self.GLOBAL.isWorker()) {
target_url = target_url + '/path/within/scope';
}

target_url = target_url + "#foo";

const cookie = await cookieStore.get({ url: target_url });
assert_equals(cookie.name, 'cookie-name');
assert_equals(cookie.value, 'cookie-value');
}, 'cookieStore.get with absolute url with fragment in options');

promise_test(async testCase => {
if (!self.GLOBAL.isWorker()) {
await cookieStore.set('cookie-name', 'cookie-value');
testCase.add_cleanup(async () => {
await cookieStore.delete('cookie-name');
});

self.location = "#foo";
let target_url = self.location.href;

const cookie = await cookieStore.get({ url: target_url });
assert_equals(cookie.name, 'cookie-name');
assert_equals(cookie.value, 'cookie-value');
}
}, 'cookieStore.get with absolute different url in options');
86 changes: 86 additions & 0 deletions cookie-store/cookieStore_get_set_creation_url.sub.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!doctype html>
<meta charset='utf-8'>
<title>Async Cookies: cookieStore basic API on creation URL with fragments</title>
<link rel='help' href='https://github.com/WICG/cookie-store'>
<link rel='author' href='[email protected]' title='Andrea Marchesini'>
<script src='/resources/testharness.js'></script>
<script src='/resources/testharnessreport.js'></script>
<script src='resources/helpers.js'></script>
<style>iframe { display: none; }</style>

<script>
'use strict';

const kUrl = '/cookie-store/resources/helper_iframe.sub.html';

promise_test(async t => {
const url = new URL(kUrl, location);

const iframe = await createIframe(url + "#fragment", t);
assert_true(iframe != null);

iframe.contentWindow.postMessage({
opname: 'set-cookie',
name: 'cookie-name',
value: 'cookie-value',
}, '*');

t.add_cleanup(async () => {
await cookieStore.delete({ name: 'cookie-name', domain: '{{host}}' });
});

await waitForMessage();

iframe.contentWindow.postMessage({
opname: 'get-cookie',
name: 'cookie-name',
options: { url: url.href }
}, '*');

const message = await waitForMessage();

const { frameCookie } = message;
assert_not_equals(frameCookie, null);
assert_equals(frameCookie.name, 'cookie-name');
assert_equals(frameCookie.value, 'cookie-value');
}, 'cookieStore.get() option url ignores fragments');

promise_test(async t => {
const url = new URL(kUrl, location);

const iframe = await createIframe(url + "#fragment", t);
assert_true(iframe != null);

iframe.contentWindow.postMessage({
opname: 'set-cookie',
name: 'cookie-name',
value: 'cookie-value',
}, '*');

t.add_cleanup(async () => {
await cookieStore.delete({ name: 'cookie-name', domain: '{{host}}' });
});

await waitForMessage();

iframe.contentWindow.postMessage({
opname: 'push-state',
}, '*');

await waitForMessage();

iframe.contentWindow.postMessage({
opname: 'get-cookie',
name: 'cookie-name',
options: { url: url.href }
}, '*');

const message = await waitForMessage();

const { frameCookie } = message;
assert_not_equals(frameCookie, null);
assert_equals(frameCookie.name, 'cookie-name');
assert_equals(frameCookie.value, 'cookie-value');
}, 'cookieStore.get() option url + pushState()');

</script>
7 changes: 5 additions & 2 deletions cookie-store/resources/helper_iframe.sub.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
});
event.source.postMessage('Cookie has been set', event.origin);
} else if (opname === 'get-cookie') {
const { name } = event.data
const frameCookie = await cookieStore.get(name);
const { name, options } = event.data
const frameCookie = await cookieStore.get(name, options);
event.source.postMessage({frameCookie}, event.origin);
} else if (opname === 'push-state') {
history.pushState("foo", null, "some/path");
event.source.postMessage('pushState called');
}
});
</script>

0 comments on commit 6159022

Please sign in to comment.