Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_SuperfluousAuth.js
blob581ae8bf3c81ba998f181aca86f5f6db293b9c4e
1 /*
3 Create two http requests with the same URL in which has a user name. We allow
4 first http request to be loaded and saved in the cache, so the second request
5 will be served from the cache. However, we disallow loading by returning 1
6 in the prompt service. In the end, the second request will be failed.
8 */
10 "use strict";
12 const { HttpServer } = ChromeUtils.importESModule(
13 "resource://testing-common/httpd.sys.mjs"
16 const { MockRegistrar } = ChromeUtils.importESModule(
17 "resource://testing-common/MockRegistrar.sys.mjs"
20 var httpProtocolHandler = Cc[
21 "@mozilla.org/network/protocol;1?name=http"
22 ].getService(Ci.nsIHttpProtocolHandler);
24 ChromeUtils.defineLazyGetter(this, "URL", function () {
25 return "http://foo@localhost:" + httpServer.identity.primaryPort;
26 });
28 var httpServer = null;
30 const gMockPromptService = {
31 firstTimeCalled: false,
32 confirmExBC() {
33 if (!this.firstTimeCalled) {
34 this.firstTimeCalled = true;
35 return 0;
38 return 1;
41 QueryInterface: ChromeUtils.generateQI(["nsIPromptService"]),
44 var gMockPromptServiceCID = MockRegistrar.register(
45 "@mozilla.org/prompter;1",
46 gMockPromptService
49 registerCleanupFunction(() => {
50 MockRegistrar.unregister(gMockPromptServiceCID);
51 });
53 function makeChan(uri) {
54 let chan = NetUtil.newChannel({
55 uri,
56 loadUsingSystemPrincipal: true,
57 }).QueryInterface(Ci.nsIHttpChannel);
58 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
59 return chan;
62 const responseBody = "body";
64 function contentHandler(metadata, response) {
65 response.setHeader("Content-Type", "text/plain");
66 response.setHeader("ETag", "Just testing");
67 response.setHeader("Cache-Control", "max-age=99999");
68 response.setHeader("Content-Length", "" + responseBody.length);
69 response.bodyOutputStream.write(responseBody, responseBody.length);
72 function run_test() {
73 do_get_profile();
75 Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
76 Services.prefs.setBoolPref("network.auth.confirmAuth.enabled", true);
78 httpServer = new HttpServer();
79 httpServer.registerPathHandler("/content", contentHandler);
80 httpServer.start(-1);
82 httpProtocolHandler.EnsureHSTSDataReady().then(function () {
83 var chan1 = makeChan(URL + "/content");
84 chan1.asyncOpen(new ChannelListener(firstTimeThrough, null));
85 var chan2 = makeChan(URL + "/content");
86 chan2.asyncOpen(
87 new ChannelListener(secondTimeThrough, null, CL_EXPECT_FAILURE)
89 });
91 do_test_pending();
94 function firstTimeThrough(request, buffer) {
95 Assert.equal(buffer, responseBody);
96 Assert.ok(gMockPromptService.firstTimeCalled, "Prompt service invoked");
99 function secondTimeThrough(request) {
100 Assert.equal(request.status, Cr.NS_ERROR_SUPERFLUOS_AUTH);
101 httpServer.stop(do_test_finished);