test_prio_header_override_forbid.js (2476B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 add_setup(() => { 6 Services.prefs.setBoolPref("network.http.priority_header.enabled", true); 7 }); 8 9 registerCleanupFunction(() => { 10 Services.prefs.clearUserPref("network.http.priority_header.enabled"); 11 }); 12 13 const { NodeHTTPSServer } = ChromeUtils.importESModule( 14 "resource://testing-common/NodeServer.sys.mjs" 15 ); 16 17 function channelOpenPromise(chan, flags) { 18 return new Promise(resolve => { 19 function finish(req, buffer) { 20 resolve([req, buffer]); 21 } 22 chan.asyncOpen(new ChannelListener(finish, null, flags)); 23 }); 24 } 25 26 async function test_flag_override_priority( 27 prioHeaderValue, 28 listenerPriorityValue, 29 flag 30 ) { 31 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService( 32 Ci.nsIX509CertDB 33 ); 34 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u"); 35 36 let server = new NodeHTTPSServer(); 37 await server.start(); 38 39 await server.registerPathHandler("/test", (req, resp) => { 40 resp.writeHead(200); 41 resp.end(req.headers.priority); 42 }); 43 44 let request = NetUtil.newChannel({ 45 uri: `https://localhost:${server.port()}/test`, 46 loadUsingSystemPrincipal: true, 47 }); 48 let chan = request.QueryInterface(Ci.nsIHttpChannel); 49 50 if (prioHeaderValue !== null) { 51 request.setRequestHeader("Priority", prioHeaderValue, false); 52 } 53 54 // Setting flags should not override if priority is already set 55 let cos = chan.QueryInterface(Ci.nsIClassOfService); 56 cos.addClassFlags(flag); 57 58 let [req, buff] = await channelOpenPromise(chan, CL_ALLOW_UNKNOWN_CL); 59 Assert.equal(req.status, Cr.NS_OK); 60 Assert.equal(buff, listenerPriorityValue); // Check buffer 61 Assert.equal(req.getRequestHeader("Priority"), listenerPriorityValue); 62 await server.stop(); 63 } 64 65 add_task(async function test_prio_header_no_override() { 66 await test_flag_override_priority(null, "u=2", Ci.nsIClassOfService.Leader); 67 }); 68 69 add_task(async function test_prio_header_no_override2() { 70 await test_flag_override_priority(null, "u=4", Ci.nsIClassOfService.Follower); 71 }); 72 73 add_task(async function test_prio_header_override() { 74 await test_flag_override_priority("foo", "foo", Ci.nsIClassOfService.Leader); 75 }); 76 77 add_task(async function test_prio_header_override2() { 78 await test_flag_override_priority( 79 "foo", 80 "foo", 81 Ci.nsIClassOfService.Follower 82 ); 83 });