3 // utility functions for worker/window communication
5 function isInWorker() {
7 return !(self instanceof Window);
13 function message(aData) {
15 self.postMessage(aData);
17 self.postMessage(aData, "*");
23 function is(aActual, aExpected, aMessage) {
34 function ok(aBool, aMessage) {
44 function info(aMessage) {
53 function request(aURL) {
54 return new Promise(function (aResolve, aReject) {
55 var xhr = new XMLHttpRequest();
56 xhr.open("GET", aURL);
57 xhr.addEventListener("load", function () {
61 xhr.addEventListener("error", function () {
62 xhr.succeeded = false;
69 function createSequentialRequest(aParameters, aTest) {
70 var sequence = aParameters.reduce(function (aPromise, aParam) {
73 return request(aParam.requestURL);
75 .then(function (aXHR) {
76 return aTest(aXHR, aParam);
78 }, Promise.resolve());
83 function testSuccessResponse() {
84 var blob = new Blob(["data"], { type: "text/plain" });
85 var blobURL = URL.createObjectURL(blob);
88 // tests that start with same-origin request
90 message: "request to same-origin without redirect",
92 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text",
94 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text",
97 message: "request to same-origin redirect to same-origin URL",
99 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text",
101 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text",
105 "request to same-origin redirects several times and finally go to same-origin URL",
107 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=" +
109 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text"
112 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text",
115 message: "request to same-origin redirect to cross-origin URL",
117 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.text",
119 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.text",
123 "request to same-origin redirects several times and finally go to cross-origin URL",
125 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=" +
127 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.text"
130 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.text",
133 // tests that start with cross-origin request
135 message: "request to cross-origin without redirect",
137 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.text",
139 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.text",
142 message: "request to cross-origin redirect back to same-origin URL",
144 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text",
146 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text",
149 message: "request to cross-origin redirect to the same cross-origin URL",
151 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.text",
153 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.text",
156 message: "request to cross-origin redirect to another cross-origin URL",
158 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://example.org/tests/dom/xhr/tests/file_XHRResponseURL.text",
160 "http://example.org/tests/dom/xhr/tests/file_XHRResponseURL.text",
164 "request to cross-origin redirects several times and finally go to same-origin URL",
166 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=" +
168 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text"
171 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text",
175 "request to cross-origin redirects several times and finally go to the same cross-origin URL",
177 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=" +
179 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.text"
182 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.text",
186 "request to cross-origin redirects several times and finally go to another cross-origin URL",
188 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=" +
190 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://example.org/tests/dom/xhr/tests/file_XHRResponseURL.text"
193 "http://example.org/tests/dom/xhr/tests/file_XHRResponseURL.text",
197 "request to cross-origin redirects to another cross-origin and finally go to the other cross-origin URL",
199 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=" +
201 "http://example.org/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://test1.example.com/tests/dom/xhr/tests/file_XHRResponseURL.text"
204 "http://test1.example.com/tests/dom/xhr/tests/file_XHRResponseURL.text",
207 message: "request URL has fragment",
209 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text#fragment",
211 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text",
214 // tests for non-http(s) URL
216 message: "request to data: URL",
217 requestURL: "data:text/plain,data",
218 responseURL: "data:text/plain,data",
221 message: "request to blob: URL",
223 responseURL: blobURL,
227 var sequence = createSequentialRequest(parameters, function (aXHR, aParam) {
228 ok(aXHR.succeeded, "assert request succeeded");
229 is(aXHR.responseURL, aParam.responseURL, aParam.message);
232 sequence.then(function () {
233 URL.revokeObjectURL(blobURL);
239 function testFailedResponse() {
240 info("test not to leak responseURL for denied cross-origin request");
244 "should be empty for denied cross-origin request without redirect",
246 "http://example.com/tests/dom/xhr/tests/file_XHRResponseURL_nocors.text",
249 message: "should be empty for denied cross-origin request with redirect",
251 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://example.com/tests/dom/xhr/tests/file_XHRResponseURL_nocors.text",
255 var sequence = createSequentialRequest(parameters, function (aXHR, aParam) {
256 ok(!aXHR.succeeded, "assert request failed");
257 is(aXHR.responseURL, "", aParam.message);
263 function testNotToLeakResponseURLWhileDoingRedirects() {
264 info("test not to leak responeseURL while doing redirects");
267 return testNotToLeakResponseURLWhileDoingRedirectsInWorker();
269 return testNotToLeakResponseURLWhileDoingRedirectsInWindow();
272 function testNotToLeakResponseURLWhileDoingRedirectsInWindow() {
273 var xhr = new XMLHttpRequest();
274 var requestObserver = {
275 observe(aSubject, aTopic, aData) {
276 is(xhr.readyState, XMLHttpRequest.OPENED, "assert for XHR state");
280 "responseURL should return empty string before HEADERS_RECEIVED"
284 SpecialPowers.addObserver(
286 "specialpowers-http-notify-request"
289 return new Promise(function (aResolve, aReject) {
292 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text"
294 xhr.addEventListener("load", function () {
295 SpecialPowers.removeObserver(
297 "specialpowers-http-notify-request"
301 xhr.addEventListener("error", function () {
302 ok(false, "unexpected request falilure");
303 SpecialPowers.removeObserver(
305 "specialpowers-http-notify-request"
313 function testNotToLeakResponseURLWhileDoingRedirectsInWorker() {
314 var xhr = new XMLHttpRequest();
315 var testRedirect = function (e) {
316 if (e.data === "request" && xhr.readyState === XMLHttpRequest.OPENED) {
320 "responseURL should return empty string before HEADERS_RECEIVED"
325 return new Promise(function (aResolve, aReject) {
326 self.addEventListener("message", testRedirect);
327 message({ type: "redirect_test", status: "start" });
330 "http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.sjs?url=http://mochi.test:8888/tests/dom/xhr/tests/file_XHRResponseURL.text"
332 xhr.addEventListener("load", function () {
333 self.removeEventListener("message", testRedirect);
334 message({ type: "redirect_test", status: "end" });
337 xhr.addEventListener("error", function (e) {
338 ok(false, "unexpected request falilure");
339 self.removeEventListener("message", testRedirect);
340 message({ type: "redirect_test", status: "end" });
347 function waitForAllMessagesProcessed() {
348 return new Promise(function (aResolve, aReject) {
349 var id = setInterval(function () {
350 if (message.ping === message.pong) {
358 self.addEventListener("message", function (aEvent) {
359 if (aEvent.data === "start") {
361 "responseURL" in new XMLHttpRequest(),
362 "XMLHttpRequest should have responseURL attribute"
365 new XMLHttpRequest().responseURL,
367 "responseURL should be empty string if response's url is null"
370 var promise = testSuccessResponse();
373 return testFailedResponse();
376 return testNotToLeakResponseURLWhileDoingRedirects();
379 return waitForAllMessagesProcessed();
385 if (aEvent.data === "pong") {