summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser/trendingSuggestionEngine.sjs
blob: 358d2a60770a6be0285dfdd42e206c6cde184d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* 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;
  }, {});

  writeResponse(params, resp);
}

function writeResponse(params, resp) {
  // Echoes back 15 results, query, query0, query1, query2 etc.
  let query = params.query || "";
  let suffixes = [...Array(15).keys()].map(s => query + s);
  // If we have a query, echo it back (to help test deduplication)
  if (query) {
    suffixes.unshift(query);
  }
  let data = [query, suffixes];

  if (params?.richsuggestions) {
    data.push([]);
    data.push({
      "google:suggestdetail": data[1].map(() => ({
        a: "Extended title",
        dc: "#FFFFFF",
        i: "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==",
        t: "Title",
      })),
    });
  }
  resp.setHeader("Content-Type", "application/json", false);

  let json = JSON.stringify(data);
  let utf8 = String.fromCharCode(...new TextEncoder().encode(json));
  resp.write(utf8);
}

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