summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/XMLHttpRequest/data-uri.htm
blob: 8c2970583e8ab6105b176c2f223a75ed587bd384 (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
<!doctype html>
<meta charset=utf-8>
<title>XMLHttpRequest: data uri</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
    <link rel="help" href="https://xhr.spec.whatwg.org/#data:-urls-and-http" data-tested-assertations="following::ul/li[1] following::ul/li[2] following::ul/li[4]" />
    <link rel="help" href="https://xhr.spec.whatwg.org/#the-send()-method" data-tested-assertations="following::ul/li[10]/dl/dt[2]" />
<div id="log"></div>

<script>
  function do_test(method, uri, charset, testNamePostfix) {
    if (typeof charset === 'undefined' || charset === null) charset = 'text/plain';
    var test = async_test("XHR method " + method + " with charset " + charset+(testNamePostfix||''));
    test.step(function() {
      var client = new XMLHttpRequest();
      client.onreadystatechange = test.step_func(function () {
        if (client.readyState !== 4) {
          return;
        }

        // Note: fetching a data URL with a non-GET method returns a network
        // error per <http://fetch.spec.whatwg.org/#basic-fetch>.
        if (method.toUpperCase() !== 'GET') {
          assert_equals(client.status, 0);
          assert_equals(client.responseText, '');
          assert_equals(client.statusText, '');
          test.done();
          return;
        }

        assert_equals(client.responseText, "Hello, World!");
        assert_equals(client.status, 200);
        assert_equals(client.getResponseHeader('Content-Type'), charset);
        var allHeaders = client.getAllResponseHeaders();
        assert_regexp_match(allHeaders, /content\-type\:/i, 'getAllResponseHeaders() includes Content-Type');
        assert_false(/content\-length\:/i.test(allHeaders), 'getAllResponseHeaders() must not include Content-Length');
        test.done();
      });
      client.open(method, uri);
      client.send(null);
    });
  }
  do_test('GET', "data:text/plain,Hello, World!");
  do_test('GET', "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==", undefined, " (base64)");
  do_test('GET', "data:text/html,Hello, World!", 'text/html');
  do_test('GET', "data:text/html;charset=UTF-8,Hello, World!", 'text/html;charset=UTF-8');
  do_test('GET', "data:image/png,Hello, World!", 'image/png');
  do_test('POST', "data:text/plain,Hello, World!");
  do_test('PUT', "data:text/plain,Hello, World!");
  do_test('DELETE', "data:text/plain,Hello, World!");
  do_test('HEAD', "data:text/plain,Hello, World!");
  do_test('UNICORN', "data:text/plain,Hello, World!");
</script>