mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-01-09 13:25:00 +00:00
c7264cb3ac
--HG-- rename : dom/workers/XMLHttpRequestUploadWorker.cpp => dom/xhr/XMLHttpRequestUploadWorker.cpp rename : dom/workers/XMLHttpRequestUploadWorker.h => dom/xhr/XMLHttpRequestUploadWorker.h rename : dom/workers/XMLHttpRequestWorker.cpp => dom/xhr/XMLHttpRequestWorker.cpp rename : dom/workers/XMLHttpRequestWorker.h => dom/xhr/XMLHttpRequestWorker.h rename : dom/base/nsIXMLHttpRequest.idl => dom/xhr/nsIXMLHttpRequest.idl rename : dom/base/nsXMLHttpRequest.cpp => dom/xhr/nsXMLHttpRequest.cpp rename : dom/base/nsXMLHttpRequest.h => dom/xhr/nsXMLHttpRequest.h rename : dom/base/test/echo.sjs => dom/xhr/tests/echo.sjs rename : dom/base/test/file_XHRDocURI.html => dom/xhr/tests/file_XHRDocURI.html rename : dom/base/test/file_XHRDocURI.html^headers^ => dom/xhr/tests/file_XHRDocURI.html^headers^ rename : dom/base/test/file_XHRDocURI.sjs => dom/xhr/tests/file_XHRDocURI.sjs rename : dom/base/test/file_XHRDocURI.text => dom/xhr/tests/file_XHRDocURI.text rename : dom/base/test/file_XHRDocURI.text^headers^ => dom/xhr/tests/file_XHRDocURI.text^headers^ rename : dom/base/test/file_XHRDocURI.xml => dom/xhr/tests/file_XHRDocURI.xml rename : dom/base/test/file_XHRDocURI.xml^headers^ => dom/xhr/tests/file_XHRDocURI.xml^headers^ rename : dom/base/test/file_XHRResponseURL.js => dom/xhr/tests/file_XHRResponseURL.js rename : dom/base/test/file_XHRResponseURL.sjs => dom/xhr/tests/file_XHRResponseURL.sjs rename : dom/base/test/file_XHRResponseURL.text => dom/xhr/tests/file_XHRResponseURL.text rename : dom/base/test/file_XHRResponseURL.text^headers^ => dom/xhr/tests/file_XHRResponseURL.text^headers^ rename : dom/base/test/file_XHRResponseURL_nocors.text => dom/xhr/tests/file_XHRResponseURL_nocors.text rename : dom/base/test/file_XHRSendData.sjs => dom/xhr/tests/file_XHRSendData.sjs rename : dom/base/test/file_XHRSendData_doc.xml => dom/xhr/tests/file_XHRSendData_doc.xml rename : dom/base/test/file_XHRSendData_doc.xml^headers^ => dom/xhr/tests/file_XHRSendData_doc.xml^headers^ rename : dom/base/test/file_XHR_anon.sjs => dom/xhr/tests/file_XHR_anon.sjs rename : dom/base/test/file_XHR_binary1.bin => dom/xhr/tests/file_XHR_binary1.bin rename : dom/base/test/file_XHR_binary1.bin^headers^ => dom/xhr/tests/file_XHR_binary1.bin^headers^ rename : dom/base/test/file_XHR_binary2.bin => dom/xhr/tests/file_XHR_binary2.bin rename : dom/base/test/file_XHR_fail1.txt => dom/xhr/tests/file_XHR_fail1.txt rename : dom/base/test/file_XHR_fail1.txt^headers^ => dom/xhr/tests/file_XHR_fail1.txt^headers^ rename : dom/base/test/file_XHR_fail1b.txt => dom/xhr/tests/file_XHR_fail1b.txt rename : dom/base/test/file_XHR_header.sjs => dom/xhr/tests/file_XHR_header.sjs rename : dom/base/test/file_XHR_pass1.xml => dom/xhr/tests/file_XHR_pass1.xml rename : dom/base/test/file_XHR_pass2.txt => dom/xhr/tests/file_XHR_pass2.txt rename : dom/base/test/file_XHR_pass3.txt => dom/xhr/tests/file_XHR_pass3.txt rename : dom/base/test/file_XHR_pass3.txt^headers^ => dom/xhr/tests/file_XHR_pass3.txt^headers^ rename : dom/base/test/file_XHR_system_redirect.html => dom/xhr/tests/file_XHR_system_redirect.html rename : dom/base/test/file_XHR_system_redirect.html^headers^ => dom/xhr/tests/file_XHR_system_redirect.html^headers^ rename : dom/base/test/file_XHR_timeout.sjs => dom/xhr/tests/file_XHR_timeout.sjs rename : dom/base/test/file_html_in_xhr.html => dom/xhr/tests/file_html_in_xhr.html rename : dom/base/test/file_html_in_xhr.sjs => dom/xhr/tests/file_html_in_xhr.sjs rename : dom/base/test/file_html_in_xhr2.html => dom/xhr/tests/file_html_in_xhr2.html rename : dom/base/test/file_html_in_xhr3.html => dom/xhr/tests/file_html_in_xhr3.html rename : dom/base/test/progressserver.sjs => dom/xhr/tests/progressserver.sjs rename : dom/workers/test/relativeLoad_import.js => dom/xhr/tests/relativeLoad_import.js rename : dom/workers/test/relativeLoad_worker.js => dom/xhr/tests/relativeLoad_worker.js rename : dom/workers/test/relativeLoad_worker2.js => dom/xhr/tests/relativeLoad_worker2.js rename : dom/base/test/responseIdentical.sjs => dom/xhr/tests/responseIdentical.sjs rename : dom/workers/test/subdir/relativeLoad_sub_import.js => dom/xhr/tests/subdir/relativeLoad_sub_import.js rename : dom/workers/test/subdir/relativeLoad_sub_worker.js => dom/xhr/tests/subdir/relativeLoad_sub_worker.js rename : dom/workers/test/subdir/relativeLoad_sub_worker2.js => dom/xhr/tests/subdir/relativeLoad_sub_worker2.js rename : dom/workers/test/terminateSyncXHR_worker.js => dom/xhr/tests/terminateSyncXHR_worker.js rename : dom/base/test/test_XHR.html => dom/xhr/tests/test_XHR.html rename : dom/base/test/test_XHRDocURI.html => dom/xhr/tests/test_XHRDocURI.html rename : dom/base/test/test_XHRResponseURL.html => dom/xhr/tests/test_XHRResponseURL.html rename : dom/base/test/test_XHRSendData.html => dom/xhr/tests/test_XHRSendData.html rename : dom/base/test/test_XHR_anon.html => dom/xhr/tests/test_XHR_anon.html rename : dom/base/test/test_XHR_header.html => dom/xhr/tests/test_XHR_header.html rename : dom/base/test/test_XHR_onuploadprogress.html => dom/xhr/tests/test_XHR_onuploadprogress.html rename : dom/base/test/test_XHR_parameters.html => dom/xhr/tests/test_XHR_parameters.html rename : dom/base/test/test_XHR_system.html => dom/xhr/tests/test_XHR_system.html rename : dom/base/test/test_XHR_timeout.html => dom/xhr/tests/test_XHR_timeout.html rename : dom/base/test/test_XHR_timeout.js => dom/xhr/tests/test_XHR_timeout.js rename : dom/base/test/test_html_in_xhr.html => dom/xhr/tests/test_html_in_xhr.html rename : dom/workers/test/test_relativeLoad.html => dom/xhr/tests/test_relativeLoad.html rename : dom/base/test/test_sync_xhr_timer.xhtml => dom/xhr/tests/test_sync_xhr_timer.xhtml rename : dom/workers/test/test_terminateSyncXHR.html => dom/xhr/tests/test_worker_terminateSyncXHR.html rename : dom/workers/test/test_xhr.html => dom/xhr/tests/test_worker_xhr.html rename : dom/workers/test/test_xhr2.html => dom/xhr/tests/test_worker_xhr2.html rename : dom/workers/test/test_xhrAbort.html => dom/xhr/tests/test_worker_xhrAbort.html rename : dom/workers/test/test_xhr_3rdparty.html => dom/xhr/tests/test_worker_xhr_3rdparty.html rename : dom/workers/test/test_xhr_cors_redirect.html => dom/xhr/tests/test_worker_xhr_cors_redirect.html rename : dom/workers/test/test_xhr_headers.html => dom/xhr/tests/test_worker_xhr_headers.html rename : dom/workers/test/test_xhr_implicit_cancel.html => dom/xhr/tests/test_worker_xhr_implicit_cancel.html rename : dom/workers/test/test_xhr_parameters.html => dom/xhr/tests/test_worker_xhr_parameters.html rename : dom/workers/test/test_xhr_parameters.js => dom/xhr/tests/test_worker_xhr_parameters.js rename : dom/workers/test/test_xhr_responseURL.html => dom/xhr/tests/test_worker_xhr_responseURL.html rename : dom/workers/test/test_xhr_system.html => dom/xhr/tests/test_worker_xhr_system.html rename : dom/workers/test/test_xhr_system.js => dom/xhr/tests/test_worker_xhr_system.js rename : dom/workers/test/test_xhr_timeout.html => dom/xhr/tests/test_worker_xhr_timeout.html rename : dom/base/test/test_xhr_abort_after_load.html => dom/xhr/tests/test_xhr_abort_after_load.html rename : dom/base/test/test_xhr_forbidden_headers.html => dom/xhr/tests/test_xhr_forbidden_headers.html rename : dom/base/test/test_xhr_overridemimetype_throws_on_invalid_state.html => dom/xhr/tests/test_xhr_overridemimetype_throws_on_invalid_state.html rename : dom/base/test/test_xhr_progressevents.html => dom/xhr/tests/test_xhr_progressevents.html rename : dom/base/test/test_xhr_send.html => dom/xhr/tests/test_xhr_send.html rename : dom/base/test/test_xhr_send_readystate.html => dom/xhr/tests/test_xhr_send_readystate.html rename : dom/base/test/test_xhr_withCredentials.html => dom/xhr/tests/test_xhr_withCredentials.html rename : dom/workers/test/file_getcookie.sjs => dom/xhr/tests/worker_file_getcookie.sjs rename : dom/workers/test/terminateSyncXHR_frame.html => dom/xhr/tests/worker_terminateSyncXHR_frame.html rename : dom/workers/test/testXHR.txt => dom/xhr/tests/worker_testXHR.txt rename : dom/workers/test/xhr_cors_redirect.js => dom/xhr/tests/worker_xhr_cors_redirect.js rename : dom/workers/test/xhr_cors_redirect.sjs => dom/xhr/tests/worker_xhr_cors_redirect.sjs rename : dom/workers/test/xhr_headers_server.sjs => dom/xhr/tests/worker_xhr_headers_server.sjs rename : dom/workers/test/xhr_headers_worker.js => dom/xhr/tests/worker_xhr_headers_worker.js rename : dom/workers/test/xhr2_worker.js => dom/xhr/tests/xhr2_worker.js rename : dom/workers/test/xhrAbort_worker.js => dom/xhr/tests/xhrAbort_worker.js rename : dom/workers/test/xhr_implicit_cancel_worker.js => dom/xhr/tests/xhr_implicit_cancel_worker.js rename : dom/workers/test/xhr_worker.js => dom/xhr/tests/xhr_worker.js
343 lines
8.9 KiB
JavaScript
343 lines
8.9 KiB
JavaScript
/* Notes:
|
|
- All times are expressed in milliseconds in this test suite.
|
|
- Test harness code is at the end of this file.
|
|
- We generate only one request at a time, to avoid overloading the HTTP
|
|
request handlers.
|
|
*/
|
|
|
|
var inWorker = false;
|
|
try {
|
|
inWorker = !(self instanceof Window);
|
|
} catch (e) {
|
|
inWorker = true;
|
|
}
|
|
|
|
function message(data) {
|
|
if (inWorker)
|
|
self.postMessage(data);
|
|
else
|
|
self.postMessage(data, "*");
|
|
}
|
|
|
|
function is(got, expected, msg) {
|
|
var obj = {};
|
|
obj.type = "is";
|
|
obj.got = got;
|
|
obj.expected = expected;
|
|
obj.msg = msg;
|
|
|
|
message(obj);
|
|
}
|
|
|
|
function ok(bool, msg) {
|
|
var obj = {};
|
|
obj.type = "ok";
|
|
obj.bool = bool;
|
|
obj.msg = msg;
|
|
|
|
message(obj);
|
|
}
|
|
|
|
/**
|
|
* Generate and track results from a XMLHttpRequest with regards to timeouts.
|
|
*
|
|
* @param {String} id The test description.
|
|
* @param {Number} timeLimit The initial setting for the request timeout.
|
|
* @param {Number} resetAfter (Optional) The time after sending the request, to
|
|
* reset the timeout.
|
|
* @param {Number} resetTo (Optional) The delay to reset the timeout to.
|
|
*
|
|
* @note The actual testing takes place in handleEvent(event).
|
|
* The requests are generated in startXHR().
|
|
*
|
|
* @note If resetAfter and resetTo are omitted, only the initial timeout setting
|
|
* applies.
|
|
*
|
|
* @constructor
|
|
* @implements DOMEventListener
|
|
*/
|
|
function RequestTracker(async, id, timeLimit /*[, resetAfter, resetTo]*/) {
|
|
this.async = async;
|
|
this.id = id;
|
|
this.timeLimit = timeLimit;
|
|
|
|
if (arguments.length > 3) {
|
|
this.mustReset = true;
|
|
this.resetAfter = arguments[3];
|
|
this.resetTo = arguments[4];
|
|
}
|
|
|
|
this.hasFired = false;
|
|
}
|
|
RequestTracker.prototype = {
|
|
/**
|
|
* Start the XMLHttpRequest!
|
|
*/
|
|
startXHR: function() {
|
|
var req = new XMLHttpRequest();
|
|
this.request = req;
|
|
req.open("GET", "file_XHR_timeout.sjs", this.async);
|
|
var me = this;
|
|
function handleEvent(e) { return me.handleEvent(e); };
|
|
req.onerror = handleEvent;
|
|
req.onload = handleEvent;
|
|
req.onabort = handleEvent;
|
|
req.ontimeout = handleEvent;
|
|
|
|
req.timeout = this.timeLimit;
|
|
|
|
if (this.mustReset) {
|
|
var resetTo = this.resetTo;
|
|
self.setTimeout(function() {
|
|
req.timeout = resetTo;
|
|
}, this.resetAfter);
|
|
}
|
|
|
|
req.send(null);
|
|
},
|
|
|
|
/**
|
|
* Get a message describing this test.
|
|
*
|
|
* @returns {String} The test description.
|
|
*/
|
|
getMessage: function() {
|
|
var rv = this.id + ", ";
|
|
if (this.mustReset) {
|
|
rv += "original timeout at " + this.timeLimit + ", ";
|
|
rv += "reset at " + this.resetAfter + " to " + this.resetTo;
|
|
}
|
|
else {
|
|
rv += "timeout scheduled at " + this.timeLimit;
|
|
}
|
|
return rv;
|
|
},
|
|
|
|
/**
|
|
* Check the event received, and if it's the right (and only) one we get.
|
|
*
|
|
* @param {DOMProgressEvent} evt An event of type "load" or "timeout".
|
|
*/
|
|
handleEvent: function(evt) {
|
|
if (this.hasFired) {
|
|
ok(false, "Only one event should fire: " + this.getMessage());
|
|
return;
|
|
}
|
|
this.hasFired = true;
|
|
|
|
var type = evt.type, expectedType;
|
|
// The XHR responds after 3000 milliseconds with a load event.
|
|
var timeLimit = this.mustReset && (this.resetAfter < Math.min(3000, this.timeLimit)) ?
|
|
this.resetTo :
|
|
this.timeLimit;
|
|
if ((timeLimit == 0) || (timeLimit >= 3000)) {
|
|
expectedType = "load";
|
|
}
|
|
else {
|
|
expectedType = "timeout";
|
|
}
|
|
is(type, expectedType, this.getMessage());
|
|
TestCounter.testComplete();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Generate and track XMLHttpRequests which will have abort() called on.
|
|
*
|
|
* @param shouldAbort {Boolean} True if we should call abort at all.
|
|
* @param abortDelay {Number} The time in ms to wait before calling abort().
|
|
*/
|
|
function AbortedRequest(shouldAbort, abortDelay) {
|
|
this.shouldAbort = shouldAbort;
|
|
this.abortDelay = abortDelay;
|
|
this.hasFired = false;
|
|
}
|
|
AbortedRequest.prototype = {
|
|
/**
|
|
* Start the XMLHttpRequest!
|
|
*/
|
|
startXHR: function() {
|
|
var req = new XMLHttpRequest();
|
|
this.request = req;
|
|
req.open("GET", "file_XHR_timeout.sjs");
|
|
var me = this;
|
|
function handleEvent(e) { return me.handleEvent(e); };
|
|
req.onerror = handleEvent;
|
|
req.onload = handleEvent;
|
|
req.onabort = handleEvent;
|
|
req.ontimeout = handleEvent;
|
|
|
|
req.timeout = 2000;
|
|
var _this = this;
|
|
|
|
function abortReq() {
|
|
req.abort();
|
|
}
|
|
|
|
if (!this.shouldAbort) {
|
|
self.setTimeout(function() {
|
|
try {
|
|
_this.noEventsFired();
|
|
}
|
|
catch (e) {
|
|
ok(false, "Unexpected error: " + e);
|
|
TestCounter.testComplete();
|
|
}
|
|
}, 5000);
|
|
}
|
|
else {
|
|
// Abort events can only be triggered on sent requests.
|
|
req.send();
|
|
if (this.abortDelay == -1) {
|
|
abortReq();
|
|
}
|
|
else {
|
|
self.setTimeout(abortReq, this.abortDelay);
|
|
}
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Ensure that no events fired at all, especially not our timeout event.
|
|
*/
|
|
noEventsFired: function() {
|
|
ok(!this.hasFired, "No events should fire for an unsent, unaborted request");
|
|
// We're done; if timeout hasn't fired by now, it never will.
|
|
TestCounter.testComplete();
|
|
},
|
|
|
|
/**
|
|
* Get a message describing this test.
|
|
*
|
|
* @returns {String} The test description.
|
|
*/
|
|
getMessage: function() {
|
|
return "time to abort is " + this.abortDelay + ", timeout set at 2000";
|
|
},
|
|
|
|
/**
|
|
* Check the event received, and if it's the right (and only) one we get.
|
|
*
|
|
* @param {DOMProgressEvent} evt An event of type "load" or "timeout".
|
|
*/
|
|
handleEvent: function(evt) {
|
|
if (this.hasFired) {
|
|
ok(false, "Only abort event should fire: " + this.getMessage());
|
|
return;
|
|
}
|
|
this.hasFired = true;
|
|
|
|
var expectedEvent = (this.abortDelay >= 2000) ? "timeout" : "abort";
|
|
is(evt.type, expectedEvent, this.getMessage());
|
|
TestCounter.testComplete();
|
|
}
|
|
};
|
|
|
|
var SyncRequestSettingTimeoutAfterOpen = {
|
|
startXHR: function() {
|
|
var pass = false;
|
|
var req = new XMLHttpRequest();
|
|
req.open("GET", "file_XHR_timeout.sjs", false);
|
|
try {
|
|
req.timeout = 1000;
|
|
}
|
|
catch (e) {
|
|
pass = true;
|
|
}
|
|
ok(pass, "Synchronous XHR must not allow a timeout to be set");
|
|
TestCounter.testComplete();
|
|
}
|
|
};
|
|
|
|
var SyncRequestSettingTimeoutBeforeOpen = {
|
|
startXHR: function() {
|
|
var pass = false;
|
|
var req = new XMLHttpRequest();
|
|
req.timeout = 1000;
|
|
try {
|
|
req.open("GET", "file_XHR_timeout.sjs", false);
|
|
}
|
|
catch (e) {
|
|
pass = true;
|
|
}
|
|
ok(pass, "Synchronous XHR must not allow a timeout to be set");
|
|
TestCounter.testComplete();
|
|
}
|
|
};
|
|
|
|
var TestRequests = [
|
|
// Simple timeouts.
|
|
new RequestTracker(true, "no time out scheduled, load fires normally", 0),
|
|
new RequestTracker(true, "load fires normally", 5000),
|
|
new RequestTracker(true, "timeout hit before load", 2000),
|
|
|
|
// Timeouts reset after a certain delay.
|
|
new RequestTracker(true, "load fires normally with no timeout set, twice", 0, 2000, 0),
|
|
new RequestTracker(true, "load fires normally with same timeout set twice", 5000, 2000, 5000),
|
|
new RequestTracker(true, "timeout fires normally with same timeout set twice", 2000, 1000, 2000),
|
|
|
|
new RequestTracker(true, "timeout disabled after initially set", 5000, 2000, 0),
|
|
new RequestTracker(true, "timeout overrides load after a delay", 5000, 1000, 2000),
|
|
new RequestTracker(true, "timeout enabled after initially disabled", 0, 2000, 5000),
|
|
|
|
new RequestTracker(true, "timeout set to expiring value after load fires", 5000, 4000, 1000),
|
|
new RequestTracker(true, "timeout set to expired value before load fires", 5000, 2000, 1000),
|
|
new RequestTracker(true, "timeout set to non-expiring value after timeout fires", 1000, 2000, 5000),
|
|
|
|
// Aborted requests.
|
|
new AbortedRequest(false),
|
|
new AbortedRequest(true, -1),
|
|
new AbortedRequest(true, 5000),
|
|
];
|
|
|
|
var MainThreadTestRequests = [
|
|
new AbortedRequest(true, 0),
|
|
new AbortedRequest(true, 1000),
|
|
|
|
// Synchronous requests.
|
|
SyncRequestSettingTimeoutAfterOpen,
|
|
SyncRequestSettingTimeoutBeforeOpen
|
|
];
|
|
|
|
var WorkerThreadTestRequests = [
|
|
// Simple timeouts.
|
|
new RequestTracker(false, "no time out scheduled, load fires normally", 0),
|
|
new RequestTracker(false, "load fires normally", 5000),
|
|
new RequestTracker(false, "timeout hit before load", 2000),
|
|
|
|
// Reset timeouts don't make much sense with a sync request ...
|
|
];
|
|
|
|
if (inWorker) {
|
|
TestRequests = TestRequests.concat(WorkerThreadTestRequests);
|
|
} else {
|
|
TestRequests = TestRequests.concat(MainThreadTestRequests);
|
|
}
|
|
|
|
// This code controls moving from one test to another.
|
|
var TestCounter = {
|
|
testComplete: function() {
|
|
// Allow for the possibility there are other events coming.
|
|
self.setTimeout(function() {
|
|
TestCounter.next();
|
|
}, 5000);
|
|
},
|
|
|
|
next: function() {
|
|
var test = TestRequests.shift();
|
|
|
|
if (test) {
|
|
test.startXHR();
|
|
}
|
|
else {
|
|
message("done");
|
|
}
|
|
}
|
|
};
|
|
|
|
self.addEventListener("message", function (event) {
|
|
if (event.data == "start") {
|
|
TestCounter.next();
|
|
}
|
|
});
|