test_header_Server_Timing.js (2170B)
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 // 6 // HTTP Server-Timing header test 7 // 8 9 "use strict"; 10 11 function make_and_open_channel(url, callback) { 12 let chan = NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true }); 13 chan.asyncOpen(new ChannelListener(callback, null, CL_ALLOW_UNKNOWN_CL)); 14 } 15 16 var responseServerTiming = [ 17 { metric: "metric", duration: "123.4", description: "description" }, 18 { metric: "metric2", duration: "456.78", description: "description1" }, 19 ]; 20 var trailerServerTiming = [ 21 { metric: "metric3", duration: "789.11", description: "description2" }, 22 { metric: "metric4", duration: "1112.13", description: "description3" }, 23 ]; 24 25 function run_test() { 26 do_test_pending(); 27 28 // Set up to allow the cert presented by the server 29 do_get_profile(); 30 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService( 31 Ci.nsIX509CertDB 32 ); 33 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u"); 34 35 Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com"); 36 registerCleanupFunction(() => { 37 Services.prefs.clearUserPref("network.dns.localDomains"); 38 }); 39 40 var serverPort = Services.env.get("MOZHTTP2_PORT"); 41 make_and_open_channel( 42 "https://foo.example.com:" + serverPort + "/server-timing", 43 readServerContent 44 ); 45 } 46 47 function checkServerTimingContent(headers) { 48 var expectedResult = responseServerTiming.concat(trailerServerTiming); 49 Assert.equal(headers.length, expectedResult.length); 50 51 for (var i = 0; i < expectedResult.length; i++) { 52 let header = headers.queryElementAt(i, Ci.nsIServerTiming); 53 Assert.equal(header.name, expectedResult[i].metric); 54 Assert.equal(header.description, expectedResult[i].description); 55 Assert.equal(header.duration, parseFloat(expectedResult[i].duration)); 56 } 57 } 58 59 function readServerContent(request) { 60 let channel = request.QueryInterface(Ci.nsITimedChannel); 61 let headers = channel.serverTiming.QueryInterface(Ci.nsIArray); 62 checkServerTimingContent(headers); 63 do_test_finished(); 64 }