| <!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.rawHeader, header, `cookie.rawHeader 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.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, |
| }); |
| |
| test(`name=value; path=/foo`, { |
| name: "name", |
| value: "value", |
| expires: null, |
| maxAge: null, |
| path: "/foo", |
| domain: null, |
| secure: false, |
| httpOnly: false, |
| }); |
| |
| test(`name=value; domain=example.com`, { |
| name: "name", |
| value: "value", |
| expires: null, |
| maxAge: null, |
| path: null, |
| domain: "example.com", |
| secure: false, |
| httpOnly: false, |
| }); |
| |
| test(`name=value; secure`, { |
| name: "name", |
| value: "value", |
| expires: null, |
| maxAge: null, |
| path: null, |
| domain: null, |
| secure: true, |
| httpOnly: false, |
| }); |
| |
| test(`name=value; Secure`, { |
| name: "name", |
| value: "value", |
| expires: null, |
| maxAge: null, |
| path: null, |
| domain: null, |
| secure: true, |
| httpOnly: false, |
| }); |
| |
| test(`name=value; HttpOnly`, { |
| name: "name", |
| value: "value", |
| expires: null, |
| maxAge: null, |
| path: null, |
| domain: null, |
| secure: false, |
| httpOnly: true, |
| }); |
| |
| 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, |
| }); |
| |
| 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, |
| }); |
| |
| test(`name=value; Unknown; path=/one/two`, { |
| name: "name", |
| value: "value", |
| expires: null, |
| maxAge: null, |
| path: "/one/two", |
| domain: null, |
| secure: false, |
| httpOnly: false, |
| }); |
| |
| 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, |
| }); |
| |
| 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, |
| }); |
| |
| // 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, |
| }); |
| |
| return true; |
| } |
| }); |
| |
| suite.runTestCasesAndFinish(); |
| } |
| </script> |
| </head> |
| <body onload="runTest()"> |
| <p>Testing WI.Cookie.</p> |
| </body> |
| </html> |