1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 const { HttpServer
} = ChromeUtils
.importESModule(
10 "resource://testing-common/httpd.sys.mjs"
12 let hserv
= Cc
["@mozilla.org/uriloader/handler-service;1"].getService(
16 const testScheme
= "x-moz-test";
19 var handler
= Cc
["@mozilla.org/uriloader/web-handler-app;1"].createInstance(
22 handler
.name
= testScheme
;
23 handler
.uriTemplate
= "http://test.mozilla.org/%s";
26 "@mozilla.org/uriloader/external-protocol-service;1"
27 ].getService(Ci
.nsIExternalProtocolService
);
28 handlerInfo
= extps
.getProtocolHandlerInfo(testScheme
);
29 handlerInfo
.possibleApplicationHandlers
.appendElement(handler
);
31 hserv
.store(handlerInfo
);
32 Assert
.ok(extps
.externalProtocolHandlerExists(testScheme
));
36 registerCleanupFunction(() => {
37 hserv
.remove(handlerInfo
);
40 function makeChan(url
) {
41 let chan
= NetUtil
.newChannel({
43 loadUsingSystemPrincipal
: true,
44 contentPolicyType
: Ci
.nsIContentPolicy
.TYPE_DOCUMENT
,
45 }).QueryInterface(Ci
.nsIHttpChannel
);
46 chan
.loadFlags
= Ci
.nsIChannel
.LOAD_INITIAL_DOCUMENT_URI
;
50 function channelOpenPromise(chan
, flags
) {
51 return new Promise(resolve
=> {
52 function finish(req
, buffer
) {
53 resolve([req
, buffer
]);
55 chan
.asyncOpen(new ChannelListener(finish
, null, flags
));
59 add_task(async
function viewsourceExternalProtocol() {
61 () => makeChan(`view-source:${testScheme}:foo.example.com`),
62 /NS_ERROR_MALFORMED_URI/
66 add_task(async
function viewsourceExternalProtocolRedirect() {
67 let httpserv
= new HttpServer();
68 httpserv
.registerPathHandler("/", function handler(metadata
, response
) {
69 response
.setStatusLine(metadata
.httpVersion
, 301, "Moved Permanently");
70 response
.setHeader("Location", `${testScheme}:foo@bar.com`, false);
73 response
.bodyOutputStream
.write(body
, body
.length
);
78 `view-source:http://127.0.0.1:${httpserv.identity.primaryPort}/`
80 let [req
] = await
channelOpenPromise(chan
, CL_EXPECT_FAILURE
);
81 Assert
.equal(req
.status
, Cr
.NS_ERROR_MALFORMED_URI
);
82 await httpserv
.stop();