browser_webconsole_bug_632817.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. // Tests that Web console messages can be filtered for NET events.
  5. "use strict";
  6. const TEST_NETWORK_REQUEST_URI =
  7. "https://example.com/browser/devtools/client/webconsole/test/" +
  8. "test-network-request.html";
  9. const TEST_IMG = "http://example.com/browser/devtools/client/webconsole/" +
  10. "test/test-image.png";
  11. const TEST_DATA_JSON_CONTENT =
  12. '{ id: "test JSON data", myArray: [ "foo", "bar", "baz", "biff" ] }';
  13. const TEST_URI = "data:text/html;charset=utf-8,Web Console network logging " +
  14. "tests";
  15. const PAGE_REQUEST_PREDICATE =
  16. ({ request }) => request.url.endsWith("test-network-request.html");
  17. const TEST_DATA_REQUEST_PREDICATE =
  18. ({ request }) => request.url.endsWith("test-data.json");
  19. const XHR_WARN_REQUEST_PREDICATE =
  20. ({ request }) => request.url.endsWith("sjs_cors-test-server.sjs");
  21. let hud;
  22. add_task(function*() {
  23. const PREF = "devtools.webconsole.persistlog";
  24. const NET_PREF = "devtools.webconsole.filter.networkinfo";
  25. const NETXHR_PREF = "devtools.webconsole.filter.netxhr";
  26. const MIXED_AC_PREF = "security.mixed_content.block_active_content";
  27. let original = Services.prefs.getBoolPref(NET_PREF);
  28. let originalXhr = Services.prefs.getBoolPref(NETXHR_PREF);
  29. let originalMixedActive = Services.prefs.getBoolPref(MIXED_AC_PREF);
  30. Services.prefs.setBoolPref(NET_PREF, true);
  31. Services.prefs.setBoolPref(NETXHR_PREF, true);
  32. Services.prefs.setBoolPref(MIXED_AC_PREF, false);
  33. Services.prefs.setBoolPref(PREF, true);
  34. registerCleanupFunction(() => {
  35. Services.prefs.setBoolPref(NET_PREF, original);
  36. Services.prefs.setBoolPref(NETXHR_PREF, originalXhr);
  37. Services.prefs.setBoolPref(MIXED_AC_PREF, originalMixedActive);
  38. Services.prefs.clearUserPref(PREF);
  39. hud = null;
  40. });
  41. yield loadTab(TEST_URI);
  42. hud = yield openConsole();
  43. yield testPageLoad();
  44. yield testXhrGet();
  45. yield testXhrWarn();
  46. yield testXhrPost();
  47. yield testFormSubmission();
  48. yield testLiveFilteringOnSearchStrings();
  49. });
  50. function testPageLoad() {
  51. BrowserTestUtils.loadURI(gBrowser.selectedBrowser, TEST_NETWORK_REQUEST_URI);
  52. let lastRequest = yield waitForFinishedRequest(PAGE_REQUEST_PREDICATE);
  53. // Check if page load was logged correctly.
  54. ok(lastRequest, "Page load was logged");
  55. is(lastRequest.request.url, TEST_NETWORK_REQUEST_URI,
  56. "Logged network entry is page load");
  57. is(lastRequest.request.method, "GET", "Method is correct");
  58. }
  59. function testXhrGet() {
  60. // Start the XMLHttpRequest() GET test.
  61. ContentTask.spawn(gBrowser.selectedBrowser, {}, function*() {
  62. content.wrappedJSObject.testXhrGet();
  63. });
  64. let lastRequest = yield waitForFinishedRequest(TEST_DATA_REQUEST_PREDICATE);
  65. ok(lastRequest, "testXhrGet() was logged");
  66. is(lastRequest.request.method, "GET", "Method is correct");
  67. ok(lastRequest.isXHR, "It's an XHR request");
  68. }
  69. function testXhrWarn() {
  70. // Start the XMLHttpRequest() warn test.
  71. ContentTask.spawn(gBrowser.selectedBrowser, {}, function*() {
  72. content.wrappedJSObject.testXhrWarn();
  73. });
  74. let lastRequest = yield waitForFinishedRequest(XHR_WARN_REQUEST_PREDICATE);
  75. if (lastRequest.request.method == "HEAD") {
  76. lastRequest = yield waitForFinishedRequest(XHR_WARN_REQUEST_PREDICATE);
  77. }
  78. ok(lastRequest, "testXhrWarn() was logged");
  79. is(lastRequest.request.method, "GET", "Method is correct");
  80. ok(lastRequest.isXHR, "It's an XHR request");
  81. is(lastRequest.securityInfo, "insecure", "It's an insecure request");
  82. }
  83. function testXhrPost() {
  84. // Start the XMLHttpRequest() POST test.
  85. ContentTask.spawn(gBrowser.selectedBrowser, {}, function*() {
  86. content.wrappedJSObject.testXhrPost();
  87. });
  88. let lastRequest = yield waitForFinishedRequest(TEST_DATA_REQUEST_PREDICATE);
  89. ok(lastRequest, "testXhrPost() was logged");
  90. is(lastRequest.request.method, "POST", "Method is correct");
  91. ok(lastRequest.isXHR, "It's an XHR request");
  92. }
  93. function testFormSubmission() {
  94. // Start the form submission test. As the form is submitted, the page is
  95. // loaded again. Bind to the load event to catch when this is done.
  96. ContentTask.spawn(gBrowser.selectedBrowser, {}, function*() {
  97. let form = content.document.querySelector("form");
  98. ok(form, "we have the HTML form");
  99. form.submit();
  100. });
  101. // The form POSTs to the page URL but over https (page over http).
  102. let lastRequest = yield waitForFinishedRequest(PAGE_REQUEST_PREDICATE);
  103. ok(lastRequest, "testFormSubmission() was logged");
  104. is(lastRequest.request.method, "POST", "Method is correct");
  105. // There should be 3 network requests pointing to the HTML file.
  106. waitForMessages({
  107. webconsole: hud,
  108. messages: [
  109. {
  110. text: "test-network-request.html",
  111. category: CATEGORY_NETWORK,
  112. severity: SEVERITY_LOG,
  113. count: 3,
  114. },
  115. {
  116. text: "test-data.json",
  117. category: CATEGORY_NETWORK,
  118. severity: SEVERITY_INFO,
  119. isXhr: true,
  120. count: 2,
  121. },
  122. {
  123. text: "http://example.com/",
  124. category: CATEGORY_NETWORK,
  125. severity: SEVERITY_WARNING,
  126. isXhr: true,
  127. count: 1,
  128. },
  129. ],
  130. });
  131. }
  132. function testLiveFilteringOnSearchStrings() {
  133. setStringFilter("http");
  134. isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
  135. "search string is set to \"http\"");
  136. setStringFilter("HTTP");
  137. isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
  138. "search string is set to \"HTTP\"");
  139. setStringFilter("hxxp");
  140. is(countMessageNodes(), 0, "the log nodes are hidden when the search " +
  141. "string is set to \"hxxp\"");
  142. setStringFilter("ht tp");
  143. isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
  144. "search string is set to \"ht tp\"");
  145. setStringFilter("");
  146. isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
  147. "search string is removed");
  148. setStringFilter("json");
  149. is(countMessageNodes(), 2, "the log nodes show only the nodes with \"json\"");
  150. setStringFilter("'foo'");
  151. is(countMessageNodes(), 0, "the log nodes are hidden when searching for " +
  152. "the string 'foo'");
  153. setStringFilter("foo\"bar'baz\"boo'");
  154. is(countMessageNodes(), 0, "the log nodes are hidden when searching for " +
  155. "the string \"foo\"bar'baz\"boo'\"");
  156. }
  157. function countMessageNodes() {
  158. let messageNodes = hud.outputNode.querySelectorAll(".message");
  159. let displayedMessageNodes = 0;
  160. let view = hud.iframeWindow;
  161. for (let i = 0; i < messageNodes.length; i++) {
  162. let computedStyle = view.getComputedStyle(messageNodes[i], null);
  163. if (computedStyle.display !== "none") {
  164. displayedMessageNodes++;
  165. }
  166. }
  167. return displayedMessageNodes;
  168. }
  169. function setStringFilter(value) {
  170. hud.ui.filterBox.value = value;
  171. hud.ui.adjustVisibilityOnSearchStringChange();
  172. }