Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/netwerk/test/unit/   (Browser von der Mozilla Stiftung Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 4 kB image not shown  

Quelle  test_bug248970_cookie.js   Sprache: JAVA

 
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


"use strict";

const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);

var httpserver;

function inChildProcess() {
  return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
}
function makeChan(path) {
  return NetUtil.newChannel({
    uri: "http://localhost:" + httpserver.identity.primaryPort + "/" + path,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
}

function setup_chan(path, isPrivate, callback) {
  var chan = makeChan(path);
  chan.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(isPrivate);
  chan.asyncOpen(new ChannelListener(callback));
}

function set_cookie(value, callback) {
  return setup_chan("set?cookie=" + value, false, callback);
}

function set_private_cookie(value, callback) {
  return setup_chan("set?cookie=" + value, true, callback);
}

function check_cookie_presence(value, isPrivate, expected, callback) {
  setup_chan(
    "present?cookie=" + value.replace("=""|"),
    isPrivate,
    function (req) {
      req.QueryInterface(Ci.nsIHttpChannel);
      Assert.equal(req.responseStatus, expected ? 200 : 404);
      callback(req);
    }
  );
}

function presentHandler(metadata, response) {
  var present = false;
  var match = /cookie=([^&]*)/.exec(metadata.queryString);
  if (match) {
    try {
      present = metadata
        .getHeader("Cookie")
        .includes(match[1].replace("|""="));
    } catch (x) {}
  }
  response.setStatusLine("1.0", present ? 200 : 404, "");
}

function setHandler(metadata, response) {
  response.setStatusLine("1.0", 200, "Cookie set");
  var match = /cookie=([^&]*)/.exec(metadata.queryString);
  if (match) {
    response.setHeader("Set-Cookie", match[1]);
  }
}

function run_test() {
  // Allow all cookies if the pref service is available in this process.
  if (!inChildProcess()) {
    Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
    Services.prefs.setBoolPref(
      "network.cookieJarSettings.unblocked_for_testing",
      true
    );
  }

  httpserver = new HttpServer();
  httpserver.registerPathHandler("/set", setHandler);
  httpserver.registerPathHandler("/present", presentHandler);
  httpserver.start(-1);

  do_test_pending();

  function check_cookie(req) {
    req.QueryInterface(Ci.nsIHttpChannel);
    Assert.equal(req.responseStatus, 200);
    try {
      Assert.ok(
        req.getResponseHeader("Set-Cookie") != "",
        "expected a Set-Cookie header"
      );
    } catch (x) {
      do_throw("missing Set-Cookie header");
    }

    runNextTest();
  }

  let tests = [];

  function runNextTest() {
    executeSoon(tests.shift());
  }

  tests.push(function () {
    set_cookie("C1=V1", check_cookie);
  });
  tests.push(function () {
    set_private_cookie("C2=V2", check_cookie);
  });
  tests.push(function () {
    // Check that the first cookie is present in a non-private request
    check_cookie_presence("C1=V1"falsetrue, runNextTest);
  });
  tests.push(function () {
    // Check that the second cookie is present in a private request
    check_cookie_presence("C2=V2"truetrue, runNextTest);
  });
  tests.push(function () {
    // Check that the first cookie is not present in a private request
    check_cookie_presence("C1=V1"truefalse, runNextTest);
  });
  tests.push(function () {
    // Check that the second cookie is not present in a non-private request
    check_cookie_presence("C2=V2"falsefalse, runNextTest);
  });

  // The following test only works in a non-e10s situation at the moment,
  // since the notification needs to run in the parent process but there is
  // no existing mechanism to make that happen.
  if (!inChildProcess()) {
    tests.push(function () {
      // Simulate all private browsing instances being closed
      Services.obs.notifyObservers(null"last-pb-context-exited");
      // Check that all private cookies are now unavailable in new private requests
      check_cookie_presence("C2=V2"truefalse, runNextTest);
    });
  }

  tests.push(function () {
    httpserver.stop(do_test_finished);
  });

  runNextTest();
}

Messung V0.5
C=92 H=100 G=95

¤ Dauer der Verarbeitung: 0.17 Sekunden  (vorverarbeitet)  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.