Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_bug1683176.js
blob9b0fddf7bd28d76e5a9b9a28a15150151ca5d91d
1 // Test bug 1683176
2 //
3 // Summary:
4 //   Test the case when a channel is cancelled when "negotiate" authentication
5 //   is processing.
6 //
8 "use strict";
10 let prefs;
11 let httpserv;
13 const { HttpServer } = ChromeUtils.importESModule(
14   "resource://testing-common/httpd.sys.mjs"
17 ChromeUtils.defineLazyGetter(this, "URL", function () {
18   return "http://localhost:" + httpserv.identity.primaryPort;
19 });
21 function makeChan(url, loadingUrl) {
22   var principal = Services.scriptSecurityManager.createContentPrincipal(
23     Services.io.newURI(loadingUrl),
24     {}
25   );
26   return NetUtil.newChannel({
27     uri: url,
28     loadingPrincipal: principal,
29     securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
30     contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER,
31   });
34 function authHandler(metadata, response) {
35   var body = "blablabla";
37   response.seizePower();
38   response.write("HTTP/1.1 401 Unauthorized\r\n");
39   response.write("WWW-Authenticate: Negotiate\r\n");
40   response.write("WWW-Authenticate: Basic realm=test\r\n");
41   response.write("\r\n");
42   response.write(body);
43   response.finish();
46 function setup() {
47   prefs = Services.prefs;
49   prefs.setIntPref("network.auth.subresource-http-auth-allow", 2);
50   prefs.setStringPref("network.negotiate-auth.trusted-uris", "localhost");
52   httpserv = new HttpServer();
53   httpserv.registerPathHandler("/auth", authHandler);
54   httpserv.start(-1);
57 setup();
58 registerCleanupFunction(async () => {
59   prefs.clearUserPref("network.auth.subresource-http-auth-allow");
60   prefs.clearUserPref("network.negotiate-auth.trusted-uris");
61   await httpserv.stop();
62 });
64 function channelOpenPromise(chan) {
65   return new Promise(resolve => {
66     let topic = "http-on-transaction-suspended-authentication";
67     let observer = {
68       QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
69       observe(aSubject, aTopic, aData) {
70         if (aTopic == topic) {
71           Services.obs.removeObserver(observer, topic);
72           let channel = aSubject.QueryInterface(Ci.nsIChannel);
73           channel.cancel(Cr.NS_BINDING_ABORTED);
74           resolve();
75         }
76       },
77     };
78     Services.obs.addObserver(observer, topic);
80     chan.asyncOpen(new ChannelListener(finish, null, CL_EXPECT_FAILURE));
81     function finish() {
82       resolve();
83     }
84   });
87 add_task(async function testCancelAuthentication() {
88   let chan = makeChan(URL + "/auth", URL);
89   await channelOpenPromise(chan);
90   Assert.equal(chan.status, Cr.NS_BINDING_ABORTED);
91 });