summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/browser/browser_document_builder_sjs.js
blob: fdf1691b7f2f009753de4ade0855b7c9ac86944c (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Checks that document-builder.sjs works as expected
add_task(async function assertHtmlParam() {
  const html = "<main><h1>I'm built different</h1></main>";
  const delay = 5000;

  const params = new URLSearchParams({
    delay,
    html,
  });
  params.append("headers", "x-header-1:a");
  params.append("headers", "x-header-2:b");

  const startTime = performance.now();
  const request = new Request(
    `https://example.com/document-builder.sjs?${params}`
  );
  info("Do a fetch request to document-builder.sjs");
  const response = await fetch(request);
  const duration = performance.now() - startTime;

  is(response.status, 200, "Response is a 200");
  Assert.greater(
    duration,
    delay,
    `The delay parameter works as expected (took ${duration}ms)`
  );

  const responseText = await response.text();
  is(responseText, html, "The response has the expected content");

  is(
    response.headers.get("content-type"),
    "text/html",
    "response has the expected content-type"
  );
  is(
    response.headers.get("x-header-1"),
    "a",
    "first header was set as expected"
  );
  is(
    response.headers.get("x-header-2"),
    "b",
    "second header was set as expected"
  );
});

add_task(async function assertFileParam() {
  const file = `browser/testing/mochitest/tests/browser/dummy.html`;
  const request = new Request(
    `https://example.com/document-builder.sjs?file=${file}`
  );

  info("Do a fetch request to document-builder.sjs with a `file` parameter");
  const response = await fetch(request);
  is(response.status, 200, "Response is a 200");
  is(
    response.headers.get("content-type"),
    "text/html",
    "response has the expected content-type"
  );

  const responseText = await response.text();
  const parser = new DOMParser();
  const doc = parser.parseFromString(responseText, "text/html");
  is(
    doc.body.innerHTML.trim(),
    "This is a dummy page",
    "The response has the file content"
  );
});