1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/unit/test_error_codes.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,63 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.7 + */ 1.8 + 1.9 +var gExpectedStatus = null; 1.10 +var gNextTestFunc = null; 1.11 + 1.12 +var asyncXHR = { 1.13 + load: function() { 1.14 + var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] 1.15 + .createInstance(Components.interfaces.nsIXMLHttpRequest); 1.16 + request.open("GET", "http://localhost:4444/test_error_code.xml", true); 1.17 + 1.18 + var self = this; 1.19 + request.addEventListener("error", function(event) { self.onError(event); }, false); 1.20 + request.send(null); 1.21 + }, 1.22 + onError: function doAsyncRequest_onError(event) { 1.23 + var request = event.target.channel.QueryInterface(Components.interfaces.nsIRequest); 1.24 + do_check_eq(request.status, gExpectedStatus); 1.25 + gNextTestFunc(); 1.26 + } 1.27 +} 1.28 + 1.29 +function run_test() { 1.30 + do_test_pending(); 1.31 + do_timeout(0, run_test_pt1); 1.32 +} 1.33 + 1.34 +// network offline 1.35 +function run_test_pt1() { 1.36 + var ioService = Components.classes["@mozilla.org/network/io-service;1"] 1.37 + .getService(Components.interfaces.nsIIOService); 1.38 + 1.39 + try { 1.40 + ioService.manageOfflineStatus = false; 1.41 + } 1.42 + catch (e) { 1.43 + } 1.44 + ioService.offline = true; 1.45 + 1.46 + gExpectedStatus = Components.results.NS_ERROR_OFFLINE; 1.47 + gNextTestFunc = run_test_pt2; 1.48 + dump("Testing error returned by async XHR when the network is offline\n"); 1.49 + asyncXHR.load(); 1.50 +} 1.51 + 1.52 +// connection refused 1.53 +function run_test_pt2() { 1.54 + var ioService = Components.classes["@mozilla.org/network/io-service;1"] 1.55 + .getService(Components.interfaces.nsIIOService); 1.56 + ioService.offline = false; 1.57 + 1.58 + gExpectedStatus = Components.results.NS_ERROR_CONNECTION_REFUSED; 1.59 + gNextTestFunc = end_test; 1.60 + dump("Testing error returned by aync XHR when the connection is refused\n"); 1.61 + asyncXHR.load(); 1.62 +} 1.63 + 1.64 +function end_test() { 1.65 + do_test_finished(); 1.66 +}