summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/basic/scheme-data.js
blob: 61efd2b2d72c4f69c7eb78c5e7a49da39c323864 (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
if (this.document === undefined) {
  importScripts("/resources/testharness.js");
  importScripts("../resources/utils.js");
}

<<<<<<< b45946c54935c6113fefb5199b3fe0e4bf087777
function checkFetchResponse(url, data, mime, fetchMode, method) {
  var cut = (url.length >= 40) ? "[...]" : "";
  desc = "Fetching " + (method ? "[" + method + "] " : "") + url.substring(0, 40) + cut + " is OK";
  var init = {"method": method || "GET"};
  if (fetchMode) {
    init.mode = fetchMode;
    desc += " (" + fetchMode + ")";
  }
  promise_test(function(test) {
    return fetch(url, init).then(function(resp) {
      assert_equals(resp.status, 200, "HTTP status is 200");
      assert_equals(resp.statusText, "OK", "HTTP statusText is OK");
      assert_equals(resp.type, "basic", "response type is basic");
      assert_equals(resp.headers.get("Content-Type"), mime, "Content-Type is " + resp.headers.get("Content-Type"));
     return resp.text();
    }).then(function(body) {
        assert_equals(body, data, "Response's body is correct");
    });
  }, desc);
}

checkFetchResponse("data:,response%27s%20body", "response's body", "text/plain;charset=US-ASCII");
checkFetchResponse("data:,response%27s%20body", "response's body", "text/plain;charset=US-ASCII", "same-origin");
checkFetchResponse("data:,response%27s%20body", "response's body", "text/plain;charset=US-ASCII", "cors");
checkFetchResponse("data:text/plain;base64,cmVzcG9uc2UncyBib2R5", "response's body", "text/plain");
checkFetchResponse("data:image/png;base64,cmVzcG9uc2UncyBib2R5",
                   "response's body",
                   "image/png");
checkFetchResponse("data:,response%27s%20body", "response's body", "text/plain;charset=US-ASCII", null, "POST");
checkFetchResponse("data:,response%27s%20body", "response's body", "text/plain;charset=US-ASCII", null, "HEAD");

function checkKoUrl(url, method, desc) {
  var cut = (url.length >= 40) ? "[...]" : "";
  desc = "Fetching [" + method + "] " + url.substring(0, 45) + cut + " is KO"
  promise_test(function(test) {
    return promise_rejects(test, new TypeError(), fetch(url, {"method": method}));
  }, desc);
}

checkKoUrl("data:notAdataUrl.com", "GET");

done();