Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_http_server_timing.js
blob7ade62edef4055bb6aecc5ea1b5414e4dc66acda
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/. */
7 /* eslint-env node */
9 "use strict";
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" },
20 let port;
22 let server;
23 add_task(async function setup() {
24 server = new NodeHTTPServer();
25 await server.start();
26 registerCleanupFunction(async () => {
27 await server.stop();
28 });
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");
34 res.setHeader(
35 "Server-Timing",
36 "metric; dur=123.4; desc=description, metric2; dur=456.78; desc=description1"
38 res.write("data reached");
39 res.addTrailers({
40 "Server-Timing":
41 "metric3; dur=789.11; desc=description2, metric4; dur=1112.13; desc=description3",
42 });
43 res.end();
44 });
45 port = server.port();
46 });
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,
53 });
54 await new Promise(resolve => {
55 chan.asyncOpen(
56 new ChannelListener(request => {
57 let channel = request.QueryInterface(Ci.nsITimedChannel);
58 let headers = channel.serverTiming.QueryInterface(Ci.nsIArray);
59 ok(headers.length);
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));
70 resolve();
71 }, null)
73 });
74 });
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");
81 });
83 let chan = NetUtil.newChannel({
84 uri: `http://example.org:${port}/`,
85 loadUsingSystemPrincipal: true,
86 });
87 await new Promise(resolve => {
88 chan.asyncOpen(
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);
93 resolve();
94 }, null)
96 });
97 });