blob: a9f14f8212a412eca94c694baba1bdeb112282e1 [file] [log] [blame]
Hallvord Reiar M. Steene3ce86d2013-04-30 10:15:401<!doctype html>
2<html>
3 <head>
4 <title>XMLHttpRequest: responseXML document properties</title>
5 <script src="/resources/testharness.js"></script>
6 <script src="/resources/testharnessreport.js"></script>
7 </head>
8 <body>
9 <div id="log"></div>
10 <script>
jgrahamb2854612016-10-06 18:36:4211 var timePreXHR = Math.floor(new Date().getTime(new Date().getTime() - 3000) / 1000); // three seconds ago, in case there's clock drift
Hallvord Reiar M. Steene3ce86d2013-04-30 10:15:4012 var client = new XMLHttpRequest()
13 client.open("GET", "resources/well-formed.xml", false)
14 client.send(null)
Ehsan Akhgari915fcf82019-05-24 17:19:1515 var responseURLObject = new URL('resources/well-formed.xml', location.href);
16 var responseURL = responseURLObject.href
17 var responseDomain = responseURLObject.hostname
Hallvord Reiar M. Steend3d45612013-04-30 12:46:0818 var expected = {
Ehsan Akhgari915fcf82019-05-24 17:19:1519 domain:responseDomain,
Anne van Kesterene2d2cde2017-05-15 07:43:4820 URL:responseURL,
21 documentURI:responseURL,
22 baseURI:responseURL,
Hallvord Reiar M. Steend3d45612013-04-30 12:46:0823 referrer:'',
24 title:'',
Hallvord R. M. Steen9ac6d662013-05-14 15:33:0025 contentType:'application/xml',
Hallvord Reiar M. Steend3d45612013-04-30 12:46:0826 readyState:'complete',
27 location:null,
28 defaultView:null,
Boris Zbarskyb96d0ba2018-01-30 18:09:0729 body:null,
Hallvord Reiar M. Steend3d45612013-04-30 12:46:0830 doctype:null,
Ehsan Akhgari7f011272019-07-10 15:11:5931 all:HTMLAllCollection,
Hallvord Reiar M. Steend3d45612013-04-30 12:46:0832 cookie:''
Hallvord Reiar M. Steen4e96dac2013-07-03 12:42:1033 }
Hallvord Reiar M. Steend3d45612013-04-30 12:46:0834
35 for (var name in expected) {
36 runTest(name, expected[name])
Hallvord Reiar M. Steen4e96dac2013-07-03 12:42:1037 }
Hallvord Reiar M. Steend3d45612013-04-30 12:46:0838
39 function runTest(name, value){
40 test(function(){
Ehsan Akhgari7f011272019-07-10 15:11:5941 if (name == "all") {
Nayeem Rahman59d28c82021-02-02 11:12:5842 assert_equals(Object.getPrototypeOf(client.responseXML[name]), value.prototype)
Ehsan Akhgari7f011272019-07-10 15:11:5943 } else {
44 assert_equals(client.responseXML[name], value)
45 }
Hallvord Reiar M. Steend3d45612013-04-30 12:46:0846 }, name)
47 }
48
Adam Rice4cf1a962021-02-10 13:40:1049 // Parse a "lastModified" value and convert it to a Date.
50 // See https://html.spec.whatwg.org/multipage/dom.html#dom-document-lastmodified
51 function parseLastModified(value) {
52 const [undefined, month, day, year, hours, minutes, seconds] =
53 /^(\d\d)\/(\d\d)\/(\d+) (\d\d):(\d\d):(\d\d)$/.exec(value);
54 return new Date(year, month - 1, day, hours, minutes, seconds);
55 }
56
Anne van Kesterene2d2cde2017-05-15 07:43:4857 async_test(t => {
58 const client = new XMLHttpRequest();
59 client.open("GET", "resources/redirect.py?location=well-formed.xml");
60 client.send();
61 client.onload = t.step_func_done(() => {
62 assert_equals(client.responseXML.URL, responseURL);
63 assert_equals(client.responseXML.baseURI, responseURL);
64 });
65 }, "Test document URL properties after redirect");
66
67 async_test(t => {
68 const client = new XMLHttpRequest();
69 client.open("GET", "resources/redirect.py?location=base.xml");
70 client.send();
71 client.onload = t.step_func_done(() => {
72 const localResponseURL = new URL('resources/base.xml', location.href).href;
73 assert_equals(client.responseXML.URL, localResponseURL);
74 assert_equals(client.responseXML.baseURI, 'https://example.com/');
75 client.responseXML.documentElement.remove();
76 assert_equals(client.responseXML.baseURI, localResponseURL);
77 const newBase = document.createElement("base"),
78 newBaseURL = "https://elsewhere.example/";
79 newBase.href = "https://elsewhere.example/";
80 client.responseXML.appendChild(newBase);
81 assert_equals(client.responseXML.baseURI, newBaseURL);
82 newBase.remove();
83 document.head.appendChild(newBase);
84 assert_equals(client.responseXML.baseURI, localResponseURL);
85 newBase.remove();
86 });
87 }, "Test document URL properties of document with <base> after redirect");
88
Hallvord Reiar M. Steene3ce86d2013-04-30 10:15:4089 test(function() {
Adam Rice4cf1a962021-02-10 13:40:1090 var lastModified = Math.floor(parseLastModified(client.responseXML.lastModified).getTime() / 1000);
jgrahamb2854612016-10-06 18:36:4291 var now = Math.floor(new Date().getTime(new Date().getTime() + 3000) / 1000); // three seconds from now, in case there's clock drift
Thomas Wisniewskife9bcee2016-06-29 12:28:2292 assert_greater_than_equal(lastModified, timePreXHR);
93 assert_less_than_equal(lastModified, now);
94 }, 'lastModified set to time of response if no HTTP header provided')
95
96 test(function() {
97 var client2 = new XMLHttpRequest()
98 client2.open("GET", "resources/last-modified.py", false)
99 client2.send(null)
Adam Rice4cf1a962021-02-10 13:40:10100 assert_equals((new Date(client2.getResponseHeader('Last-Modified'))).getTime(), (parseLastModified(client2.responseXML.lastModified)).getTime())
Thomas Wisniewskife9bcee2016-06-29 12:28:22101 }, 'lastModified set to related HTTP header if provided')
Hallvord Reiar M. Steend3d45612013-04-30 12:46:08102
Hallvord Reiar M. Steene3ce86d2013-04-30 10:15:40103 test(function() {
104 client.responseXML.cookie = "thisshouldbeignored"
105 assert_equals(client.responseXML.cookie, "")
106 }, 'cookie (after setting it)')
Hallvord Reiar M. Steend3d45612013-04-30 12:46:08107
moz-wptsync-bot995f59b2018-03-13 19:13:04108 var objectProps = [
109 "styleSheets",
110 "implementation",
111 "images",
moz-wptsync-bot5c32dfb2018-03-13 19:13:05112 "forms",
113 "links",
moz-wptsync-bot995f59b2018-03-13 19:13:04114 ];
Hallvord Reiar M. Steend3d45612013-04-30 12:46:08115
moz-wptsync-bot995f59b2018-03-13 19:13:04116 for (let prop of objectProps) {
117 test(function() {
118 assert_equals(typeof(client.responseXML[prop]), "object")
119 }, prop + " should be an object")
120 }
Hallvord Reiar M. Steene3ce86d2013-04-30 10:15:40121 </script>
122 </body>
123</html>