test_bug455311.js (3606B)
1 "use strict"; 2 3 function getUrlLinkFile() { 4 if (mozinfo.os == "win") { 5 return do_get_file("test_link.url"); 6 } 7 if (mozinfo.os == "linux") { 8 return do_get_file("test_link.desktop"); 9 } 10 do_throw("Unexpected platform"); 11 return null; 12 } 13 14 const ios = Services.io; 15 16 function NotificationCallbacks(origURI, newURI) { 17 this._origURI = origURI; 18 this._newURI = newURI; 19 } 20 NotificationCallbacks.prototype = { 21 QueryInterface: ChromeUtils.generateQI([ 22 "nsIInterfaceRequestor", 23 "nsIChannelEventSink", 24 ]), 25 getInterface(iid) { 26 return this.QueryInterface(iid); 27 }, 28 asyncOnChannelRedirect(oldChan, newChan) { 29 Assert.equal(oldChan.URI.spec, this._origURI.spec); 30 Assert.equal(oldChan.URI, this._origURI); 31 Assert.equal(oldChan.originalURI.spec, this._origURI.spec); 32 Assert.equal(oldChan.originalURI, this._origURI); 33 Assert.equal(newChan.originalURI.spec, this._newURI.spec); 34 Assert.equal(newChan.originalURI, newChan.URI); 35 Assert.equal(newChan.URI.spec, this._newURI.spec); 36 throw Components.Exception("", Cr.NS_ERROR_ABORT); 37 }, 38 }; 39 40 function RequestObserver(origURI, newURI, nextTest) { 41 this._origURI = origURI; 42 this._newURI = newURI; 43 this._nextTest = nextTest; 44 } 45 RequestObserver.prototype = { 46 QueryInterface: ChromeUtils.generateQI([ 47 "nsIRequestObserver", 48 "nsIStreamListener", 49 ]), 50 onStartRequest(req) { 51 var chan = req.QueryInterface(Ci.nsIChannel); 52 Assert.equal(chan.URI.spec, this._origURI.spec); 53 Assert.equal(chan.URI, this._origURI); 54 Assert.equal(chan.originalURI.spec, this._origURI.spec); 55 Assert.equal(chan.originalURI, this._origURI); 56 }, 57 onDataAvailable() { 58 do_throw("Unexpected call to onDataAvailable"); 59 }, 60 onStopRequest(req, status) { 61 var chan = req.QueryInterface(Ci.nsIChannel); 62 try { 63 Assert.equal(chan.URI.spec, this._origURI.spec); 64 Assert.equal(chan.URI, this._origURI); 65 Assert.equal(chan.originalURI.spec, this._origURI.spec); 66 Assert.equal(chan.originalURI, this._origURI); 67 Assert.equal(status, Cr.NS_ERROR_ABORT); 68 Assert.ok(!chan.isPending()); 69 } catch (e) {} 70 this._nextTest(); 71 }, 72 }; 73 74 function test_cancel(linkURI, newURI) { 75 var chan = NetUtil.newChannel({ 76 uri: linkURI, 77 loadUsingSystemPrincipal: true, 78 }); 79 Assert.equal(chan.URI, linkURI); 80 Assert.equal(chan.originalURI, linkURI); 81 chan.asyncOpen(new RequestObserver(linkURI, newURI, do_test_finished)); 82 Assert.ok(chan.isPending()); 83 chan.cancel(Cr.NS_ERROR_ABORT); 84 Assert.ok(chan.isPending()); 85 } 86 87 function test_channel(linkURI, newURI) { 88 const chan = NetUtil.newChannel({ 89 uri: linkURI, 90 loadUsingSystemPrincipal: true, 91 }); 92 Assert.equal(chan.URI, linkURI); 93 Assert.equal(chan.originalURI, linkURI); 94 chan.notificationCallbacks = new NotificationCallbacks(linkURI, newURI); 95 chan.asyncOpen( 96 new RequestObserver(linkURI, newURI, () => test_cancel(linkURI, newURI)) 97 ); 98 Assert.ok(chan.isPending()); 99 } 100 101 function run_test() { 102 if (mozinfo.os != "win" && mozinfo.os != "linux") { 103 return; 104 } 105 106 let link = getUrlLinkFile(); 107 let linkURI; 108 if (link.isSymlink()) { 109 let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); 110 file.initWithPath(link.target); 111 linkURI = ios.newFileURI(file); 112 } else { 113 linkURI = ios.newFileURI(link); 114 } 115 116 do_test_pending(); 117 test_channel(linkURI, ios.newURI("http://www.mozilla.org/")); 118 119 if (mozinfo.os != "win") { 120 return; 121 } 122 123 link = do_get_file("test_link.lnk"); 124 test_channel( 125 ios.newFileURI(link), 126 ios.newURI("file:///Z:/moz-nonexistent/index.html") 127 ); 128 }