summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/fetch/test_fetch_basic.js
blob: 27343d8662c147b1265db81709c7580600b20a88 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
function testAboutURL() {
  var p1 = fetch("about:blank").then(
    function (res) {
      ok(false, "about:blank should fail");
    },
    function (e) {
      ok(e instanceof TypeError, "about:blank should fail");
    }
  );

  var p2 = fetch("about:config").then(
    function (res) {
      ok(false, "about:config should fail");
    },
    function (e) {
      ok(e instanceof TypeError, "about:config should fail");
    }
  );

  return Promise.all([p1, p2]);
}

function testDataURL() {
  return Promise.all(
    [
      [
        "data:text/plain;charset=UTF-8,Hello",
        "text/plain;charset=UTF-8",
        "Hello",
      ],
      [
        "data:text/plain;charset=utf-8;base64,SGVsbG8=",
        "text/plain;charset=utf-8",
        "Hello",
      ],
      [
        "data:text/xml,%3Cres%3Ehello%3C/res%3E%0A",
        "text/xml",
        "<res>hello</res>\n",
      ],
      ["data:text/plain,hello%20pass%0A", "text/plain", "hello pass\n"],
      ["data:,foo", "text/plain;charset=US-ASCII", "foo"],
      ["data:text/plain;base64,Zm9v", "text/plain", "foo"],
      ["data:text/plain,foo#bar", "text/plain", "foo"],
      ["data:text/plain,foo%23bar", "text/plain", "foo#bar"],
    ].map(test => {
      var uri = test[0],
        contentType = test[1],
        expectedBody = test[2];
      return fetch(uri).then(res => {
        ok(true, "Data URL fetch should resolve");
        if (res.type == "error") {
          ok(false, "Data URL fetch should not fail.");
          return Promise.reject();
        }
        ok(res instanceof Response, "Fetch should resolve to a Response");
        is(res.status, 200, "Data URL status should be 200");
        is(res.statusText, "OK", "Data URL statusText should be OK");
        ok(
          res.headers.has("content-type"),
          "Headers must have Content-Type header"
        );
        is(
          res.headers.get("content-type"),
          contentType,
          "Content-Type header should match specified value"
        );
        return res
          .text()
          .then(body => is(body, expectedBody, "Data URL Body should match"));
      });
    })
  );
}

function testSameOriginBlobURL() {
  var blob = new Blob(["english ", "sentence"], { type: "text/plain" });
  var url = URL.createObjectURL(blob);
  return fetch(url).then(function (res) {
    URL.revokeObjectURL(url);
    ok(true, "Blob URL fetch should resolve");
    if (res.type == "error") {
      ok(false, "Blob URL fetch should not fail.");
      return Promise.reject();
    }
    ok(res instanceof Response, "Fetch should resolve to a Response");
    is(res.status, 200, "Blob fetch status should be 200");
    is(res.statusText, "OK", "Blob fetch statusText should be OK");
    ok(
      res.headers.has("content-type"),
      "Headers must have Content-Type header"
    );
    is(
      res.headers.get("content-type"),
      blob.type,
      "Content-Type header should match specified value"
    );
    ok(
      res.headers.has("content-length"),
      "Headers must have Content-Length header"
    );
    is(
      parseInt(res.headers.get("content-length")),
      16,
      "Content-Length should match Blob's size"
    );
    return res.text().then(function (body) {
      is(body, "english sentence", "Blob fetch body should match");
    });
  });
}

function testNonGetBlobURL() {
  var blob = new Blob(["english ", "sentence"], { type: "text/plain" });
  var url = URL.createObjectURL(blob);
  return Promise.all(
    ["HEAD", "POST", "PUT", "DELETE"].map(method => {
      var req = new Request(url, { method });
      return fetch(req)
        .then(function (res) {
          ok(false, "Blob URL with non-GET request should not succeed");
        })
        .catch(function (e) {
          ok(
            e instanceof TypeError,
            "Blob URL with non-GET request should get a TypeError"
          );
        });
    })
  ).then(function () {
    URL.revokeObjectURL(url);
  });
}

function testMozErrors() {
  // mozErrors shouldn't be available to content and be ignored.
  return fetch("http://localhost:4/should/fail", { mozErrors: true })
    .then(res => {
      ok(false, "Request should not succeed");
    })
    .catch(err => {
      ok(err instanceof TypeError);
    });
}

function testRequestMozErrors() {
  // mozErrors shouldn't be available to content and be ignored.
  const r = new Request("http://localhost:4/should/fail", { mozErrors: true });
  return fetch(r)
    .then(res => {
      ok(false, "Request should not succeed");
    })
    .catch(err => {
      ok(err instanceof TypeError);
    });
}

function testViewSourceURL() {
  var p2 = fetch("view-source:/").then(
    function (res) {
      ok(false, "view-source: URL should fail");
    },
    function (e) {
      ok(e instanceof TypeError, "view-source: URL should fail");
    }
  );
}

function runTest() {
  return Promise.resolve()
    .then(testAboutURL)
    .then(testDataURL)
    .then(testSameOriginBlobURL)
    .then(testNonGetBlobURL)
    .then(testMozErrors)
    .then(testRequestMozErrors)
    .then(testViewSourceURL);
  // Put more promise based tests here.
}