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 let internal = chan.QueryInterface(Ci.nsIHttpChannelInternal);
56 internal.setWaitForHTTPSSVCRecord();
57 chan.asyncOpen(new ChannelListener(finish, null, flags));
61 add_task(async function viewsourceExternalProtocol() {
63 () => makeChan(`view-source:${testScheme}:foo.example.com`),
64 /NS_ERROR_MALFORMED_URI/
68 add_task(async function viewsourceExternalProtocolRedirect() {
69 let httpserv = new HttpServer();
70 httpserv.registerPathHandler("/", function handler(metadata, response) {
71 response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
72 response.setHeader("Location", `${testScheme}:foo@bar.com`, false);
75 response.bodyOutputStream.write(body, body.length);
80 `view-source:http://127.0.0.1:${httpserv.identity.primaryPort}/`
82 let [req] = await channelOpenPromise(chan, CL_EXPECT_FAILURE);
83 Assert.equal(req.status, Cr.NS_ERROR_MALFORMED_URI);
84 await httpserv.stop();