summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/network/head.js
blob: 2910f1c080127a0e05ba7918103062b52a751944 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/remote/cdp/test/browser/head.js",
  this
);

function assertCookie(cookie, expected = {}) {
  const {
    name = "",
    value = "",
    domain = "example.org",
    path = "/",
    expires = -1,
    size = name.length + value.length,
    httpOnly = false,
    secure = false,
    session = true,
    sameSite,
  } = expected;

  const expectedCookie = {
    name,
    value,
    domain,
    path,
    expires,
    size,
    httpOnly,
    secure,
    session,
  };

  if (sameSite) {
    expectedCookie.sameSite = sameSite;
  }

  Assert.deepEqual(cookie, expectedCookie);
}

function assertEventOrder(first, second, options = {}) {
  const { ignoreTimestamps = false } = options;

  const firstDescription = getDescriptionForEvent(first);
  const secondDescription = getDescriptionForEvent(second);

  Assert.less(
    first.index,
    second.index,
    `${firstDescription} received before ${secondDescription})`
  );

  if (!ignoreTimestamps) {
    Assert.lessOrEqual(
      first.payload.timestamp,
      second.payload.timestamp,
      `Timestamp of ${firstDescription}) is earlier than ${secondDescription})`
    );
  }
}

function filterEventsByType(history, type) {
  return history.filter(event => event.payload.type == type);
}

function getCookies() {
  return Services.cookies.cookies.map(cookie => {
    const data = {
      name: cookie.name,
      value: cookie.value,
      domain: cookie.host,
      path: cookie.path,
      expires: cookie.isSession ? -1 : cookie.expiry,
      // The size is the combined length of both the cookie name and value
      size: cookie.name.length + cookie.value.length,
      httpOnly: cookie.isHttpOnly,
      secure: cookie.isSecure,
      session: cookie.isSession,
    };

    if (cookie.sameSite) {
      const sameSiteMap = new Map([
        [Ci.nsICookie.SAMESITE_LAX, "Lax"],
        [Ci.nsICookie.SAMESITE_STRICT, "Strict"],
      ]);

      data.sameSite = sameSiteMap.get(cookie.sameSite);
    }

    return data;
  });
}

function getDescriptionForEvent(event) {
  const { eventName, payload } = event;

  return `${eventName}(${payload.type || payload.name || payload.frameId})`;
}