resource_timing_cross_origin.html (8792B)
1 <!-- 2 Any copyright is dedicated to the Public Domain. 3 http://creativecommons.org/publicdomain/zero/1.0/ 4 --> 5 6 <!DOCTYPE HTML> 7 <html> 8 <head> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 10 <script type="application/javascript"> 11 12 function ok(cond, message) { 13 window.opener.ok(cond, message) 14 } 15 16 function is(received, expected, message) { 17 window.opener.is(received, expected, message); 18 } 19 20 function isnot(received, notExpected, message) { 21 window.opener.isnot(received, notExpected, message); 22 } 23 24 var bufferFullCounter = 0; 25 const expectedBufferFullEvents = 0; 26 27 const properties = ["startTime", "redirectStart", "redirectEnd", "fetchStart", 28 "domainLookupStart", "domainLookupEnd", "connectStart", 29 "connectEnd", "requestStart", "responseStart", "responseEnd"]; 30 31 window.onload = function() { 32 ok(!!window.performance, "Performance object should exist"); 33 ok(!!window.performance.getEntries, "Performance.getEntries() should exist"); 34 ok(!!window.performance.getEntriesByName, "Performance.getEntriesByName() should exist"); 35 ok(!!window.performance.getEntriesByType, "Performance.getEntriesByType() should exist"); 36 37 window.performance.onresourcetimingbufferfull = function() { 38 bufferFullCounter += 1; 39 } 40 41 makeXhr("http://mochi.test:8888/tests/dom/tests/mochitest/general/test-data.json", firstCheck); 42 }; 43 44 function firstCheck() { 45 var entries = window.performance.getEntriesByName("http://mochi.test:8888/tests/dom/tests/mochitest/general/test-data.json"); 46 ok(!!entries[0], "same origin test-data.json is missing from entries"); 47 checkSameOrigin(entries[0]); 48 49 var entries = window.performance.getEntriesByName("http://mochi.test:8888/tests/dom/tests/mochitest/general/res0.resource"); 50 ok(!!entries[0], "same origin res0.resource is missing from entries"); 51 checkSameOrigin(entries[0]); 52 53 entries = window.performance.getEntriesByName("http://test1.example.com/tests/dom/tests/mochitest/general/res0.resource"); 54 ok(!!entries[0], "cross origin res0.resource is missing from entries"); 55 checkCrossOrigin(entries[0]); 56 57 entries = window.performance.getEntriesByName("http://test1.example.com/tests/dom/tests/mochitest/general/res1.resource"); 58 ok(!!entries[0], "res1.resource is missing from entries"); 59 checkSameOrigin(entries[0]); 60 61 entries = window.performance.getEntriesByName("http://test1.example.com/tests/dom/tests/mochitest/general/res2.resource"); 62 ok(!!entries[0], "redirected res2.resource is missing from entries"); 63 checkRedirectedCrossOrigin(entries[0]); 64 65 entries = window.performance.getEntriesByName("http://test1.example.com/tests/dom/tests/mochitest/general/res3.resource"); 66 ok(!!entries[0], "cross origin res3.resource is missing from entries"); 67 checkSameOrigin(entries[0]); 68 69 entries = window.performance.getEntriesByName("http://test1.example.com/tests/dom/tests/mochitest/general/res4.resource"); 70 ok(!!entries[0], "redirected res4.resource is missing from entries"); 71 checkRedirectedSameOrigin(entries[0]); 72 73 entries = window.performance.getEntriesByName("http://test1.example.com/tests/dom/tests/mochitest/general/res5.resource"); 74 ok(!!entries[0], "cross origin res5.resource is missing from entries"); 75 checkCrossOrigin(entries[0]); 76 77 entries = window.performance.getEntriesByName("http://test1.example.com/tests/dom/tests/mochitest/general/res6.resource"); 78 ok(!!entries[0], "cross origin res6.resource is missing from entries"); 79 checkCrossOrigin(entries[0]); 80 81 entries = window.performance.getEntriesByName("http://test1.example.com/tests/dom/tests/mochitest/general/res7.resource"); 82 ok(!!entries[0], "cross origin res7.resource is missing from entries"); 83 checkCrossOrigin(entries[0]); 84 85 entries = window.performance.getEntriesByName("http://test1.example.com/tests/dom/tests/mochitest/general/res8.resource"); 86 ok(!!entries[0], "redirected res8.resource is missing from entries"); 87 checkRedirectCrossOriginResourceSameOrigin(entries[0]); 88 89 entries = window.performance.getEntriesByName("http://mochi.test:8888/tests/dom/tests/mochitest/general/resource_timing.js"); 90 ok(!!entries[0], "same origin resource_timing.js is missing from entries"); 91 checkSameOrigin(entries[0]); 92 93 is(bufferFullCounter, expectedBufferFullEvents, "Buffer full was called"); 94 finishTests(); 95 } 96 97 function checkEntry(entry, checks) { 98 // If the entry is undefined, we return early so we don't get a JS error 99 if (entry == undefined) 100 return; 101 102 for (var j = 1; j < properties.length; ++j) { 103 var prop = properties[j]; 104 if (checks[prop] != undefined) { 105 is(entry[prop], checks[prop], "Wrong value for prop " + prop + " for resource " + entry.name); 106 } else { 107 isnot(entry[prop], 0, "Wrong value for prop " + prop + " for resource " + entry.name); 108 } 109 } 110 } 111 112 // No redirects have occured so RedirectStart/End are 0 113 function checkSameOrigin(entry) { 114 const checks = { "redirectStart": 0, "redirectEnd": 0 }; 115 checkEntry(entry, checks); 116 } 117 118 // This is a cross-origin resource that doesn't pass the check 119 // All of these attributes are 0. No redirects 120 function checkCrossOrigin(entry) { 121 const checks = { "redirectStart": 0, "redirectEnd": 0, 122 "domainLookupStart": 0, "domainLookupEnd": 0, 123 "connectStart": 0, "connectEnd": 0, 124 "requestStart": 0, "responseStart": 0 }; 125 checkEntry(entry, checks); 126 } 127 128 // A cross-origin redirect has occured. RedirectStart/End and the rest of the 129 // attributes are 0. 130 function checkRedirectedCrossOrigin(entry) { 131 const checks = { "redirectStart": 0, "redirectEnd": 0, 132 "domainLookupStart": 0, "domainLookupEnd": 0, 133 "connectStart": 0, "connectEnd": 0, 134 "requestStart": 0, "responseStart": 0 }; 135 checkEntry(entry, checks); 136 } 137 138 // The redirect is to the same origin as the initial document, 139 // so no entries are 0. 140 function checkRedirectedSameOrigin(entry) { 141 const checks = { }; 142 checkEntry(entry, checks); 143 } 144 145 // The final entry passes the timing-allow-check, 146 // but one of the redirects does not. redirectStart/End and the rest of the 147 // attributes are 0 because all redirects need to pass TAO check. 148 function checkRedirectCrossOriginResourceSameOrigin(entry) { 149 const checks = { "redirectStart": 0, "redirectEnd": 0, 150 "domainLookupStart": 0, "domainLookupEnd": 0, 151 "connectStart": 0, "connectEnd": 0, 152 "requestStart": 0, "responseStart": 0 }; 153 checkEntry(entry, checks); 154 } 155 156 function makeXhr(aUrl, aCallback) { 157 var xmlhttp = new XMLHttpRequest(); 158 xmlhttp.onload = aCallback; 159 xmlhttp.open("get", aUrl, true); 160 xmlhttp.send(); 161 } 162 163 function finishTests() { 164 window.opener.finishTests(); 165 } 166 167 </script> 168 169 <body> 170 <a target="_blank" 171 href="https://bugzilla.mozilla.org/show_bug.cgi?id=822480" 172 title="Add resource timing API."> 173 Bug #822480 - Add in the Resource Timing API 174 </a> 175 <p id="display"></p> 176 <div id="content"> 177 <object data="http://mochi.test:8888/tests/dom/tests/mochitest/general/res0.resource"></object> <!-- same origin, no header --> 178 <object data="http://test1.example.com/tests/dom/tests/mochitest/general/res0.resource"></object> <!-- cross origin, no header --> 179 <object data="http://test1.example.com/tests/dom/tests/mochitest/general/res1.resource"></object> <!-- cross origin, Timing-Allow-Origin: * header --> 180 <object data="http://test1.example.com/tests/dom/tests/mochitest/general/res2.resource"></object> <!-- cross origin redirect to test2.example.com, no header --> 181 <object data="http://test1.example.com/tests/dom/tests/mochitest/general/res3.resource"></object> <!-- cross origin, Timing-Allow-Origin: http://mochi.test:8888 header --> 182 <object data="http://test1.example.com/tests/dom/tests/mochitest/general/res4.resource"></object> <!-- cross origin redirect to mochi.test:8888/.../res1.resource, Timing-Allow-Origin: * --> 183 <object data="http://test1.example.com/tests/dom/tests/mochitest/general/res5.resource"></object> <!-- cross origin, Timing-Allow-Origin: http://mochi.test:8889 --> 184 <object data="http://test1.example.com/tests/dom/tests/mochitest/general/res6.resource"></object> <!-- cross origin, Timing-Allow-Origin: "" (empty string) --> 185 <object data="http://test1.example.com/tests/dom/tests/mochitest/general/res7.resource"></object> <!-- cross origin, Timing-Allow-Origin: http://mochi.test:8888 http://test1.com header --> 186 <object data="http://test1.example.com/tests/dom/tests/mochitest/general/res8.resource"></object> <!-- double cross origin redirect --> 187 <script type="text/javascript" src="http://mochi.test:8888/tests/dom/tests/mochitest/general/resource_timing.js"></script> <!-- same origin script --> 188 </div> 189 </body> 190 191 </html>