Commit 70ce6634 authored by CanadaHonk's avatar CanadaHonk
Browse files

Bug 1810253 - [cdp] Support trailing slashes for supported JSONHandler...

Bug 1810253 - [cdp] Support trailing slashes for supported JSONHandler endpoints r=webdriver-reviewers,whimboo

Added support for trailing slashes when using HTTP endpoints
(eg: /json/protocol/), previously they would return 404, not conforming
with CDP docs / expected result. Also added tests for such cases.

Depends on D167048

Differential Revision: https://phabricator.services.mozilla.com/D166873
parent bedadc5c
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -59,12 +59,15 @@ export class JSONHandler {
      throw lazy.HTTP_404;
    }

    if (!(request.path in this.routes)) {
    // Trim trailing slashes to conform with expected routes
    const path = request.path.replace(/\/+$/, "");

    if (!(path in this.routes)) {
      throw lazy.HTTP_404;
    }

    try {
      const body = this.routes[request.path]();
      const body = this.routes[path]();
      const payload = JSON.stringify(
        body,
        null,
+21 −13
Original line number Diff line number Diff line
@@ -3,12 +3,19 @@

"use strict";

const { JSONHandler } = ChromeUtils.importESModule(
  "chrome://remote/content/cdp/JSONHandler.sys.mjs"
);

// Get list of supported routes from JSONHandler
const routes = Object.keys(new JSONHandler().routes);

add_task(async function json_version() {
  const { userAgent } = Cc[
    "@mozilla.org/network/protocol;1?name=http"
  ].getService(Ci.nsIHttpProtocolHandler);

  const json = await requestJSON("/version");
  const json = await requestJSON("/json/version");
  is(
    json.Browser,
    `${Services.appinfo.name}/${Services.appinfo.version}`,
@@ -25,16 +32,17 @@ add_task(async function json_version() {
  );
});

function requestJSON(path) {
  const url = `http://${RemoteAgent.debuggerAddress}`;

  return new Promise(resolve => {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", `${url}/json${path}`);
    xhr.responseType = "json";
    xhr.onloadend = () => {
      resolve(xhr.response);
    };
    xhr.send();
add_task(async function check_routes() {
  for (const route of routes) {
    // Check request succeeded (200) and responded with valid JSON
    await requestJSON(route);
    await requestJSON(route + "/");
  }
});

async function requestJSON(path) {
  const response = await fetch(`http://${RemoteAgent.debuggerAddress}${path}`);
  is(response.status, 200, "JSON response is 200");

  return response.json();
}