netwerk/test/unit/test_httpcancel.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 // This file ensures that canceling a channel early does not
michael@0 2 // send the request to the server (bug 350790)
michael@0 3 //
michael@0 4 // I've also shoehorned in a test that ENSURE_CALLED_BEFORE_CONNECT works as
michael@0 5 // expected: see comments that start with ENSURE_CALLED_BEFORE_CONNECT:
michael@0 6
michael@0 7 Cu.import("resource://testing-common/httpd.js");
michael@0 8
michael@0 9 var ios = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 10 .getService(Components.interfaces.nsIIOService);
michael@0 11 var observer = {
michael@0 12 QueryInterface: function eventsink_qi(iid) {
michael@0 13 if (iid.equals(Components.interfaces.nsISupports) ||
michael@0 14 iid.equals(Components.interfaces.nsIObserver))
michael@0 15 return this;
michael@0 16 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 17 },
michael@0 18
michael@0 19 observe: function(subject, topic, data) {
michael@0 20 subject = subject.QueryInterface(Components.interfaces.nsIRequest);
michael@0 21 subject.cancel(Components.results.NS_BINDING_ABORTED);
michael@0 22
michael@0 23 // ENSURE_CALLED_BEFORE_CONNECT: setting values should still work
michael@0 24 try {
michael@0 25 subject.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 26 currentReferrer = subject.getRequestHeader("Referer");
michael@0 27 do_check_eq(currentReferrer, "http://site1.com/");
michael@0 28 var uri = ios.newURI("http://site2.com", null, null);
michael@0 29 subject.referrer = uri;
michael@0 30 } catch (ex) {
michael@0 31 do_throw("Exception: " + ex);
michael@0 32 }
michael@0 33
michael@0 34 var obs = Components.classes["@mozilla.org/observer-service;1"].getService();
michael@0 35 obs = obs.QueryInterface(Components.interfaces.nsIObserverService);
michael@0 36 obs.removeObserver(observer, "http-on-modify-request");
michael@0 37 }
michael@0 38 };
michael@0 39
michael@0 40 var listener = {
michael@0 41 onStartRequest: function test_onStartR(request, ctx) {
michael@0 42 do_check_eq(request.status, Components.results.NS_BINDING_ABORTED);
michael@0 43
michael@0 44 // ENSURE_CALLED_BEFORE_CONNECT: setting referrer should now fail
michael@0 45 try {
michael@0 46 request.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 47 currentReferrer = request.getRequestHeader("Referer");
michael@0 48 do_check_eq(currentReferrer, "http://site2.com/");
michael@0 49 var uri = ios.newURI("http://site3.com/", null, null);
michael@0 50
michael@0 51 // Need to set NECKO_ERRORS_ARE_FATAL=0 else we'll abort process
michael@0 52 var env = Components.classes["@mozilla.org/process/environment;1"].
michael@0 53 getService(Components.interfaces.nsIEnvironment);
michael@0 54 env.set("NECKO_ERRORS_ARE_FATAL", "0");
michael@0 55 // we expect setting referrer to fail
michael@0 56 try {
michael@0 57 request.referrer = uri;
michael@0 58 do_throw("Error should have been thrown before getting here");
michael@0 59 } catch (ex) { }
michael@0 60 } catch (ex) {
michael@0 61 do_throw("Exception: " + ex);
michael@0 62 }
michael@0 63 },
michael@0 64
michael@0 65 onDataAvailable: function test_ODA() {
michael@0 66 do_throw("Should not get any data!");
michael@0 67 },
michael@0 68
michael@0 69 onStopRequest: function test_onStopR(request, ctx, status) {
michael@0 70 httpserv.stop(do_test_finished);
michael@0 71 }
michael@0 72 };
michael@0 73
michael@0 74 function makeChan(url) {
michael@0 75 var chan = ios.newChannel(url, null, null)
michael@0 76 .QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 77
michael@0 78 // ENSURE_CALLED_BEFORE_CONNECT: set original value
michael@0 79 var uri = ios.newURI("http://site1.com", null, null);
michael@0 80 chan.referrer = uri;
michael@0 81
michael@0 82 return chan;
michael@0 83 }
michael@0 84
michael@0 85 var httpserv = null;
michael@0 86
michael@0 87 function execute_test() {
michael@0 88 var chan = makeChan("http://localhost:" +
michael@0 89 httpserv.identity.primaryPort + "/failtest");
michael@0 90
michael@0 91 var obs = Components.classes["@mozilla.org/observer-service;1"].getService();
michael@0 92 obs = obs.QueryInterface(Components.interfaces.nsIObserverService);
michael@0 93 obs.addObserver(observer, "http-on-modify-request", false);
michael@0 94
michael@0 95 chan.asyncOpen(listener, null);
michael@0 96 }
michael@0 97
michael@0 98 function run_test() {
michael@0 99 httpserv = new HttpServer();
michael@0 100 httpserv.registerPathHandler("/failtest", failtest);
michael@0 101 httpserv.start(-1);
michael@0 102
michael@0 103 execute_test();
michael@0 104
michael@0 105 do_test_pending();
michael@0 106 }
michael@0 107
michael@0 108 // PATHS
michael@0 109
michael@0 110 // /failtest
michael@0 111 function failtest(metadata, response) {
michael@0 112 do_throw("This should not be reached");
michael@0 113 }

mercurial