1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
6 let { OHTTPConfigManager } = ChromeUtils.importESModule(
7 "resource://gre/modules/OHTTPConfigManager.sys.mjs"
10 const { HttpServer } = ChromeUtils.importESModule(
11 "resource://testing-common/httpd.sys.mjs"
15 let gValidRequestCount = 0;
16 let gFickleIsWorking = true;
18 add_setup(async function () {
19 gHttpServer = new HttpServer();
20 let invalidHandler = (req, res) => {
21 res.setStatusLine(req.httpVersion, 500, "Oh no, it broke");
22 res.write("Uh oh, it broke.");
24 let validHandler = (req, res) => {
25 res.setHeader("Content-Type", "application/ohttp-keys");
30 gHttpServer.registerPathHandler("/.wellknown/invalid", invalidHandler);
31 gHttpServer.registerPathHandler("/.wellknown/valid", validHandler);
33 gHttpServer.registerPathHandler("/.wellknown/fickle", (req, res) => {
34 if (gFickleIsWorking) {
35 return validHandler(req, res);
37 return invalidHandler(req, res);
40 gHttpServer.start(-1);
43 function getLocalURL(path) {
44 return `http://localhost:${gHttpServer.identity.primaryPort}/.wellknown/${path}`;
47 add_task(async function test_broken_url_returns_null() {
48 Assert.equal(await OHTTPConfigManager.get(getLocalURL("invalid")), null);
51 add_task(async function test_working_url_returns_data() {
53 await OHTTPConfigManager.get(getLocalURL("valid")),
54 new TextEncoder().encode("1234")
58 add_task(async function test_we_only_request_once() {
60 await OHTTPConfigManager.get(getLocalURL("valid")),
61 new TextEncoder().encode("1234")
63 let oldRequestCount = gValidRequestCount;
66 await OHTTPConfigManager.get(getLocalURL("valid")),
67 new TextEncoder().encode("1234")
72 "Shouldn't have made another request."
76 add_task(async function test_maxAge_forces_refresh() {
78 await OHTTPConfigManager.get(getLocalURL("valid")),
79 new TextEncoder().encode("1234")
81 let oldRequestCount = gValidRequestCount;
84 await OHTTPConfigManager.get(getLocalURL("valid"), { maxAge: 0 }),
85 new TextEncoder().encode("1234")
90 "Should have made another request due to maxAge."
94 add_task(async function test_maxAge_handling_of_invalid_requests() {
96 await OHTTPConfigManager.get(getLocalURL("fickle")),
97 new TextEncoder().encode("1234")
100 gFickleIsWorking = false;
103 await OHTTPConfigManager.get(getLocalURL("fickle"), { maxAge: 0 }),
108 await OHTTPConfigManager.get(getLocalURL("fickle")),
109 new TextEncoder().encode("1234"),
110 "Should still have the cached config if no max age is passed."