summaryrefslogtreecommitdiffstats
path: root/devtools/shared/tests/xpcshell/test_fetch-file.js
blob: 8b2602a9c416b5c952927be5799db6b171aea118 (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
102
103
104
105
106
107
108
109
110
111
112
113
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Tests for DevToolsUtils.fetch on file:// URI's.

const { FileUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/FileUtils.sys.mjs"
);

const TEST_CONTENT = "aéd";

// The TEST_CONTENT encoded as UTF-8.
const UTF8_TEST_BUFFER = new Uint8Array([0x61, 0xc3, 0xa9, 0x64]);

// The TEST_CONTENT encoded as ISO 8859-1.
const ISO_8859_1_BUFFER = new Uint8Array([0x61, 0xe9, 0x64]);

/**
 * Tests that URLs with arrows pointing to an actual source are handled properly
 * (bug 808960). For example 'resource://gre/modules/XPIProvider.jsm ->
 * file://l10n.js' should load 'file://l10n.js'.
 */
add_task(async function test_arrow_urls() {
  const { path } = createTemporaryFile(".js");
  const url = "resource://gre/modules/XPIProvider.jsm -> file://" + path;

  await IOUtils.writeUTF8(path, TEST_CONTENT);
  const { content } = await DevToolsUtils.fetch(url);

  deepEqual(content, TEST_CONTENT, "The file contents were correctly read.");
});

/**
 * Tests that empty files are read correctly.
 */
add_task(async function test_empty() {
  const { path } = createTemporaryFile();
  const { content } = await DevToolsUtils.fetch("file://" + path);
  deepEqual(content, "", "The empty file was read correctly.");
});

/**
 * Tests that UTF-8 encoded files are correctly read.
 */
add_task(async function test_encoding_utf8() {
  const { path } = createTemporaryFile();
  await IOUtils.write(path, UTF8_TEST_BUFFER);

  const { content } = await DevToolsUtils.fetch(path);
  deepEqual(
    content,
    TEST_CONTENT,
    "The UTF-8 encoded file was correctly read."
  );
});

/**
 * Tests that ISO 8859-1 (Latin-1) encoded files are correctly read.
 */
add_task(async function test_encoding_iso_8859_1() {
  const { path } = createTemporaryFile();
  await IOUtils.write(path, ISO_8859_1_BUFFER);

  const { content } = await DevToolsUtils.fetch(path);
  deepEqual(
    content,
    TEST_CONTENT,
    "The ISO 8859-1 encoded file was correctly read."
  );
});

/**
 * Test that non-existent files are handled correctly.
 */
add_task(async function test_missing() {
  await DevToolsUtils.fetch("file:///file/not/found.right").then(
    result => {
      info(result);
      ok(false, "Fetch resolved unexpectedly when the file was not found.");
    },
    () => {
      ok(true, "Fetch rejected as expected because the file was not found.");
    }
  );
});

/**
 * Test that URLs without file:// scheme work.
 */
add_task(async function test_schemeless_files() {
  const { path } = createTemporaryFile();

  await IOUtils.writeUTF8(path, TEST_CONTENT);

  const { content } = await DevToolsUtils.fetch(path);
  deepEqual(content, TEST_CONTENT, "The content was correct.");
});

/**
 * Creates a temporary file that is removed after the test completes.
 */
function createTemporaryFile(extension) {
  const name = "test_fetch-file-" + Math.random() + (extension || "");
  const file = FileUtils.getFile("TmpD", [name]);
  file.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0755", 8));

  registerCleanupFunction(() => {
    file.remove(false);
  });

  return file;
}