tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CookieXPCShellUtils.sys.mjs (1768B)


      1 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* vim: set sts=2 sw=2 et tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 import { ExtensionTestUtils } from "resource://testing-common/ExtensionXPCShellUtils.sys.mjs";
      8 
      9 import { AddonTestUtils } from "resource://testing-common/AddonTestUtils.sys.mjs";
     10 
     11 export const CookieXPCShellUtils = {
     12  init(scope) {
     13    AddonTestUtils.maybeInit(scope);
     14    ExtensionTestUtils.init(scope);
     15  },
     16 
     17  createServer(args) {
     18    const server = AddonTestUtils.createHttpServer(args);
     19    server.registerPathHandler("/", (metadata, response) => {
     20      response.setStatusLine(metadata.httpVersion, 200, "OK");
     21      response.setHeader("Content-Type", "text/html", false);
     22 
     23      let body = "<body><h1>Hello world!</h1></body>";
     24      response.bodyOutputStream.write(body, body.length);
     25    });
     26    return server;
     27  },
     28 
     29  async loadContentPage(uri, options = {}) {
     30    return ExtensionTestUtils.loadContentPage(uri, options);
     31  },
     32 
     33  async getCookieStringFromDocument(uri, options = {}) {
     34    const contentPage = await this.loadContentPage(uri, options);
     35    const cookies = await contentPage.spawn(
     36      [],
     37      // eslint-disable-next-line no-undef
     38      () => content.document.cookie
     39    );
     40    await contentPage.close();
     41    return cookies;
     42  },
     43 
     44  async setCookieToDocument(uri, set, options = {}) {
     45    const contentPage = await this.loadContentPage(uri, options);
     46    await contentPage.spawn(
     47      [set],
     48      // eslint-disable-next-line no-undef
     49      cookies => (content.document.cookie = cookies)
     50    );
     51    await contentPage.close();
     52  },
     53 };