browser/components/urlbar/tests/browser/searchSuggestionEngine.sjs
author Mark Banner <standard8@mozilla.com>
Mon, 21 Jan 2019 16:58:39 +0000
changeset 514727 306a66424aad636fc9ec9010dd605f72fdc47b94
parent 458031 browser/base/content/test/urlbar/searchSuggestionEngine.sjs@dce5f1c884ccf014611a57328223cffa6f61910c
child 514791 44be6c1eb581bdd91023174d70f5b49052904945
permissions -rw-r--r--
Bug 1514164 - Move urlbar tests to the new urlbar directory and split out tests that already pass with QuantumBar. r=mak This moves the tests to browser/components/urlbar and sets them up for running both against QuantumBar as well as the legacy address bar. Differential Revision: https://phabricator.services.mozilla.com/D16996

/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

let gTimer;

function handleRequest(req, resp) {
  // Parse the query params.  If the params aren't in the form "foo=bar", then
  // treat the entire query string as a search string.
  let params = req.queryString.split("&").reduce((memo, pair) => {
    let [key, val] = pair.split("=");
    if (!val) {
      // This part isn't in the form "foo=bar".  Treat it as the search string
      // (the "query").
      val = key;
      key = "query";
    }
    memo[decode(key)] = decode(val);
    return memo;
  }, {});

  let timeout = parseInt(params["timeout"]);
  if (timeout) {
    // Write the response after a timeout.
    resp.processAsync();
    gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    gTimer.init(() => {
      writeResponse(params, resp);
      resp.finish();
    }, timeout, Ci.nsITimer.TYPE_ONE_SHOT);
    return;
  }

  writeResponse(params, resp);
}

function writeResponse(params, resp) {
  // Echo back the search string with "foo" and "bar" appended.
  let suffixes = ["foo", "bar"];
  let data = [params["query"], suffixes.map(s => params["query"] + s)];
  resp.setHeader("Content-Type", "application/json", false);
  resp.write(JSON.stringify(data));
}

function decode(str) {
  return decodeURIComponent(str.replace(/\+/g, encodeURIComponent(" ")));
}