content/base/test/unit/test_error_codes.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4 */
michael@0 5
michael@0 6 var gExpectedStatus = null;
michael@0 7 var gNextTestFunc = null;
michael@0 8
michael@0 9 var asyncXHR = {
michael@0 10 load: function() {
michael@0 11 var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
michael@0 12 .createInstance(Components.interfaces.nsIXMLHttpRequest);
michael@0 13 request.open("GET", "http://localhost:4444/test_error_code.xml", true);
michael@0 14
michael@0 15 var self = this;
michael@0 16 request.addEventListener("error", function(event) { self.onError(event); }, false);
michael@0 17 request.send(null);
michael@0 18 },
michael@0 19 onError: function doAsyncRequest_onError(event) {
michael@0 20 var request = event.target.channel.QueryInterface(Components.interfaces.nsIRequest);
michael@0 21 do_check_eq(request.status, gExpectedStatus);
michael@0 22 gNextTestFunc();
michael@0 23 }
michael@0 24 }
michael@0 25
michael@0 26 function run_test() {
michael@0 27 do_test_pending();
michael@0 28 do_timeout(0, run_test_pt1);
michael@0 29 }
michael@0 30
michael@0 31 // network offline
michael@0 32 function run_test_pt1() {
michael@0 33 var ioService = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 34 .getService(Components.interfaces.nsIIOService);
michael@0 35
michael@0 36 try {
michael@0 37 ioService.manageOfflineStatus = false;
michael@0 38 }
michael@0 39 catch (e) {
michael@0 40 }
michael@0 41 ioService.offline = true;
michael@0 42
michael@0 43 gExpectedStatus = Components.results.NS_ERROR_OFFLINE;
michael@0 44 gNextTestFunc = run_test_pt2;
michael@0 45 dump("Testing error returned by async XHR when the network is offline\n");
michael@0 46 asyncXHR.load();
michael@0 47 }
michael@0 48
michael@0 49 // connection refused
michael@0 50 function run_test_pt2() {
michael@0 51 var ioService = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 52 .getService(Components.interfaces.nsIIOService);
michael@0 53 ioService.offline = false;
michael@0 54
michael@0 55 gExpectedStatus = Components.results.NS_ERROR_CONNECTION_REFUSED;
michael@0 56 gNextTestFunc = end_test;
michael@0 57 dump("Testing error returned by aync XHR when the connection is refused\n");
michael@0 58 asyncXHR.load();
michael@0 59 }
michael@0 60
michael@0 61 function end_test() {
michael@0 62 do_test_finished();
michael@0 63 }

mercurial