summaryrefslogtreecommitdiffstats
path: root/dom/fetch/tests/browser_default_credentialless_fetch.js
blob: 1c7e820d5fc7d2cb0ca1dd0e6c2a251644eaa9fc (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
/* Any copyright is dedicated to the Public Domain.
   https://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const RESOURCE_URL =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  ) + "store_header.sjs";

add_task(async function test_fetch_defaults_to_credentialless() {
  // Ensure cookie is set up:
  let expiry = Date.now() / 1000 + 24 * 60 * 60;
  Services.cookies.add(
    "example.com",
    "/",
    "foo",
    "bar",
    false,
    false,
    false,
    expiry,
    {},
    Ci.nsICookie.SAMESITE_NONE,
    Ci.nsICookie.SCHEME_HTTPS
  );

  // Explicitly send cookie header by using `same-origin` in the init dict, to
  // ensure cookies are stored correctly and can be sent.
  await fetch(RESOURCE_URL + "?checkheader", { credentials: "same-origin" });

  Assert.equal(
    await fetch(RESOURCE_URL + "?getstate").then(r => r.text()),
    "hasCookie",
    "Should have cookie when explicitly passing credentials info in 'checkheader' request."
  );

  // Check the default behaviour.
  await fetch(RESOURCE_URL + "?checkheader");
  Assert.equal(
    await fetch(RESOURCE_URL + "?getstate").then(r => r.text()),
    "noCookie",
    "Should not have cookie in the default case (no explicit credentials mode) for chrome privileged requests."
  );
});