blob: 3e160c93607412f430091d1bec289118633f33e4 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
<script>
function test()
{
let suite = InspectorTest.createSyncSuite("Cookie");
suite.addTestCase({
name: "WI.Cookie.parseCookieRequestHeader",
description: "Cookie request header.",
test() {
function test(header, expectedCookies) {
InspectorTest.log(`HEADER: Cookie: ${header}`);
let cookies = WI.Cookie.parseCookieRequestHeader(header);
InspectorTest.expectEqual(cookies.length, expectedCookies.length, `Should have ${expectedCookies.length} cookies.`);
for (let i = 0; i < cookies.length; ++i) {
let cookie = cookies[i];
let expected = expectedCookies[i];
InspectorTest.expectThat(cookie instanceof WI.Cookie, `Value should be a WI.Cookie.`);
InspectorTest.expectEqual(cookie.type, WI.Cookie.Type.Request, `cookie.type should be WI.Cookie.Type.Request.`);
InspectorTest.expectEqual(cookie.name, expected.name, `cookie.name should be '${expected.name}'.`);
InspectorTest.expectEqual(cookie.value, expected.value, `cookie.value should be '${expected.value}'.`);
}
InspectorTest.log("");
}
test("", []);
test(`foo=bar`, [
{name: "foo", value: "bar"},
]);
test(`foo=bar; alpha=beta`, [
{name: "foo", value: "bar"},
{name: "alpha", value: "beta"},
]);
test(`foo=a,b,c, d, e, f,g; alpha=123423 qwerty; beta=gamma`, [
{name: "foo", value: "a,b,c, d, e, f,g"},
{name: "alpha", value: "123423 qwerty"},
{name: "beta", value: "gamma"},
]);
return true;
}
});
suite.addTestCase({
name: "WI.Cookie.parseSetCookieResponseHeader",
description: "Set-Cookie response headers.",
test() {
function test(header, expected) {
InspectorTest.log(`HEADER: Set-Cookie: ${header}`);
let cookie = WI.Cookie.parseSetCookieResponseHeader(header);
InspectorTest.expectThat(cookie instanceof WI.Cookie, `Value should be a WI.Cookie.`);
InspectorTest.expectEqual(cookie.header, header, `cookie.header should be the original header text.`);
InspectorTest.expectEqual(cookie.type, WI.Cookie.Type.Response, `cookie.type should be WI.Cookie.Type.Response.`);
InspectorTest.expectEqual(cookie.name, expected.name, `cookie.name should be '${expected.name}'.`);
InspectorTest.expectEqual(cookie.value, expected.value, `cookie.value should be '${expected.value}'.`);
if (cookie.expires && expected.expires)
InspectorTest.expectEqual(cookie.expires.getTime(), expected.expires.getTime(), `cookie.expires should be '${expected.expires}'.`);
else
InspectorTest.expectEqual(cookie.expires, expected.expires, `cookie.expires should be '${expected.expires}'.`);
InspectorTest.expectEqual(cookie.maxAge, expected.maxAge, `cookie.maxAge should be '${expected.maxAge}'.`);
InspectorTest.expectEqual(cookie.path, expected.path, `cookie.path should be '${expected.path}'.`);
InspectorTest.expectEqual(cookie.domain, expected.domain, `cookie.domain should be '${expected.domain}'.`);
InspectorTest.expectEqual(cookie.secure, expected.secure, `cookie.secure should be '${expected.secure}'.`);
InspectorTest.expectEqual(cookie.httpOnly, expected.httpOnly, `cookie.httpOnly should be '${expected.httpOnly}'.`);
InspectorTest.expectEqual(cookie.sameSite, expected.sameSite, `cookie.sameSite should be '${expected.sameSite}'.`);
InspectorTest.log("");
}
InspectorTest.expectNull(WI.Cookie.parseSetCookieResponseHeader(""), "Empty header should produce null.");
test(`name=value`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: null,
domain: null,
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=value; path=/foo`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: "/foo",
domain: null,
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=value; domain=example.com`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: null,
domain: "example.com",
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=value; secure`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: null,
domain: null,
secure: true,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=value; Secure`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: null,
domain: null,
secure: true,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=value; HttpOnly`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: null,
domain: null,
secure: false,
httpOnly: true,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=value; SameSite=lax`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: null,
domain: null,
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.Lax,
});
test(`name=value; SameSite=strict`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: null,
domain: null,
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.Strict,
});
// SameSite with unknown value is ignored.
test(`name=value; SameSite=invalid`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: null,
domain: null,
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
// SameSite without value is ignored.
test(`name=value; SameSite`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: null,
domain: null,
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=value; expires=Fri 06-Oct-2017 03:20:27 GMT; Max-Age=3600`, {
name: "name",
value: "value",
expires: new Date("Fri 06-Oct-2017 03:20:27 GMT"),
maxAge: 3600,
path: null,
domain: null,
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=value; expires=Fri 06-Oct-2017 03:43:47 GMT; Max-Age=5000; path=/foo; domain=example.com; secure; HttpOnly`, {
name: "name",
value: "value",
expires: new Date("Fri 06-Oct-2017 03:43:47 GMT"),
maxAge: 5000,
path: "/foo",
domain: "example.com",
secure: true,
httpOnly: true,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=value; Unknown; path=/one/two`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: "/one/two",
domain: null,
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=value; Unknown=Ignored; path=/one/two`, {
name: "name",
value: "value",
expires: null,
maxAge: null,
path: "/one/two",
domain: null,
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
test(`name=somewhat longer value than normal with spaces, and commas; domain=other.example.com`, {
name: "name",
value: "somewhat longer value than normal with spaces, and commas",
expires: null,
maxAge: null,
path: null,
domain: "other.example.com",
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
// Some servers omit the required space after the semicolon.
test(`name==value=;Domain=.example.com;Expires=Wed, 04-Apr-2018 03:34:02 GMT`, {
name: "name",
value: "=value=",
expires: new Date("Wed, 04-Apr-2018 03:34:02 GMT"),
maxAge: null,
path: null,
domain: ".example.com",
secure: false,
httpOnly: false,
sameSite: WI.Cookie.SameSiteType.None,
});
return true;
}
});
suite.addTestCase({
name: "WI.Cookie.url",
description: "Cookie url property.",
test() {
let cookieWithDomain = WI.Cookie.parseSetCookieResponseHeader(`name=value; domain=example.com`);
InspectorTest.expectEqual(cookieWithDomain.url, "http://example.com", "Cookie url should include domain.");
let cookieWithDomainAndPath = WI.Cookie.parseSetCookieResponseHeader(`name=value; domain=example.com; path=/foo`);
InspectorTest.expectEqual(cookieWithDomainAndPath.url, "http://example.com/foo", "Cookie url should include domain and path.");
let secureCookie = WI.Cookie.parseSetCookieResponseHeader(`name=value; domain=example.com; secure`);
InspectorTest.expectEqual(secureCookie.url, "https://example.com", "Secure cookie url should begin with 'https'.");
return true;
}
});
suite.runTestCasesAndFinish();
}
</script>
</head>
<body onload="runTest()">
<p>Testing WI.Cookie.</p>
</body>
</html>