test_error_codes.js (1934B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 */ 5 6 var prefs = Services.prefs; 7 prefs.setBoolPref( 8 "network.proxy.testing_localhost_is_secure_when_hijacked", 9 false 10 ); 11 12 function asyncXHR(expectedStatus, nextTestFunc) { 13 var xhr = new XMLHttpRequest(); 14 xhr.open("GET", "http://localhost:4444/test_error_code.xml", true); 15 16 var sawError = false; 17 xhr.addEventListener("loadend", function doAsyncRequest_onLoad() { 18 Assert.ok(sawError, "Should have received an error"); 19 nextTestFunc(); 20 }); 21 xhr.addEventListener("error", function doAsyncRequest_onError(event) { 22 var request = event.target.channel.QueryInterface(Ci.nsIRequest); 23 Assert.equal(request.status, expectedStatus); 24 sawError = true; 25 }); 26 xhr.send(null); 27 } 28 29 function run_test() { 30 do_test_pending(); 31 do_timeout(0, run_test_pt1); 32 } 33 34 // network offline 35 function run_test_pt1() { 36 try { 37 Services.io.manageOfflineStatus = false; 38 } catch (e) {} 39 Services.io.offline = true; 40 prefs.setBoolPref("network.dns.offline-localhost", false); 41 // We always resolve localhost as it's hardcoded without the following pref: 42 prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true); 43 44 dump("Testing error returned by async XHR when the network is offline\n"); 45 asyncXHR(Cr.NS_ERROR_OFFLINE, run_test_pt2); 46 } 47 48 // connection refused 49 function run_test_pt2() { 50 Services.io.offline = false; 51 prefs.clearUserPref("network.dns.offline-localhost"); 52 prefs.clearUserPref("network.proxy.allow_hijacking_localhost"); 53 54 dump("Testing error returned by aync XHR when the connection is refused\n"); 55 asyncXHR(Cr.NS_ERROR_CONNECTION_REFUSED, end_test); 56 } 57 58 function end_test() { 59 prefs.clearUserPref( 60 "network.proxy.testing_localhost_is_secure_when_hijacked" 61 ); 62 do_test_finished(); 63 }