summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_cookies.js
blob: f514efc777e0a92ed857a15d5b8673cf84d3b39a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use strict";

function promiseSetCookie(cookie) {
  info(`Set-Cookie: ${cookie}`);
  return Promise.all([
    waitForCookieChanged(),
    SpecialPowers.spawn(
      gBrowser.selectedBrowser,
      [cookie],
      passedCookie => (content.document.cookie = passedCookie)
    ),
  ]);
}

function waitForCookieChanged() {
  return new Promise(resolve => {
    Services.obs.addObserver(function observer(subj, topic, data) {
      Services.obs.removeObserver(observer, topic);
      resolve();
    }, "session-cookie-changed");
  });
}

function cookieExists(host, name, value) {
  let {
    cookies: [c],
  } = JSON.parse(ss.getBrowserState());
  return c && c.host == host && c.name == name && c.value == value;
}

// Setup and cleanup.
add_task(async function test_setup() {
  registerCleanupFunction(() => {
    Services.cookies.removeAll();
  });
});

// Test session cookie storage.
add_task(async function test_run() {
  Services.cookies.removeAll();

  // Add a new tab for testing.
  gBrowser.selectedTab = BrowserTestUtils.addTab(
    gBrowser,
    "http://example.com/"
  );
  await promiseBrowserLoaded(gBrowser.selectedBrowser);

  // Add a session cookie.
  await promiseSetCookie("foo=bar");
  ok(cookieExists("example.com", "foo", "bar"), "cookie was added");

  // Modify a session cookie.
  await promiseSetCookie("foo=baz");
  ok(cookieExists("example.com", "foo", "baz"), "cookie was modified");

  // Turn the session cookie into a normal one.
  let expiry = new Date();
  expiry.setDate(expiry.getDate() + 2);
  await promiseSetCookie(`foo=baz; Expires=${expiry.toUTCString()}`);
  ok(!cookieExists("example.com", "foo", "baz"), "no longer a session cookie");

  // Turn it back into a session cookie.
  await promiseSetCookie("foo=bar");
  ok(cookieExists("example.com", "foo", "bar"), "again a session cookie");

  // Remove the session cookie.
  await promiseSetCookie("foo=; Expires=Thu, 01 Jan 1970 00:00:00 GMT");
  ok(!cookieExists("example.com", "foo", ""), "cookie was removed");

  // Add a session cookie.
  await promiseSetCookie("foo=bar");
  ok(cookieExists("example.com", "foo", "bar"), "cookie was added");

  // Clear all session cookies.
  Services.cookies.removeAll();
  ok(!cookieExists("example.com", "foo", "bar"), "cookies were cleared");

  // Cleanup.
  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});