Bug 1475599 - part 11 - CookieStore API - Extra tests for document URLs with fragments, r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D221412
This commit is contained in:
Andrea Marchesini 2024-09-10 16:31:42 +00:00
Родитель fe60b0827c
Коммит 36b81dbf5d
4 изменённых файлов: 161 добавлений и 2 удалений

Просмотреть файл

@ -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');

Просмотреть файл

@ -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');

Просмотреть файл

@ -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='baku@mozilla.com' 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>

Просмотреть файл

@ -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>