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/. */
11 const responseServerTiming
= [
12 { metric
: "metric", duration
: "123.4", description
: "description" },
13 { metric
: "metric2", duration
: "456.78", description
: "description1" },
15 const trailerServerTiming
= [
16 { metric
: "metric3", duration
: "789.11", description
: "description2" },
17 { metric
: "metric4", duration
: "1112.13", description
: "description3" },
23 add_task(async
function setup() {
24 server
= new NodeHTTPServer();
26 registerCleanupFunction(async () => {
29 server
.registerPathHandler("/", (req
, res
) => {
30 res
.setHeader("Content-Type", "text/plain");
31 res
.setHeader("Content-Length", "12");
32 res
.setHeader("Transfer-Encoding", "chunked");
33 res
.setHeader("Trailer", "Server-Timing");
36 "metric; dur=123.4; desc=description, metric2; dur=456.78; desc=description1"
38 res
.write("data reached");
41 "metric3; dur=789.11; desc=description2, metric4; dur=1112.13; desc=description3",
48 // Test that secure origins can use server-timing, even with plain http
49 add_task(async
function test_localhost_origin() {
50 let chan
= NetUtil
.newChannel({
51 uri
: `http://localhost:${port}/`,
52 loadUsingSystemPrincipal
: true,
54 await
new Promise(resolve
=> {
56 new ChannelListener(request
=> {
57 let channel
= request
.QueryInterface(Ci
.nsITimedChannel
);
58 let headers
= channel
.serverTiming
.QueryInterface(Ci
.nsIArray
);
61 let expectedResult
= responseServerTiming
.concat(trailerServerTiming
);
62 Assert
.equal(headers
.length
, expectedResult
.length
);
64 for (let i
= 0; i
< expectedResult
.length
; i
++) {
65 let header
= headers
.queryElementAt(i
, Ci
.nsIServerTiming
);
66 Assert
.equal(header
.name
, expectedResult
[i
].metric
);
67 Assert
.equal(header
.description
, expectedResult
[i
].description
);
68 Assert
.equal(header
.duration
, parseFloat(expectedResult
[i
].duration
));
76 // Test that insecure origins can't use server timing.
77 add_task(async
function test_http_non_localhost() {
78 Services
.prefs
.setBoolPref("network.dns.native-is-localhost", true);
79 registerCleanupFunction(async () => {
80 Services
.prefs
.clearUserPref("network.dns.native-is-localhost");
83 let chan
= NetUtil
.newChannel({
84 uri
: `http://example.org:${port}/`,
85 loadUsingSystemPrincipal
: true,
87 await
new Promise(resolve
=> {
89 new ChannelListener(request
=> {
90 let channel
= request
.QueryInterface(Ci
.nsITimedChannel
);
91 let headers
= channel
.serverTiming
.QueryInterface(Ci
.nsIArray
);
92 Assert
.equal(headers
.length
, 0);