netwerk/test/unit/test_event_sink.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 // This file tests channel event sinks (bug 315598 et al)
michael@0 2
michael@0 3 Cu.import("resource://testing-common/httpd.js");
michael@0 4
michael@0 5 XPCOMUtils.defineLazyGetter(this, "URL", function() {
michael@0 6 return "http://localhost:" + httpserv.identity.primaryPort;
michael@0 7 });
michael@0 8
michael@0 9 const sinkCID = Components.ID("{14aa4b81-e266-45cb-88f8-89595dece114}");
michael@0 10 const sinkContract = "@mozilla.org/network/unittest/channeleventsink;1";
michael@0 11
michael@0 12 const categoryName = "net-channel-event-sinks";
michael@0 13
michael@0 14 /**
michael@0 15 * This object is both a factory and an nsIChannelEventSink implementation (so, it
michael@0 16 * is de-facto a service). It's also an interface requestor that gives out
michael@0 17 * itself when asked for nsIChannelEventSink.
michael@0 18 */
michael@0 19 var eventsink = {
michael@0 20 QueryInterface: function eventsink_qi(iid) {
michael@0 21 if (iid.equals(Components.interfaces.nsISupports) ||
michael@0 22 iid.equals(Components.interfaces.nsIFactory) ||
michael@0 23 iid.equals(Components.interfaces.nsIChannelEventSink))
michael@0 24 return this;
michael@0 25 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 26 },
michael@0 27 createInstance: function eventsink_ci(outer, iid) {
michael@0 28 if (outer)
michael@0 29 throw Components.results.NS_ERROR_NO_AGGREGATION;
michael@0 30 return this.QueryInterface(iid);
michael@0 31 },
michael@0 32 lockFactory: function eventsink_lockf(lock) {
michael@0 33 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
michael@0 34 },
michael@0 35
michael@0 36 asyncOnChannelRedirect: function eventsink_onredir(oldChan, newChan, flags, callback) {
michael@0 37 // veto
michael@0 38 this.called = true;
michael@0 39 throw Components.results.NS_BINDING_ABORTED;
michael@0 40 },
michael@0 41
michael@0 42 getInterface: function eventsink_gi(iid) {
michael@0 43 if (iid.equals(Components.interfaces.nsIChannelEventSink))
michael@0 44 return this;
michael@0 45 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 46 },
michael@0 47
michael@0 48 called: false
michael@0 49 };
michael@0 50
michael@0 51 var listener = {
michael@0 52 expectSinkCall: true,
michael@0 53
michael@0 54 onStartRequest: function test_onStartR(request, ctx) {
michael@0 55 try {
michael@0 56 // Commenting out this check pending resolution of bug 255119
michael@0 57 //if (Components.isSuccessCode(request.status))
michael@0 58 // do_throw("Channel should have a failure code!");
michael@0 59
michael@0 60 // The current URI must be the original URI, as all redirects have been
michael@0 61 // cancelled
michael@0 62 if (!(request instanceof Components.interfaces.nsIChannel) ||
michael@0 63 !request.URI.equals(request.originalURI))
michael@0 64 do_throw("Wrong URI: Is <" + request.URI.spec + ">, should be <" +
michael@0 65 request.originalURI.spec + ">");
michael@0 66
michael@0 67 if (request instanceof Components.interfaces.nsIHttpChannel) {
michael@0 68 // As we expect a blocked redirect, verify that we have a 3xx status
michael@0 69 do_check_eq(Math.floor(request.responseStatus / 100), 3);
michael@0 70 do_check_eq(request.requestSucceeded, false);
michael@0 71 }
michael@0 72
michael@0 73 do_check_eq(eventsink.called, this.expectSinkCall);
michael@0 74 } catch (e) {
michael@0 75 do_throw("Unexpected exception: " + e);
michael@0 76 }
michael@0 77
michael@0 78 throw Components.results.NS_ERROR_ABORT;
michael@0 79 },
michael@0 80
michael@0 81 onDataAvailable: function test_ODA() {
michael@0 82 do_throw("Should not get any data!");
michael@0 83 },
michael@0 84
michael@0 85 onStopRequest: function test_onStopR(request, ctx, status) {
michael@0 86 if (this._iteration <= 2) {
michael@0 87 run_test_continued();
michael@0 88 } else {
michael@0 89 do_test_pending();
michael@0 90 httpserv.stop(do_test_finished);
michael@0 91 }
michael@0 92 do_test_finished();
michael@0 93 },
michael@0 94
michael@0 95 _iteration: 1
michael@0 96 };
michael@0 97
michael@0 98 function makeChan(url) {
michael@0 99 var ios = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 100 .getService(Components.interfaces.nsIIOService);
michael@0 101 var chan = ios.newChannel(url, null, null)
michael@0 102 .QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 103
michael@0 104 return chan;
michael@0 105 }
michael@0 106
michael@0 107 var httpserv = null;
michael@0 108
michael@0 109 function run_test() {
michael@0 110 httpserv = new HttpServer();
michael@0 111 httpserv.registerPathHandler("/redirect", redirect);
michael@0 112 httpserv.registerPathHandler("/redirectfile", redirectfile);
michael@0 113 httpserv.start(-1);
michael@0 114
michael@0 115 Components.manager.nsIComponentRegistrar.registerFactory(sinkCID,
michael@0 116 "Unit test Event sink", sinkContract, eventsink);
michael@0 117
michael@0 118 // Step 1: Set the callbacks on the listener itself
michael@0 119 var chan = makeChan(URL + "/redirect");
michael@0 120 chan.notificationCallbacks = eventsink;
michael@0 121
michael@0 122 chan.asyncOpen(listener, null);
michael@0 123
michael@0 124 do_test_pending();
michael@0 125 }
michael@0 126
michael@0 127 function run_test_continued() {
michael@0 128 eventsink.called = false;
michael@0 129
michael@0 130 var catMan = Components.classes["@mozilla.org/categorymanager;1"]
michael@0 131 .getService(Components.interfaces.nsICategoryManager);
michael@0 132
michael@0 133 var chan;
michael@0 134 if (listener._iteration == 1) {
michael@0 135 // Step 2: Category entry
michael@0 136 catMan.nsICategoryManager.addCategoryEntry(categoryName, "unit test",
michael@0 137 sinkContract, false, true);
michael@0 138 chan = makeChan(URL + "/redirect")
michael@0 139 } else {
michael@0 140 // Step 3: Global contract id
michael@0 141 catMan.nsICategoryManager.deleteCategoryEntry(categoryName, "unit test",
michael@0 142 false);
michael@0 143 listener.expectSinkCall = false;
michael@0 144 chan = makeChan(URL + "/redirectfile");
michael@0 145 }
michael@0 146
michael@0 147 listener._iteration++;
michael@0 148 chan.asyncOpen(listener, null);
michael@0 149
michael@0 150 do_test_pending();
michael@0 151 }
michael@0 152
michael@0 153 // PATHS
michael@0 154
michael@0 155 // /redirect
michael@0 156 function redirect(metadata, response) {
michael@0 157 response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
michael@0 158 response.setHeader("Location",
michael@0 159 "http://localhost:" + metadata.port + "/",
michael@0 160 false);
michael@0 161
michael@0 162 var body = "Moved\n";
michael@0 163 response.bodyOutputStream.write(body, body.length);
michael@0 164 }
michael@0 165
michael@0 166 // /redirectfile
michael@0 167 function redirectfile(metadata, response) {
michael@0 168 response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
michael@0 169 response.setHeader("Content-Type", "text/plain", false);
michael@0 170 response.setHeader("Location", "file:///etc/", false);
michael@0 171
michael@0 172 var body = "Attempted to move to a file URI, but failed.\n";
michael@0 173 response.bodyOutputStream.write(body, body.length);
michael@0 174 }

mercurial