Skip to content

Instantly share code, notes, and snippets.

@daleharvey
Last active April 26, 2024 21:58
Show Gist options
  • Save daleharvey/a0e9eb3f4fc3795a91b1f0c405c0b27e to your computer and use it in GitHub Desktop.
Save daleharvey/a0e9eb3f4fc3795a91b1f0c405c0b27e to your computer and use it in GitHub Desktop.
"use strict";
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
const { Region } = ChromeUtils.import("resource://gre/modules/Region.jsm");
function useHttpServer() {
let server = new HttpServer();
server.start(-1);
Services.prefs.setCharPref(
"browser.region.network.url",
`http://localhost:${server.identity.primaryPort}/geo`
);
return server;
}
function send(res, json) {
res.setStatusLine("1.1", 200, "OK");
res.setHeader("content-type", "application/json", true);
res.write(JSON.stringify(json));
//res.finish();
}
add_task(async function test_basic() {
let srv = useHttpServer();
srv.registerPathHandler("/geo", (metadata, response) => {
send(response, { country_code: "US" });
});
await Region.getRegion();
Assert.ok(true, "Region fetch should succeed");
await new Promise(r => srv.stop(r));
});
add_task(async function test_timeout() {
let srv = useHttpServer();
srv.registerPathHandler("/geo", (metadata, response) => {
dump("!!! request to " + Date.now() + ":" + metadata._path + "\n");
response.processAsync();
// eslint-disable-next-line mozilla/no-arbitrary-setTimeout
setTimeout(() => {
send(response, { country_code: "US" });
}, 2000);
});
Services.prefs.setIntPref("browser.region.timeout", 1000);
try {
await Region.getRegion();
Assert.ok(false, "Region fetch should have timed out");
} catch (e) {
Assert.equal(e.message, "region-fetch-timeout", "Region fetch timed out");
}
await new Promise(r => srv.stop(r));
});
id:18366 !!! CALLING FETCH A SINGLE TIME TO: http://localhost:63467/geo
0:03.52 INFO (xpcshell/head.js) | test run_next_test 0 finished (2)
0:03.53 pid:18366 !!! DONE
0:03.53 PASS test_basic - [test_basic : 31] Region fetch should succeed - true == true
0:03.53 INFO (xpcshell/head.js) | test run_next_test 1 pending (2)
0:03.53 INFO (xpcshell/head.js) | test test_basic finished (2)
0:03.53 INFO toolkit/modules/tests/xpcshell/test_Region.js | Starting test_timeout
0:03.53 INFO (xpcshell/head.js) | test test_timeout pending (2)
0:03.53 pid:18366 !!! CALLING FETCH A SINGLE TIME TO: http://localhost:63469/geo
0:03.53 INFO (xpcshell/head.js) | test run_next_test 1 finished (2)
0:03.53 pid:18366 !!! request to 1588106739843:/geo
0:03.53 pid:18366 !!! request to 1588106739844:/geo
0:03.54 pid:18366 !!! request to 1588106739846:/geo
0:03.54 pid:18366 !!! request to 1588106739848:/geo
0:03.54 pid:18366 !!! request to 1588106739850:/geo
0:03.54 pid:18366 !!! request to 1588106739851:/geo
0:03.54 pid:18366 !!! request to 1588106739852:/geo
0:03.54 pid:18366 !!! request to 1588106739853:/geo
0:03.54 pid:18366 !!! request to 1588106739854:/geo
0:03.54 pid:18366 !!! request to 1588106739856:/geo
0:04.54 PASS test_timeout - [test_timeout : 54] Region fetch timed out - "region-fetch-timeout" == "region-fetch-timeout"
0:04.54 INFO (xpcshell/head.js) | test run_next_test 2 pending (2)
0:04.54 INFO (xpcshell/head.js) | test test_timeout finished (2)
0:04.54 INFO (xpcshell/head.js) | test run_next_test 2 finished (1)
0:04.54 INFO exiting test
0:04.56 TEST_END: Test PASS. Subtests passed 2/2. Unexpected 0
0:04.57 INFO INFO | Result summary:
0:04.57 INFO INFO | Passed: 1
0:04.57 INFO INFO | Failed: 0
0:04.57 INFO INFO | Todo: 0
0:04.57 INFO INFO | Retried: 0
0:04.57 SUITE_END
0:04.57 INFO Node moz-http2 server shutting down ...
0:04.57
Overall Summary
===============
xpcshell
~~~~~~~~
Ran 3 checks (2 subtests, 1 tests)
Expected results: 3
Unexpected results: 0
OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment