summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/xhr/responsexml-document-properties.htm
blob: 1d2811bec7dab88aa4408f6bd4e1ffe0e384afd1 (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
<!doctype html>
<html>
  <head>
    <title>XMLHttpRequest: responseXML document properties</title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="../html/dom/documents/resource-metadata-management/support/document-lastModified-utils.js"></script>
  </head>
  <body>
    <div id="log"></div>
    <script>
      var client = new XMLHttpRequest()
      client.open("GET", "resources/well-formed.xml", false)
      client.send(null)
      var responseURLObject = new URL('resources/well-formed.xml', location.href);
      var responseURL = responseURLObject.href
      var responseDomain = responseURLObject.hostname
      var expected = {
        domain:responseDomain,
        URL:responseURL,
        documentURI:responseURL,
        baseURI:responseURL,
        referrer:'',
        title:'',
        contentType:'application/xml',
        readyState:'complete',
        location:null,
        defaultView:null,
        body:null,
        doctype:null,
        all:HTMLAllCollection,
        cookie:''
      }

      for (var name in expected) {
        runTest(name, expected[name])
      }

      function runTest(name, value){
        test(function(){
          if (name == "all") {
            assert_equals(Object.getPrototypeOf(client.responseXML[name]), value.prototype)
          } else {
            assert_equals(client.responseXML[name], value)
          }
        }, name)
      }

      // Parse a "lastModified" value and convert it to a Date.
      // See https://html.spec.whatwg.org/multipage/dom.html#dom-document-lastmodified
      function parseLastModified(value) {
        const [undefined, month, day, year, hours, minutes, seconds] =
          /^(\d\d)\/(\d\d)\/(\d+) (\d\d):(\d\d):(\d\d)$/.exec(value);
        return new Date(year, month - 1, day, hours, minutes, seconds);
      }

      async_test(t => {
        const client = new XMLHttpRequest();
        client.open("GET", "resources/redirect.py?location=well-formed.xml");
        client.send();
        client.onload = t.step_func_done(() => {
          assert_equals(client.responseXML.URL, responseURL);
          assert_equals(client.responseXML.baseURI, responseURL);
        });
      }, "Test document URL properties after redirect");

      async_test(t => {
        const client = new XMLHttpRequest();
        client.open("GET", "resources/redirect.py?location=base.xml");
        client.send();
        client.onload = t.step_func_done(() => {
          const localResponseURL = new URL('resources/base.xml', location.href).href;
          assert_equals(client.responseXML.URL, localResponseURL);
          assert_equals(client.responseXML.baseURI, 'https://example.com/');
          client.responseXML.documentElement.remove();
          assert_equals(client.responseXML.baseURI, localResponseURL);
          const newBase = document.createElement("base"),
                newBaseURL = "https://elsewhere.example/";
          newBase.href = "https://elsewhere.example/";
          client.responseXML.appendChild(newBase);
          assert_equals(client.responseXML.baseURI, newBaseURL);
          newBase.remove();
          document.head.appendChild(newBase);
          assert_equals(client.responseXML.baseURI, localResponseURL);
          newBase.remove();
        });
      }, "Test document URL properties of document with <base> after redirect");

      test(function() {
        assert_document_lastmodified_string_approximately_now(client.responseXML.lastModified);
      }, 'lastModified set to time of response if no HTTP header provided')

      test(function() {
        var client2 = new XMLHttpRequest()
        client2.open("GET", "resources/last-modified.py", false)
        client2.send(null)
        assert_equals((new Date(client2.getResponseHeader('Last-Modified'))).getTime(), (parseLastModified(client2.responseXML.lastModified)).getTime())
      }, 'lastModified set to related HTTP header if provided')

      test(function() {
        client.responseXML.cookie = "thisshouldbeignored"
        assert_equals(client.responseXML.cookie, "")
      }, 'cookie (after setting it)')

      var objectProps = [
        "styleSheets",
        "implementation",
        "images",
        "forms",
        "links",
      ];

      for (let prop of objectProps) {
        test(function() {
          assert_equals(typeof(client.responseXML[prop]), "object")
        }, prop + " should be an object")
      }
    </script>
  </body>
</html>