toolkit/components/extensions/test/mochitest/test_ext_cookies.html
author Carsten "Tomcat" Book <cbook@mozilla.com>
Fri, 01 Apr 2016 13:34:01 +0200
changeset 291380 1ffc15e906033f2b4940eb81f4f252e0175dccf1
parent 291366 0228e00caf591a590ac553d3ccf297c647b4c9c0
permissions -rw-r--r--
Backed out changeset 0228e00caf59 (bug 1255569) for m8 perma failures

<!DOCTYPE HTML>
<html>
<head>
  <title>WebExtension test</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

add_task(function* test_cookies() {
  function backgroundScript() {
    function assertExpected(expected, cookie) {
      for (let key of Object.keys(cookie)) {
        browser.test.assertTrue(key in expected, `found property ${key}`);
        browser.test.assertEq(expected[key], cookie[key], `property value for ${key} is correct`);
      }
      browser.test.assertEq(Object.keys(expected).length, Object.keys(cookie).length, "all expected properties found");
    }

    const TEST_URL = "http://example.org/";
    const TEST_SECURE_URL = "https://example.org/";
    const THE_FUTURE = Date.now() + 5 * 60;
    const TEST_PATH = "set_path";
    const TEST_URL_WITH_PATH = TEST_URL + TEST_PATH;
    const TEST_COOKIE_PATH = `/${TEST_PATH}`;
    const STORE_ID = "firefox-default";

    let expected = {
      name: "name1",
      value: "value1",
      domain: "example.org",
      hostOnly: true,
      path: "/",
      secure: false,
      httpOnly: false,
      session: false,
      expirationDate: THE_FUTURE,
      storeId: STORE_ID,
    };

    browser.cookies.set({url: TEST_URL, name: "name1", value: "value1", expirationDate: THE_FUTURE}).then(cookie => {
      assertExpected(expected, cookie);
      return browser.cookies.get({url: TEST_URL, name: "name1"});
    }).then(cookie => {
      assertExpected(expected, cookie);
      return browser.cookies.getAll({name: "name1"});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 1, "one cookie found for matching name");
      assertExpected(expected, cookies[0]);
      return browser.cookies.getAll({domain: "example.org"});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 1, "one cookie found for matching domain");
      assertExpected(expected, cookies[0]);
      return browser.cookies.getAll({domain: "example.net"});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 0, "no cookies found for non-matching domain");
      return browser.cookies.getAll({secure: false});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 1, "one non-secure cookie found");
      assertExpected(expected, cookies[0]);
      return browser.cookies.getAll({secure: true});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 0, "no secure cookies found");
      return browser.cookies.getAll({storeId: STORE_ID});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 1, "one cookie found for valid storeId");
      assertExpected(expected, cookies[0]);
      return browser.cookies.getAll({storeId: "invalid_id"});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 0, "no cookies found for invalid storeId");
      return browser.cookies.remove({url: TEST_URL, name: "name1"});
    }).then(details => {
      assertExpected({url: TEST_URL, name: "name1", storeId: STORE_ID}, details);
      return browser.cookies.get({url: TEST_URL, name: "name1"});
    }).then(cookie => {
      browser.test.assertEq(null, cookie, "removed cookie not found");
      return browser.cookies.getAllCookieStores();
    }).then(stores => {
      browser.test.assertEq(1, stores.length, "expected number of stores returned");
      browser.test.assertEq(STORE_ID, stores[0].id, "expected store id returned");
      browser.test.assertEq(0, stores[0].tabIds.length, "no tabs returned for store"); // Todo: Implement this.
      return browser.cookies.set({url: TEST_URL, name: "name2", domain: ".example.org", expirationDate: THE_FUTURE});
    }).then(cookie => {
      browser.test.assertEq(false, cookie.hostOnly, "cookie is not a hostOnly cookie");
      return browser.cookies.remove({url: TEST_URL, name: "name2"});
    }).then(details => {
      assertExpected({url: TEST_URL, name: "name2", storeId: STORE_ID}, details);
      // Create a session cookie.
      return browser.cookies.set({url: TEST_URL, name: "name1", value: "value1"});
    }).then(cookie => {
      browser.test.assertEq(true, cookie.session, "session cookie set");
      return browser.cookies.get({url: TEST_URL, name: "name1"});
    }).then(cookie => {
      browser.test.assertEq(true, cookie.session, "got session cookie");
      return browser.cookies.getAll({session: true});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 1, "one session cookie found");
      browser.test.assertEq(true, cookies[0].session, "found session cookie");
      return browser.cookies.getAll({session: false});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 0, "no non-session cookies found");
      return browser.cookies.remove({url: TEST_URL, name: "name1"});
    }).then(details => {
      assertExpected({url: TEST_URL, name: "name1", storeId: STORE_ID}, details);
      return browser.cookies.get({url: TEST_URL, name: "name1"});
    }).then(cookie => {
      browser.test.assertEq(null, cookie, "removed cookie not found");
      return browser.cookies.set({url: TEST_SECURE_URL, name: "name1", value: "value1", secure: true});
    }).then(cookie => {
      browser.test.assertEq(true, cookie.secure, "secure cookie set");
      return browser.cookies.get({url: TEST_SECURE_URL, name: "name1"});
    }).then(cookie => {
      browser.test.assertEq(true, cookie.session, "got secure cookie");
      return browser.cookies.getAll({secure: true});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 1, "one secure cookie found");
      browser.test.assertEq(true, cookies[0].secure, "found secure cookie");
      return browser.cookies.getAll({secure: false});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 0, "no non-secure cookies found");
      return browser.cookies.remove({url: TEST_SECURE_URL, name: "name1"});
    }).then(details => {
      assertExpected({url: TEST_SECURE_URL, name: "name1", storeId: STORE_ID}, details);
      return browser.cookies.get({url: TEST_SECURE_URL, name: "name1"});
    }).then(cookie => {
      browser.test.assertEq(null, cookie, "removed cookie not found");
      return browser.cookies.set({url: TEST_URL_WITH_PATH, path: TEST_COOKIE_PATH, name: "name1", value: "value1", expirationDate: THE_FUTURE});
    }).then(cookie => {
      browser.test.assertEq(TEST_COOKIE_PATH, cookie.path, "created cookie with path");
      return browser.cookies.get({url: TEST_URL_WITH_PATH, name: "name1"});
    }).then(cookie => {
      browser.test.assertEq(TEST_COOKIE_PATH, cookie.path, "got cookie with path");
      return browser.cookies.getAll({path: TEST_COOKIE_PATH});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 1, "one cookie with path found");
      browser.test.assertEq(TEST_COOKIE_PATH, cookies[0].path, "found cookie with path");
      return browser.cookies.get({url: TEST_URL + "invalid_path", name: "name1"});
    }).then(cookie => {
      browser.test.assertEq(null, cookie, "get with invalid path returns null");
      return browser.cookies.getAll({path: "/invalid_path"});
    }).then(cookies => {
      browser.test.assertEq(cookies.length, 0, "getAll with invalid path returns 0 cookies");
      return browser.cookies.remove({url: TEST_URL_WITH_PATH, name: "name1"});
    }).then(details => {
      assertExpected({url: TEST_URL_WITH_PATH, name: "name1", storeId: STORE_ID}, details);
      return browser.cookies.set({url: TEST_URL, name: "name1", value: "value1", httpOnly: true});
    }).then(cookie => {
      browser.test.assertEq(true, cookie.httpOnly, "httpOnly cookie set");
      return browser.cookies.set({url: TEST_URL, name: "name1", value: "value1", httpOnly: false});
    }).then(cookie => {
      browser.test.assertEq(false, cookie.httpOnly, "non-httpOnly cookie set");
      return browser.cookies.remove({url: TEST_URL, name: "name1"});
    }).then(details => {
      assertExpected({url: TEST_URL, name: "name1", storeId: STORE_ID}, details);
      return browser.cookies.set({url: TEST_URL});
    }).then(cookie => {
      browser.test.assertEq("", cookie.name, "default name set");
      browser.test.assertEq("", cookie.value, "default value set");
      browser.test.assertEq(true, cookie.session, "no expiry date created session cookie");
      browser.test.notifyPass("cookies");
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    background: `(${backgroundScript})()`,
    manifest: {
      permissions: ["cookies", "*://example.org/"],
    },
  });

  yield extension.startup();
  info("extension loaded");
  yield extension.awaitFinish("cookies");
  yield extension.unload();
  info("extension unloaded");
});

</script>

</body>
</html>