Bug 1810230 - [cdp] Update JSONHandler to set "/json" as alias for "/json/list" r=webdriver-reviewers,whimboo

Added /json as an alias for /json/list endpoint in the HTTP routes.
Also added tests for /json/list generally.

Differential Revision: https://phabricator.services.mozilla.com/D166829
This commit is contained in:
CanadaHonk 2023-01-20 14:47:15 +00:00
parent 70ce66342c
commit 8e0454bf81
3 changed files with 57 additions and 4 deletions

View File

@ -87,10 +87,7 @@ export class CDP {
lazy.logger.debug(`Waiting for initial application window`);
await this.agent.browserStartupFinished;
this.agent.server.registerPrefixHandler(
"/json/",
new lazy.JSONHandler(this)
);
this.agent.server.registerPrefixHandler("/", new lazy.JSONHandler(this));
this.targetList = new lazy.TargetList();
this.targetList.on("target-created", (eventName, target) => {

View File

@ -24,6 +24,7 @@ export class JSONHandler {
"/json/version": this.getVersion.bind(this),
"/json/protocol": this.getProtocol.bind(this),
"/json/list": this.getTargetList.bind(this),
"/json": this.getTargetList.bind(this),
};
}

View File

@ -35,11 +35,66 @@ add_task(async function json_version() {
add_task(async function check_routes() {
for (const route of routes) {
// Check request succeeded (200) and responded with valid JSON
info(`Checking ${route}`);
await requestJSON(route);
info(`Checking ${route + "/"}`);
await requestJSON(route + "/");
}
});
add_task(async function json_list({ client }) {
const { Target } = client;
const { targetInfos } = await Target.getTargets();
const json = await requestJSON("/json/list");
const jsonAlias = await requestJSON("/json");
Assert.deepEqual(json, jsonAlias, "/json/list and /json return the same");
ok(Array.isArray(json), "Target list is an array");
is(
json.length,
targetInfos.length,
"Targets as listed on /json/list are equal to Target.getTargets"
);
for (let i = 0; i < json.length; i++) {
const jsonTarget = json[i];
const wsTarget = targetInfos[i];
is(
jsonTarget.id,
wsTarget.targetId,
"Target id matches between HTTP and Target.getTargets"
);
is(
jsonTarget.type,
wsTarget.type,
"Target type matches between HTTP and Target.getTargets"
);
is(
jsonTarget.url,
wsTarget.url,
"Target url matches between HTTP and Target.getTargets"
);
// Ensure expected values specifically for JSON endpoint
// and that type is always "page" as main process target should not be included
is(
jsonTarget.type,
"page",
`Target (${jsonTarget.id}) from list has expected type (page)`
);
is(
jsonTarget.webSocketDebuggerUrl,
`ws://${RemoteAgent.debuggerAddress}/devtools/page/${wsTarget.targetId}`,
`Target (${jsonTarget.id}) from list has expected webSocketDebuggerUrl value`
);
}
});
async function requestJSON(path) {
const response = await fetch(`http://${RemoteAgent.debuggerAddress}${path}`);
is(response.status, 200, "JSON response is 200");