3 const { HttpServer
} = ChromeUtils
.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
7 ChromeUtils
.defineLazyGetter(this, "URL", function () {
8 return "http://localhost:" + httpServer
.identity
.primaryPort
;
11 var httpServer
= null;
12 // Need to randomize, because apparently no one clears our cache
13 var randomPath
= "/redirect/" + Math
.random();
15 ChromeUtils
.defineLazyGetter(this, "randomURI", function () {
16 return URL
+ randomPath
;
19 function make_channel(url
) {
20 return NetUtil
.newChannel({ uri
: url
, loadUsingSystemPrincipal
: true });
23 const responseBody
= "response body";
25 function redirectHandler(metadata
, response
) {
26 response
.setStatusLine(metadata
.httpVersion
, 301, "Moved");
27 response
.setHeader("Location", URL
+ "/content", false);
30 function contentHandler(metadata
, response
) {
31 response
.setHeader("Content-Type", "text/plain");
32 response
.bodyOutputStream
.write(responseBody
, responseBody
.length
);
35 let ChannelEventSink2
= {
36 _classDescription
: "WebRequest channel event sink",
37 _classID
: Components
.ID("115062f8-92f1-11e5-8b7f-08001110f7ec"),
38 _contractID
: "@mozilla.org/webrequest/channel-event-sink;1",
40 QueryInterface
: ChromeUtils
.generateQI(["nsIChannelEventSink", "nsIFactory"]),
44 .QueryInterface(Ci
.nsIComponentRegistrar
)
47 this._classDescription
,
54 Services
.catMan
.addCategoryEntry(
55 "net-channel-event-sinks",
64 Services
.catMan
.deleteCategoryEntry(
65 "net-channel-event-sinks",
71 // nsIChannelEventSink implementation
72 asyncOnChannelRedirect(oldChannel
, newChannel
, flags
, redirectCallback
) {
73 // Abort the redirection
74 redirectCallback
.onRedirectVerifyCallback(Cr
.NS_ERROR_NO_INTERFACE
);
77 // nsIFactory implementation
79 return this.QueryInterface(iid
);
83 add_task(async
function test_redirect_veto() {
84 httpServer
= new HttpServer();
85 httpServer
.registerPathHandler(randomPath
, redirectHandler
);
86 httpServer
.registerPathHandler("/content", contentHandler
);
89 ChannelEventSink2
.init();
90 ChannelEventSink2
.register();
92 let chan
= make_channel(randomURI
);
93 let [req
, buff
] = await
new Promise(resolve
=>
95 new ChannelListener((aReq
, aBuff
) => resolve([aReq
, aBuff
], null))
98 Assert
.equal(buff
, "");
99 Assert
.equal(req
.status
, Cr
.NS_OK
);
100 await httpServer
.stop();
101 ChannelEventSink2
.unregister();