Bug 1407515 - more detailed test of Open in New Tab draft
authorTom Glowka <glowka.tom@gmail.com>
Fri, 30 Mar 2018 04:33:42 +0200
changeset 776653 67e1dfd23c31
parent 775112 9ba01ea8f145
push id104936
push userbmo:glowka.tom@gmail.com
push dateTue, 03 Apr 2018 14:42:38 +0000
bugs1407515
milestone61.0a1
Bug 1407515 - more detailed test of Open in New Tab MozReview-Commit-ID: Cd7HCTg3kCJ
devtools/client/netmonitor/test/browser.ini
devtools/client/netmonitor/test/browser_net_open_request_in_tab.js
devtools/client/netmonitor/test/head.js
devtools/client/netmonitor/test/html_open-request-in-tab.html
devtools/client/netmonitor/test/sjs_method-test-server.sjs
--- a/devtools/client/netmonitor/test/browser.ini
+++ b/devtools/client/netmonitor/test/browser.ini
@@ -35,21 +35,23 @@ support-files =
   html_single-get-page.html
   html_send-beacon.html
   html_sorting-test-page.html
   html_statistics-test-page.html
   html_status-codes-test-page.html
   html_api-calls-test-page.html
   html_copy-as-curl.html
   html_curl-utils.html
+  html_open-request-in-tab.html
   sjs_content-type-test-server.sjs
   sjs_cors-test-server.sjs
   sjs_https-redirect-test-server.sjs
   sjs_hsts-test-server.sjs
   sjs_json-test-server.sjs
+  sjs_method-test-server.sjs
   sjs_simple-test-server.sjs
   sjs_simple-unsorted-cookies-test-server.sjs
   sjs_sorting-test-server.sjs
   sjs_status-codes-test-server.sjs
   sjs_truncate-test-server.sjs
   test-image.png
   service-workers/status-codes.html
   service-workers/status-codes-service-worker.js
--- a/devtools/client/netmonitor/test/browser_net_open_request_in_tab.js
+++ b/devtools/client/netmonitor/test/browser_net_open_request_in_tab.js
@@ -3,38 +3,74 @@
 
 "use strict";
 
 /**
  * Tests if Open in new tab works.
  */
 
 add_task(async function() {
-  let { tab, monitor } = await initNetMonitor(CUSTOM_GET_URL);
+  let { tab, monitor } = await initNetMonitor(OPEN_REQUEST_IN_TAB_URL);
   info("Starting test...");
 
   let { document, store, windowRequire } = monitor.panelWin;
   let contextMenuDoc = monitor.panelWin.parent.document;
   let Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
+  let newTab;
 
   store.dispatch(Actions.batchEnable(false));
 
-  // Execute requests.
-  await performRequests(monitor, tab, 1);
+  // Post data may be fetched by the Header panel,
+  // so set the Timings panel as the new default.
+  store.getState().ui.detailsPanelSelectedTab = "timings";
+
+  // Open GET request in new tab
+  await performRequest("GET");
 
-  wait = waitForDOM(contextMenuDoc, "#request-list-context-newtab");
-  EventUtils.sendMouseEvent({ type: "mousedown" },
-    document.querySelectorAll(".request-list-item")[0]);
-  EventUtils.sendMouseEvent({ type: "contextmenu" },
-    document.querySelectorAll(".request-list-item")[0]);
-  await wait;
+  newTab = await openLastRequestInTab();
+  await checkTabResponse(newTab, "GET");
 
-  let onTabOpen = once(gBrowser.tabContainer, "TabOpen", false);
-  monitor.panelWin.parent.document
-    .querySelector("#request-list-context-newtab").click();
-  await onTabOpen;
-
-  ok(true, "A new tab has been opened");
+  // Open POST request in new tab
+  await performRequest("POST");
+  newTab = await openLastRequestInTab();
+  await checkTabResponse(newTab, "POST");
 
   await teardown(monitor);
 
   gBrowser.removeCurrentTab();
+
+  async function openLastRequestInTab() {
+    let wait = waitForDOM(contextMenuDoc, "#request-list-context-newtab");
+    let requestItems = document.querySelectorAll(".request-list-item");
+    let lastRequest = requestItems[requestItems.length - 1];
+    EventUtils.sendMouseEvent({ type: "mousedown" }, lastRequest);
+    EventUtils.sendMouseEvent({ type: "contextmenu" }, lastRequest);
+    await wait;
+
+    let onTabOpen = once(gBrowser.tabContainer, "TabOpen", false);
+    monitor.panelWin.parent.document
+      .querySelector("#request-list-context-newtab").click();
+    await onTabOpen;
+    ok(true, "A new tab has been opened");
+
+    let awaitedTab = gBrowser.selectedTab;
+    await BrowserTestUtils.browserLoaded(awaitedTab.linkedBrowser);
+    info("The tab load completed");
+
+    return awaitedTab;
+  }
+
+  async function performRequest(method) {
+    let wait = waitForNetworkEvents(monitor, 1);
+    await ContentTask.spawn(tab.linkedBrowser, method, async function(meth) {
+      content.wrappedJSObject.performRequest(meth);
+    });
+    await wait;
+  }
+
+  async function checkTabResponse(checkedTab, method) {
+    await ContentTask.spawn(checkedTab.linkedBrowser, method, async function(met) {
+      let { body } = content.wrappedJSObject.document;
+      let responseRE = RegExp(met + (met == "POST" ? "\n*\s*foo\=bar\&amp;baz\=42" : ""));
+      ok(body.innerHTML.match(responseRE), "Tab method and data match original request");
+    });
+  }
 });
--- a/devtools/client/netmonitor/test/head.js
+++ b/devtools/client/netmonitor/test/head.js
@@ -58,26 +58,28 @@ const INFINITE_GET_URL = EXAMPLE_URL + "
 const CUSTOM_GET_URL = EXAMPLE_URL + "html_custom-get-page.html";
 const SINGLE_GET_URL = EXAMPLE_URL + "html_single-get-page.html";
 const STATISTICS_URL = EXAMPLE_URL + "html_statistics-test-page.html";
 const CURL_URL = EXAMPLE_URL + "html_copy-as-curl.html";
 const CURL_UTILS_URL = EXAMPLE_URL + "html_curl-utils.html";
 const SEND_BEACON_URL = EXAMPLE_URL + "html_send-beacon.html";
 const CORS_URL = EXAMPLE_URL + "html_cors-test-page.html";
 const PAUSE_URL = EXAMPLE_URL + "html_pause-test-page.html";
+const OPEN_REQUEST_IN_TAB_URL = EXAMPLE_URL + "html_open-request-in-tab.html";
 
 const SIMPLE_SJS = EXAMPLE_URL + "sjs_simple-test-server.sjs";
 const SIMPLE_UNSORTED_COOKIES_SJS = EXAMPLE_URL + "sjs_simple-unsorted-cookies-test-server.sjs";
 const CONTENT_TYPE_SJS = EXAMPLE_URL + "sjs_content-type-test-server.sjs";
 const HTTPS_CONTENT_TYPE_SJS = HTTPS_EXAMPLE_URL + "sjs_content-type-test-server.sjs";
 const STATUS_CODES_SJS = EXAMPLE_URL + "sjs_status-codes-test-server.sjs";
 const SORTING_SJS = EXAMPLE_URL + "sjs_sorting-test-server.sjs";
 const HTTPS_REDIRECT_SJS = EXAMPLE_URL + "sjs_https-redirect-test-server.sjs";
 const CORS_SJS_PATH = "/browser/devtools/client/netmonitor/test/sjs_cors-test-server.sjs";
 const HSTS_SJS = EXAMPLE_URL + "sjs_hsts-test-server.sjs";
+const METHOD_SJS = EXAMPLE_URL + "sjs_method-test-server.sjs";
 
 const HSTS_BASE_URL = EXAMPLE_URL;
 const HSTS_PAGE_URL = CUSTOM_GET_URL;
 
 const TEST_IMAGE = EXAMPLE_URL + "test-image.png";
 const TEST_IMAGE_DATA_URI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg==";
 
 /* eslint-enable no-unused-vars, max-len */
new file mode 100644
--- /dev/null
+++ b/devtools/client/netmonitor/test/html_open-request-in-tab.html
@@ -0,0 +1,31 @@
+<!-- Any copyright is dedicated to the Public Domain.
+http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!doctype html>
+
+<html>
+  <head>
+    <meta charset="utf-8"/>
+    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
+    <meta http-equiv="Pragma" content="no-cache" />
+    <meta http-equiv="Expires" content="0" />
+    <title>Network Monitor test page</title>
+  </head>
+
+  <body>
+    <p>Performing a GET or POST request</p>
+
+    <script type="text/javascript">
+      /* exported performRequest */
+      "use strict";
+
+      function performRequest(method) {
+        let xhr = new XMLHttpRequest();
+        let url = "sjs_method-test-server.sjs";
+        let payload = method == "POST" ? "foo=bar&baz=42" : null;
+        xhr.open(method, url, true);
+        xhr.setRequestHeader("Accept-Language", window.navigator.language);
+        xhr.send(payload);
+      }
+    </script>
+  </body>
+</html>
new file mode 100644
--- /dev/null
+++ b/devtools/client/netmonitor/test/sjs_method-test-server.sjs
@@ -0,0 +1,24 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const CC = Components.Constructor;
+const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
+  "nsIBinaryInputStream",
+  "setInputStream");
+
+function handleRequest(request, response) {
+  response.setStatusLine(request.httpVersion, 200, "Och Aye");
+  response.setHeader("Content-Type", "text/plain; charset=utf-8", false);
+
+  var body = request.method + "\n" ;
+  if (request.method == "POST") {
+    var bodyStream = new BinaryInputStream(request.bodyInputStream);
+    var bytes = [], avail = 0;
+    while ((avail = bodyStream.available()) > 0) {
+      body += String.fromCharCode.apply(String, bodyStream.readByteArray(avail));
+    }
+  }
+
+  response.bodyOutputStream.write(body, body.length);
+}