summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/beacon/beacon-preflight-handler.sjs
blob: c675049225e15fde48302962bd4e07085f38538b (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
function handleRequest(request, response) {
  response.setHeader("Cache-Control", "no-cache, must-revalidate", false);

  if (request.queryString === "verify") {
    var preflightState = getState("preflight");
    response.write(preflightState === "done" ? "green" : "red");
    return;
  }

  var originHeader = request.getHeader("origin");
  response.setHeader("Access-Control-Allow-Headers", "content-type", false);
  response.setHeader("Access-Control-Allow-Methods", "POST, GET", false);
  response.setHeader("Access-Control-Allow-Origin", originHeader, false);
  response.setHeader("Access-Control-Allow-Credentials", "true", false);

  if (request.queryString === "beacon") {
    if (request.method == "OPTIONS") {
      setState("preflight", "done");
      response.setStatusLine(null, 200, "OK");
      return;
    }
    response.setStatusLine(null, 200, "OK");
    response.write("DONE");
    return;
  }

  if (request.queryString === "fail") {
    if (request.method == "OPTIONS") {
      setState("preflight", "done");
      response.setStatusLine(null, 400, "Bad Request");
      return;
    }
    setState("preflight", "oops");
    response.setStatusLine(null, 200, "OK");
    response.write("DONE");
  }
}