tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_XHRDocURI.html (14764B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=459470
      5 -->
      6 <head>
      7  <title>XMLHttpRequest return document URIs</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     10  <base href="http://example.org/">
     11 </head>
     12 <body onload="startTest();">
     13 <a target="_blank"
     14   href="https://bugzilla.mozilla.org/show_bug.cgi?id=459470">Mozilla Bug 459470</a><br />
     15 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=859095">Mozilla Bug 859095</a>
     16 
     17 <p id="display">
     18 <iframe id=loader></iframe>
     19 </p>
     20 <div id="content" style="display: none">
     21 
     22 </div>
     23 <pre id="test">
     24 <script class="testbody" type="application/javascript">
     25 
     26 SimpleTest.waitForExplicitFinish();
     27 var gen;
     28 
     29 function startTest() {
     30  // The test uses history API, so don't do anything before load event has been
     31  // handled.
     32  SimpleTest.executeSoon(function() {
     33    gen = runTest();
     34    gen.next();
     35  });
     36 }
     37 
     38 function testXMLDocURI(aDoc, aExpects) {
     39  is(aDoc.documentURI, aExpects.documentURI, "wrong url");
     40  is(aDoc.baseURI, aExpects.baseURI, "wrong base");
     41 }
     42 
     43 function testChromeXMLDocURI(aDoc, aExpects) {
     44  is(aDoc.documentURI, aExpects.documentURI, "wrong url");
     45  is(aDoc.documentURIObject.spec, aExpects.documentURI,
     46     "wrong url (.documentObjectURI)");
     47  is(aDoc.baseURI, aExpects.baseURI, "wrong base");
     48  is(aDoc.baseURIObject.spec, aExpects.baseURI,
     49     "wrong base (.baseURIObject)");
     50 }
     51 
     52 function testHTMLDocURI(aDoc, aExpects) {
     53  is(aDoc.documentURI, aExpects.documentURI, "wrong url");
     54  is(aDoc.baseURI, aExpects.baseURI, "wrong base");
     55 
     56  var base = aDoc.createElement("base");
     57  var newBaseURI = "http://www.example.com/";
     58  base.href = newBaseURI;
     59  aDoc.head.appendChild(base);
     60  is(aDoc.baseURI, newBaseURI, "wrong base (after <base> changed)");
     61 }
     62 
     63 function testChromeHTMLDocURI(aDoc, aNonChromeBaseURI, aExpects) {
     64  is(aDoc.documentURI, aExpects.documentURI, "wrong url");
     65  is(aDoc.documentURIObject.spec, aExpects.documentURI,
     66     "wrong url (.documentURIObject)");
     67  is(aDoc.baseURI, aExpects.baseURI, "wrong base");
     68  is(aDoc.baseURIObject.spec, aExpects.baseURI,
     69     "wrong url (.baseURIObject)");
     70 
     71  var base = aDoc.createElement("base");
     72  var newBaseURI = "http://www.example.com/";
     73  base.href = newBaseURI;
     74  aDoc.head.appendChild(base);
     75  is(aDoc.baseURI, newBaseURI, "wrong base (after <base> changed)");
     76  is(aDoc.baseURIObject.spec, newBaseURI,
     77     "wrong base (.baseURIObject, after <base> changed)");
     78 }
     79 
     80 function testCloneDocURI(aDoc) {
     81  var clone = aDoc.cloneNode(true);
     82  is(clone.documentURI, aDoc.documentURI, "wrong url (clone)");
     83  is(clone.baseURI, aDoc.baseURI, "wrong base (clone)");
     84 }
     85 
     86 function* runTest() {
     87  is(document.baseURI, "http://example.org/", "wrong doc baseURI");
     88 
     89  // use content XHR and access URI properties from content privileged script
     90  var xhr = new XMLHttpRequest;
     91  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.xml");
     92  xhr.onreadystatechange = function() {
     93    if (!xhr.responseXML) {
     94      return;
     95    }
     96    var expects = {
     97      documentURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.xml",
     98      baseURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.xml"
     99    };
    100    testXMLDocURI(xhr.responseXML, expects);
    101    if (xhr.readyState == 4) {
    102      gen.next();
    103    }
    104  };
    105  xhr.send();
    106  yield undefined;
    107 
    108  xhr = new XMLHttpRequest;
    109  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.html");
    110  xhr.responseType = "document";
    111  xhr.onreadystatechange = function() {
    112    if (!xhr.response) {
    113      return;
    114    }
    115    var expects = {
    116      documentURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.html",
    117      baseURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.html"
    118    };
    119    testHTMLDocURI(xhr.response, expects);
    120    if (xhr.readyState == 4) {
    121      gen.next();
    122    }
    123  };
    124  xhr.send();
    125  yield undefined;
    126 
    127  xhr = new XMLHttpRequest;
    128  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.text");
    129  xhr.onreadystatechange = function() {
    130    is(xhr.responseXML, null, "should not have document");
    131    if (xhr.readyState == 4) {
    132      gen.next();
    133    }
    134  };
    135  xhr.send();
    136  yield undefined;
    137 
    138  xhr = new XMLHttpRequest;
    139  xhr.open("GET", "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml");
    140  xhr.onreadystatechange = function() {
    141    if (!xhr.responseXML) {
    142      return;
    143    }
    144    var expects = {
    145      documentURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml",
    146      baseURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml"
    147    };
    148    testXMLDocURI(xhr.responseXML, expects);
    149    if (xhr.readyState == 4) {
    150      gen.next();
    151    }
    152  };
    153  xhr.send();
    154  yield undefined;
    155 
    156  xhr = new XMLHttpRequest;
    157  xhr.open("GET", "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html");
    158  xhr.responseType = "document";
    159  xhr.onreadystatechange = function() {
    160    if (!xhr.response) {
    161      return;
    162    }
    163    var expects = {
    164      documentURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html",
    165      baseURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html"
    166    };
    167    testHTMLDocURI(xhr.response, expects);
    168    if (xhr.readyState == 4) {
    169      gen.next();
    170    }
    171  };
    172  xhr.send();
    173  yield undefined;
    174 
    175  xhr = new XMLHttpRequest;
    176  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml");
    177  xhr.onreadystatechange = function() {
    178    if (!xhr.responseXML) {
    179      return;
    180    }
    181    var expects = {
    182      documentURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml",
    183      baseURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml"
    184    };
    185    testXMLDocURI(xhr.responseXML, expects);
    186    if (xhr.readyState == 4) {
    187      gen.next();
    188    }
    189  };
    190  xhr.send();
    191  yield undefined;
    192 
    193  xhr = new XMLHttpRequest;
    194  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html");
    195  xhr.responseType = "document";
    196  xhr.onreadystatechange = function() {
    197    if (!xhr.response) {
    198      return;
    199    }
    200    var expects = {
    201      documentURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html",
    202      baseURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html"
    203    };
    204    testHTMLDocURI(xhr.response, expects);
    205    if (xhr.readyState == 4) {
    206      gen.next();
    207    }
    208  };
    209  xhr.send();
    210  yield undefined;
    211 
    212  xhr = new XMLHttpRequest;
    213  xhr.open("GET", "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.text");
    214  xhr.onreadystatechange = function() {
    215    is(xhr.responseXML, null, "should not have document");
    216    if (xhr.readyState == 4) {
    217      gen.next();
    218    }
    219  };
    220  xhr.send();
    221  yield undefined;
    222 
    223 
    224  // use content XHR and access URI properties from chrome privileged script
    225 
    226  xhr = new XMLHttpRequest;
    227  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.xml");
    228  xhr.onreadystatechange = function() {
    229    if (!xhr.responseXML) {
    230      return;
    231    }
    232    var expects = {
    233      documentURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.xml",
    234      baseURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.xml"
    235    };
    236    var xml = SpecialPowers.wrap(xhr.responseXML);
    237    testChromeXMLDocURI(xml, expects);
    238    testCloneDocURI(xml);
    239    if (xhr.readyState == 4) {
    240      gen.next();
    241    }
    242  };
    243  xhr.send();
    244  yield undefined;
    245 
    246  xhr = new XMLHttpRequest;
    247  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.html");
    248  xhr.responseType = "document";
    249  xhr.onreadystatechange = function() {
    250    if (!xhr.response) {
    251      return;
    252    }
    253    var expects = {
    254      documentURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.html",
    255      baseURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.html"
    256    };
    257    var doc = SpecialPowers.wrap(xhr.response);
    258    testChromeHTMLDocURI(doc, "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.html", expects);
    259    testCloneDocURI(doc);
    260    if (xhr.readyState == 4) {
    261      gen.next();
    262    }
    263  };
    264  xhr.send();
    265  yield undefined;
    266 
    267  xhr = new XMLHttpRequest;
    268  xhr.open("GET", "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml");
    269  xhr.onreadystatechange = function() {
    270    if (!xhr.responseXML) {
    271      return;
    272    }
    273    var expects = {
    274      documentURI: document.documentURI,
    275      baseURI: document.baseURI
    276    };
    277    var xml = SpecialPowers.wrap(xhr.responseXML);
    278    testChromeXMLDocURI(xml, expects);
    279    testCloneDocURI(xml);
    280    if (xhr.readyState == 4) {
    281      gen.next();
    282    }
    283  };
    284  xhr.send();
    285  yield undefined;
    286 
    287  xhr = new XMLHttpRequest;
    288  xhr.open("GET", "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html");
    289  xhr.responseType = "document";
    290  xhr.onreadystatechange = function() {
    291    if (!xhr.response) {
    292      return;
    293    }
    294    var expects = {
    295      documentURI: document.documentURI,
    296      baseURI: document.baseURI
    297    };
    298    var doc = SpecialPowers.wrap(xhr.response);
    299    testChromeHTMLDocURI(doc, "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html", expects);
    300    testCloneDocURI(doc);
    301    if (xhr.readyState == 4) {
    302      gen.next();
    303    }
    304  };
    305  xhr.send();
    306  yield undefined;
    307 
    308  xhr = new XMLHttpRequest;
    309  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml");
    310  xhr.onreadystatechange = function() {
    311    if (!xhr.responseXML) {
    312      return;
    313    }
    314    var expects = {
    315      documentURI: document.documentURI,
    316      baseURI: document.baseURI
    317    };
    318    var xml = SpecialPowers.wrap(xhr.responseXML);
    319    testChromeXMLDocURI(xml, expects);
    320    testCloneDocURI(xml);
    321    if (xhr.readyState == 4) {
    322      gen.next();
    323    }
    324  };
    325  xhr.send();
    326  yield undefined;
    327 
    328  xhr = new XMLHttpRequest;
    329  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html");
    330  xhr.responseType = "document";
    331  xhr.onreadystatechange = function() {
    332    if (!xhr.response) {
    333      return;
    334    }
    335    var expects = {
    336      documentURI: document.documentURI,
    337      baseURI: document.baseURI
    338    };
    339    var doc = SpecialPowers.wrap(xhr.response);
    340    testChromeHTMLDocURI(doc, "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html", expects);
    341    testCloneDocURI(doc);
    342    if (xhr.readyState == 4) {
    343      gen.next();
    344    }
    345  };
    346  xhr.send();
    347  yield undefined;
    348 
    349 
    350  // use the systemXHR special privilege
    351  SpecialPowers.addPermission("systemXHR", true, document);
    352  xhr = new XMLHttpRequest({mozAnon: false, mozSystem: true});
    353  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.xml");
    354  xhr.onreadystatechange = function() {
    355    if (!xhr.responseXML) {
    356      return;
    357    }
    358    var expects = {
    359      documentURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.xml",
    360      baseURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.xml"
    361    };
    362    testXMLDocURI(xhr.responseXML, expects);
    363    if (xhr.readyState == 4) {
    364      gen.next();
    365    }
    366  };
    367  xhr.send();
    368  yield undefined;
    369 
    370  xhr = new XMLHttpRequest({mozAnon: false, mozSystem: true});
    371  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.html");
    372  xhr.responseType = "document";
    373  xhr.onreadystatechange = function() {
    374    if (!xhr.response) {
    375      return;
    376    }
    377    var expects = {
    378      documentURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.html",
    379      baseURI: "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.html"
    380    };
    381    testHTMLDocURI(xhr.response, expects);
    382    if (xhr.readyState == 4) {
    383      gen.next();
    384    }
    385  };
    386  xhr.send();
    387  yield undefined;
    388 
    389  xhr = new XMLHttpRequest({mozAnon: false, mozSystem: true});
    390  xhr.open("GET", "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml");
    391  xhr.onreadystatechange = function() {
    392    if (!xhr.responseXML) {
    393      return;
    394    }
    395    var expects = {
    396      documentURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml",
    397      baseURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml"
    398    };
    399    testXMLDocURI(xhr.responseXML, expects);
    400    if (xhr.readyState == 4) {
    401      gen.next();
    402    }
    403  };
    404  xhr.send();
    405  yield undefined;
    406 
    407  xhr = new XMLHttpRequest({mozAnon: false, mozSystem: true});
    408  xhr.open("GET", "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html");
    409  xhr.responseType = "document";
    410  xhr.onreadystatechange = function() {
    411    if (!xhr.response) {
    412      return;
    413    }
    414    var expects = {
    415      documentURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html",
    416      baseURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html"
    417    };
    418    testHTMLDocURI(xhr.response, expects);
    419    if (xhr.readyState == 4) {
    420      gen.next();
    421    }
    422  };
    423  xhr.send();
    424  yield undefined;
    425 
    426  xhr = new XMLHttpRequest({mozAnon: false, mozSystem: true});
    427  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml");
    428  xhr.onreadystatechange = function() {
    429    if (!xhr.responseXML) {
    430      return;
    431    }
    432    var expects = {
    433      documentURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml",
    434      baseURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.xml"
    435    };
    436    testXMLDocURI(xhr.responseXML, expects);
    437    if (xhr.readyState == 4) {
    438      gen.next();
    439    }
    440  };
    441  xhr.send();
    442  yield undefined;
    443 
    444  xhr = new XMLHttpRequest({mozAnon: false, mozSystem: true});
    445  xhr.open("GET", "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRDocURI.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html");
    446  xhr.responseType = "document";
    447  xhr.onreadystatechange = function() {
    448    if (!xhr.response) {
    449      return;
    450    }
    451    var expects = {
    452      documentURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html",
    453      baseURI: "http://example.com/tests/dom/xhr/tests/file_XHRDocURI.html"
    454    };
    455    testHTMLDocURI(xhr.response, expects);
    456    if (xhr.readyState == 4) {
    457      gen.next();
    458    }
    459  };
    460  xhr.send();
    461  yield undefined;
    462 
    463  history.pushState({}, "pushStateTest", window.location.href + "/pushStateTest");
    464  ok(document.documentURI.indexOf("pushStateTest") > -1);
    465 
    466  var chromeDoc = SpecialPowers.wrap(document);
    467  ok(chromeDoc.documentURI.indexOf("pushStateTest") > -1);
    468 
    469  SimpleTest.executeSoon(function() { gen.next(); });
    470 
    471  yield undefined;
    472 
    473  window.onpopstate = function() {
    474    gen.next();
    475  }
    476  history.back();
    477 
    478  yield undefined;
    479 
    480  SimpleTest.finish();
    481  SpecialPowers.removePermission("systemXHR", document);
    482 }
    483 
    484 </script>
    485 </pre>
    486 </body>
    487 </html>