test_http_server_timing.js (3364B)
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/. */ 6 7 /* eslint-env node */ 8 9 "use strict"; 10 11 const { NodeHTTPServer } = ChromeUtils.importESModule( 12 "resource://testing-common/NodeServer.sys.mjs" 13 ); 14 15 const responseServerTiming = [ 16 { metric: "metric", duration: "123.4", description: "description" }, 17 { metric: "metric2", duration: "456.78", description: "description1" }, 18 ]; 19 const trailerServerTiming = [ 20 { metric: "metric3", duration: "789.11", description: "description2" }, 21 { metric: "metric4", duration: "1112.13", description: "description3" }, 22 ]; 23 24 let port; 25 26 let server; 27 add_task(async function setup() { 28 server = new NodeHTTPServer(); 29 await server.start(); 30 registerCleanupFunction(async () => { 31 await server.stop(); 32 }); 33 server.registerPathHandler("/", (req, res) => { 34 res.setHeader("Content-Type", "text/plain"); 35 res.setHeader("Content-Length", "12"); 36 res.setHeader("Transfer-Encoding", "chunked"); 37 res.setHeader("Trailer", "Server-Timing"); 38 res.setHeader( 39 "Server-Timing", 40 "metric; dur=123.4; desc=description, metric2; dur=456.78; desc=description1" 41 ); 42 res.write("data reached"); 43 res.addTrailers({ 44 "Server-Timing": 45 "metric3; dur=789.11; desc=description2, metric4; dur=1112.13; desc=description3", 46 }); 47 res.end(); 48 }); 49 port = server.port(); 50 }); 51 52 // Test that secure origins can use server-timing, even with plain http 53 add_task(async function test_localhost_origin() { 54 let chan = NetUtil.newChannel({ 55 uri: `http://localhost:${port}/`, 56 loadUsingSystemPrincipal: true, 57 }); 58 await new Promise(resolve => { 59 chan.asyncOpen( 60 new ChannelListener(request => { 61 let channel = request.QueryInterface(Ci.nsITimedChannel); 62 let headers = channel.serverTiming.QueryInterface(Ci.nsIArray); 63 ok(headers.length); 64 65 let expectedResult = responseServerTiming.concat(trailerServerTiming); 66 Assert.equal(headers.length, expectedResult.length); 67 68 for (let i = 0; i < expectedResult.length; i++) { 69 let header = headers.queryElementAt(i, Ci.nsIServerTiming); 70 Assert.equal(header.name, expectedResult[i].metric); 71 Assert.equal(header.description, expectedResult[i].description); 72 Assert.equal(header.duration, parseFloat(expectedResult[i].duration)); 73 } 74 resolve(); 75 }, null) 76 ); 77 }); 78 }); 79 80 // Test that insecure origins can't use server timing. 81 add_task(async function test_http_non_localhost() { 82 Services.prefs.setBoolPref("network.dns.native-is-localhost", true); 83 registerCleanupFunction(async () => { 84 Services.prefs.clearUserPref("network.dns.native-is-localhost"); 85 }); 86 87 let chan = NetUtil.newChannel({ 88 uri: `http://example.org:${port}/`, 89 loadUsingSystemPrincipal: true, 90 }); 91 await new Promise(resolve => { 92 chan.asyncOpen( 93 new ChannelListener(request => { 94 let channel = request.QueryInterface(Ci.nsITimedChannel); 95 let headers = channel.serverTiming.QueryInterface(Ci.nsIArray); 96 Assert.equal(headers.length, 0); 97 resolve(); 98 }, null) 99 ); 100 }); 101 });