michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: */ michael@0: michael@0: var gExpectedStatus = null; michael@0: var gNextTestFunc = null; michael@0: michael@0: var asyncXHR = { michael@0: load: function() { michael@0: var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] michael@0: .createInstance(Components.interfaces.nsIXMLHttpRequest); michael@0: request.open("GET", "http://localhost:4444/test_error_code.xml", true); michael@0: michael@0: var self = this; michael@0: request.addEventListener("error", function(event) { self.onError(event); }, false); michael@0: request.send(null); michael@0: }, michael@0: onError: function doAsyncRequest_onError(event) { michael@0: var request = event.target.channel.QueryInterface(Components.interfaces.nsIRequest); michael@0: do_check_eq(request.status, gExpectedStatus); michael@0: gNextTestFunc(); michael@0: } michael@0: } michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: do_timeout(0, run_test_pt1); michael@0: } michael@0: michael@0: // network offline michael@0: function run_test_pt1() { michael@0: var ioService = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: michael@0: try { michael@0: ioService.manageOfflineStatus = false; michael@0: } michael@0: catch (e) { michael@0: } michael@0: ioService.offline = true; michael@0: michael@0: gExpectedStatus = Components.results.NS_ERROR_OFFLINE; michael@0: gNextTestFunc = run_test_pt2; michael@0: dump("Testing error returned by async XHR when the network is offline\n"); michael@0: asyncXHR.load(); michael@0: } michael@0: michael@0: // connection refused michael@0: function run_test_pt2() { michael@0: var ioService = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: ioService.offline = false; michael@0: michael@0: gExpectedStatus = Components.results.NS_ERROR_CONNECTION_REFUSED; michael@0: gNextTestFunc = end_test; michael@0: dump("Testing error returned by aync XHR when the connection is refused\n"); michael@0: asyncXHR.load(); michael@0: } michael@0: michael@0: function end_test() { michael@0: do_test_finished(); michael@0: }