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.import("resource://testing-common/httpd.js");
10 let hserv = Cc["@mozilla.org/uriloader/handler-service;1"].getService(
14 const testScheme = "x-moz-test";
17 var handler = Cc["@mozilla.org/uriloader/web-handler-app;1"].createInstance(
20 handler.name = testScheme;
21 handler.uriTemplate = "http://test.mozilla.org/%s";
24 "@mozilla.org/uriloader/external-protocol-service;1"
25 ].getService(Ci.nsIExternalProtocolService);
26 handlerInfo = extps.getProtocolHandlerInfo(testScheme);
27 handlerInfo.possibleApplicationHandlers.appendElement(handler);
29 hserv.store(handlerInfo);
30 Assert.ok(extps.externalProtocolHandlerExists(testScheme));
34 registerCleanupFunction(() => {
35 hserv.remove(handlerInfo);
38 function makeChan(url) {
39 let chan = NetUtil.newChannel({
41 loadUsingSystemPrincipal: true,
42 contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
43 }).QueryInterface(Ci.nsIHttpChannel);
44 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
48 function channelOpenPromise(chan, flags) {
49 return new Promise(resolve => {
50 function finish(req, buffer) {
51 resolve([req, buffer]);
53 let internal = chan.QueryInterface(Ci.nsIHttpChannelInternal);
54 internal.setWaitForHTTPSSVCRecord();
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();