Bug 1596101 - [remote] Forward Network.setUserAgentOverride to Emulation.setUserAgentOverride. r=remote-protocol-reviewers,maja_zf
authorHenrik Skupin <mail@hskupin.info>
Mon, 18 Nov 2019 10:07:42 +0000
changeset 502392 06d19681d0426ac65f242b606d2950dd96690101
parent 502391 73c00aa07450a45bae0d6552ac25fc682a755a2a
child 502393 c10fb6eef9083f88cfc423b51c640a2f6af55618
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersremote-protocol-reviewers, maja_zf
bugs1596101
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1596101 - [remote] Forward Network.setUserAgentOverride to Emulation.setUserAgentOverride. r=remote-protocol-reviewers,maja_zf The method under the Network domain is a redirect to Emulation only. It might be deprecated, but that's unclear in the current documentation. Differential Revision: https://phabricator.services.mozilla.com/D52929
remote/domains/parent/Network.jsm
remote/test/browser/emulation/browser_setUserAgentOverride.js
--- a/remote/domains/parent/Network.jsm
+++ b/remote/domains/parent/Network.jsm
@@ -70,16 +70,26 @@ class Network extends Domain {
     }
     const { browser } = this.session.target;
     this._networkObserver.stopTrackingBrowserNetwork(browser);
     this._networkObserver.off("request", this._onRequest);
     this._networkObserver.dispose();
     this.enabled = false;
   }
 
+  /**
+   * Allows overriding user agent with the given string.
+   *
+   * Redirected to Emulation.setUserAgentOverride.
+   */
+  setUserAgentOverride(options) {
+    const { id } = this.session;
+    this.session.execute(id, "Emulation", "setUserAgentOverride", options);
+  }
+
   _onRequest(eventName, httpChannel, data) {
     const topFrame = getLoadContext(httpChannel).topFrameElement;
     const request = {
       url: httpChannel.URI.spec,
       urlFragment: undefined,
       method: httpChannel.requestMethod,
       headers: [],
       postData: undefined,
--- a/remote/test/browser/emulation/browser_setUserAgentOverride.js
+++ b/remote/test/browser/emulation/browser_setUserAgentOverride.js
@@ -50,13 +50,66 @@ add_task(async function invalidUserAgent
   try {
     await Emulation.setUserAgentOverride({ userAgent });
   } catch (e) {
     errorThrown = true;
   }
   ok(errorThrown, "Invalid user agent format raised error");
 });
 
+// Network.setUserAgentOverride is a redirect to Emulation.setUserAgentOverride.
+// Run duplicated tests for Network to ensure the redirect works.
+
+add_task(async function networkSetAndResetUserAgent({ Network }) {
+  const userAgent = "foo bar";
+
+  await loadURL(DOC);
+  isnot(
+    await getNavigatorProperty("userAgent"),
+    userAgent,
+    "Custom user agent hasn't been set"
+  );
+
+  try {
+    await Network.setUserAgentOverride({ userAgent });
+    await loadURL(DOC);
+    is(
+      await getNavigatorProperty("userAgent"),
+      userAgent,
+      "Custom user agent has been set"
+    );
+
+    await Network.setUserAgentOverride({ userAgent: "" });
+    await loadURL(DOC);
+    isnot(
+      await getNavigatorProperty("userAgent"),
+      userAgent,
+      "Custom user agent hasn't been set anymore"
+    );
+  } finally {
+    Services.prefs.clearUserPref("general.useragent.override");
+  }
+});
+
+add_task(async function networkInvalidUserAgent({ Network }) {
+  const userAgent = "foobar\n";
+
+  await loadURL(DOC);
+  isnot(
+    await getNavigatorProperty("userAgent"),
+    userAgent,
+    "Custom user agent hasn't been set"
+  );
+
+  let errorThrown = false;
+  try {
+    await Network.setUserAgentOverride({ userAgent });
+  } catch (e) {
+    errorThrown = true;
+  }
+  ok(errorThrown, "Invalid user agent format raised error");
+});
+
 async function getNavigatorProperty(prop) {
   return ContentTask.spawn(gBrowser.selectedBrowser, prop, _prop => {
     return content.navigator[_prop];
   });
 }