toolkit/components/search/tests/xpcshell/data/searchSuggestions.sjs
author Marco Bonardo <mbonardo@mozilla.com>
Sat, 08 Dec 2018 17:32:27 +0000
changeset 508945 a90f08ba09140b0990750ec622931ca8bd0fba0d
parent 501413 a5dfb5153b78bc8fd4838c885c92318fe45a3116
child 508946 66fd4c2ab08185187eda60af2f32f82c3da325bd
permissions -rw-r--r--
Bug 1510281 - Backed out changeset 093960021b8b because we want to use a different approach. r=mkaply, a=RyanVM Differential Revision: https://phabricator.services.mozilla.com/D13328

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

Cu.import("resource://gre/modules/Timer.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");

/**
 * Provide search suggestions in the OpenSearch JSON format.
 */

function handleRequest(request, response) {
  // Get the query parameters from the query string.
  let query = parseQueryString(request.queryString);

  function writeSuggestions(query, completions = []) {
    let result = [query, completions];
    response.write(JSON.stringify(result));
    return result;
  }

  response.setStatusLine(request.httpVersion, 200, "OK");

  let q = request.method == "GET" ? query.q : undefined;
  if (q == "no remote" || q == "no results") {
    writeSuggestions(q);
  } else if (q == "Query Mismatch") {
    writeSuggestions("This is an incorrect query string", ["some result"]);
  } else if (q == "Query Case Mismatch") {
    writeSuggestions(q.toUpperCase(), [q]);
  } else if (q == "") {
    writeSuggestions("", ["The server should never be sent an empty query"]);
  } else if (q && q.startsWith("mo")) {
    writeSuggestions(q, ["Mozilla", "modern", "mom"]);
  } else if (q && q.startsWith("I ❤️")) {
    writeSuggestions(q, ["I ❤️ Mozilla"]);
  } else if (q && q.startsWith("letter ")) {
    let letters = [];
    for (let charCode = "A".charCodeAt(); charCode <= "Z".charCodeAt(); charCode++) {
      letters.push("letter " + String.fromCharCode(charCode));
    }
    writeSuggestions(q, letters);
  } else if (q && q.startsWith("HTTP ")) {
    response.setStatusLine(request.httpVersion, q.replace("HTTP ", ""), q);
    writeSuggestions(q, [q]);
  } else if (q && q.startsWith("delay")) {
    // Delay the response by 200 milliseconds (less than the timeout but hopefully enough to abort
    // before completion).
    response.processAsync();
    writeSuggestions(q, [q]);
    setTimeout(() => response.finish(), 200);
  } else if (q && q.startsWith("slow ")) {
    // Delay the response by 10 seconds so the client timeout is reached.
    response.processAsync();
    writeSuggestions(q, [q]);
    setTimeout(() => response.finish(), 10000);
  } else if (request.method == "POST") {
    // This includes headers, not just the body
    let requestText = NetUtil.readInputStreamToString(request.bodyInputStream,
                                                      request.bodyInputStream.available());
    // Only use the last line which contains the encoded params
    let requestLines = requestText.split("\n");
    let postParams = parseQueryString(requestLines[requestLines.length - 1]);
    writeSuggestions(postParams.q, ["Mozilla", "modern", "mom"]);
  } else {
    response.setStatusLine(request.httpVersion, 404, "Not Found");
  }
}

function parseQueryString(queryString) {
  let query = {};
  queryString.split('&').forEach(function (val) {
    let [name, value] = val.split('=');
    query[name] = unescape(value).replace(/[+]/g, " ");
  });
  return query;
}