Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* vim:set ts=2 sw=2 sts=2 et: */ |
michael@0 | 2 | /* ***** BEGIN LICENSE BLOCK ***** |
michael@0 | 3 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 4 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 5 | * |
michael@0 | 6 | * Contributor(s): |
michael@0 | 7 | * Julian Viereck <jviereck@mozilla.com> |
michael@0 | 8 | * Patrick Walton <pcwalton@mozilla.com> |
michael@0 | 9 | * Mihai Șucan <mihai.sucan@gmail.com> |
michael@0 | 10 | * |
michael@0 | 11 | * ***** END LICENSE BLOCK ***** */ |
michael@0 | 12 | |
michael@0 | 13 | // Tests that network log messages bring up the network panel. |
michael@0 | 14 | |
michael@0 | 15 | const TEST_NETWORK_REQUEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/test-network-request.html"; |
michael@0 | 16 | |
michael@0 | 17 | const TEST_IMG = "http://example.com/browser/browser/devtools/webconsole/test/test-image.png"; |
michael@0 | 18 | |
michael@0 | 19 | const TEST_DATA_JSON_CONTENT = |
michael@0 | 20 | '{ id: "test JSON data", myArray: [ "foo", "bar", "baz", "biff" ] }'; |
michael@0 | 21 | |
michael@0 | 22 | let lastRequest = null; |
michael@0 | 23 | let requestCallback = null; |
michael@0 | 24 | |
michael@0 | 25 | function test() |
michael@0 | 26 | { |
michael@0 | 27 | addTab("data:text/html;charset=utf-8,Web Console network logging tests"); |
michael@0 | 28 | |
michael@0 | 29 | browser.addEventListener("load", function onLoad() { |
michael@0 | 30 | browser.removeEventListener("load", onLoad, true); |
michael@0 | 31 | |
michael@0 | 32 | openConsole(null, function(aHud) { |
michael@0 | 33 | hud = aHud; |
michael@0 | 34 | |
michael@0 | 35 | HUDService.lastFinishedRequest.callback = requestCallbackWrapper; |
michael@0 | 36 | |
michael@0 | 37 | executeSoon(testPageLoad); |
michael@0 | 38 | }); |
michael@0 | 39 | }, true); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function requestCallbackWrapper(aRequest) |
michael@0 | 43 | { |
michael@0 | 44 | lastRequest = aRequest; |
michael@0 | 45 | |
michael@0 | 46 | hud.ui.webConsoleClient.getResponseContent(lastRequest.actor, |
michael@0 | 47 | function(aResponse) { |
michael@0 | 48 | lastRequest.response.content = aResponse.content; |
michael@0 | 49 | lastRequest.discardResponseBody = aResponse.contentDiscarded; |
michael@0 | 50 | |
michael@0 | 51 | hud.ui.webConsoleClient.getRequestPostData(lastRequest.actor, |
michael@0 | 52 | function(aResponse) { |
michael@0 | 53 | lastRequest.request.postData = aResponse.postData; |
michael@0 | 54 | lastRequest.discardRequestBody = aResponse.postDataDiscarded; |
michael@0 | 55 | |
michael@0 | 56 | if (requestCallback) { |
michael@0 | 57 | requestCallback(); |
michael@0 | 58 | } |
michael@0 | 59 | }); |
michael@0 | 60 | }); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | function testPageLoad() |
michael@0 | 64 | { |
michael@0 | 65 | requestCallback = function() { |
michael@0 | 66 | // Check if page load was logged correctly. |
michael@0 | 67 | ok(lastRequest, "Page load was logged"); |
michael@0 | 68 | |
michael@0 | 69 | is(lastRequest.request.url, TEST_NETWORK_REQUEST_URI, |
michael@0 | 70 | "Logged network entry is page load"); |
michael@0 | 71 | is(lastRequest.request.method, "GET", "Method is correct"); |
michael@0 | 72 | ok(!lastRequest.request.postData.text, "No request body was stored"); |
michael@0 | 73 | ok(lastRequest.discardRequestBody, "Request body was discarded"); |
michael@0 | 74 | ok(!lastRequest.response.content.text, "No response body was stored"); |
michael@0 | 75 | ok(lastRequest.discardResponseBody, "Response body was discarded"); |
michael@0 | 76 | |
michael@0 | 77 | lastRequest = null; |
michael@0 | 78 | requestCallback = null; |
michael@0 | 79 | executeSoon(testPageLoadBody); |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | content.location = TEST_NETWORK_REQUEST_URI; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | function testPageLoadBody() |
michael@0 | 86 | { |
michael@0 | 87 | // Turn on logging of request bodies and check again. |
michael@0 | 88 | hud.ui.setSaveRequestAndResponseBodies(true).then(() => { |
michael@0 | 89 | ok(hud.ui._saveRequestAndResponseBodies, |
michael@0 | 90 | "The saveRequestAndResponseBodies property was successfully set."); |
michael@0 | 91 | |
michael@0 | 92 | testPageLoadBodyAfterSettingUpdate(); |
michael@0 | 93 | }); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | function testPageLoadBodyAfterSettingUpdate() |
michael@0 | 97 | { |
michael@0 | 98 | let loaded = false; |
michael@0 | 99 | let requestCallbackInvoked = false; |
michael@0 | 100 | |
michael@0 | 101 | requestCallback = function() { |
michael@0 | 102 | ok(lastRequest, "Page load was logged again"); |
michael@0 | 103 | ok(!lastRequest.discardResponseBody, "Response body was not discarded"); |
michael@0 | 104 | is(lastRequest.response.content.text.indexOf("<!DOCTYPE HTML>"), 0, |
michael@0 | 105 | "Response body's beginning is okay"); |
michael@0 | 106 | |
michael@0 | 107 | lastRequest = null; |
michael@0 | 108 | requestCallback = null; |
michael@0 | 109 | requestCallbackInvoked = true; |
michael@0 | 110 | |
michael@0 | 111 | if (loaded) { |
michael@0 | 112 | executeSoon(testXhrGet); |
michael@0 | 113 | } |
michael@0 | 114 | }; |
michael@0 | 115 | |
michael@0 | 116 | browser.addEventListener("load", function onLoad() { |
michael@0 | 117 | browser.removeEventListener("load", onLoad, true); |
michael@0 | 118 | loaded = true; |
michael@0 | 119 | |
michael@0 | 120 | if (requestCallbackInvoked) { |
michael@0 | 121 | executeSoon(testXhrGet); |
michael@0 | 122 | } |
michael@0 | 123 | }, true); |
michael@0 | 124 | |
michael@0 | 125 | content.location.reload(); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | function testXhrGet() |
michael@0 | 129 | { |
michael@0 | 130 | requestCallback = function() { |
michael@0 | 131 | ok(lastRequest, "testXhrGet() was logged"); |
michael@0 | 132 | is(lastRequest.request.method, "GET", "Method is correct"); |
michael@0 | 133 | ok(!lastRequest.request.postData.text, "No request body was sent"); |
michael@0 | 134 | ok(!lastRequest.discardRequestBody, "Request body was not discarded"); |
michael@0 | 135 | is(lastRequest.response.content.text, TEST_DATA_JSON_CONTENT, |
michael@0 | 136 | "Response is correct"); |
michael@0 | 137 | |
michael@0 | 138 | lastRequest = null; |
michael@0 | 139 | requestCallback = null; |
michael@0 | 140 | executeSoon(testXhrPost); |
michael@0 | 141 | }; |
michael@0 | 142 | |
michael@0 | 143 | // Start the XMLHttpRequest() GET test. |
michael@0 | 144 | content.wrappedJSObject.testXhrGet(); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | function testXhrPost() |
michael@0 | 148 | { |
michael@0 | 149 | requestCallback = function() { |
michael@0 | 150 | ok(lastRequest, "testXhrPost() was logged"); |
michael@0 | 151 | is(lastRequest.request.method, "POST", "Method is correct"); |
michael@0 | 152 | is(lastRequest.request.postData.text, "Hello world!", |
michael@0 | 153 | "Request body was logged"); |
michael@0 | 154 | is(lastRequest.response.content.text, TEST_DATA_JSON_CONTENT, |
michael@0 | 155 | "Response is correct"); |
michael@0 | 156 | |
michael@0 | 157 | lastRequest = null; |
michael@0 | 158 | requestCallback = null; |
michael@0 | 159 | executeSoon(testFormSubmission); |
michael@0 | 160 | }; |
michael@0 | 161 | |
michael@0 | 162 | // Start the XMLHttpRequest() POST test. |
michael@0 | 163 | content.wrappedJSObject.testXhrPost(); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | function testFormSubmission() |
michael@0 | 167 | { |
michael@0 | 168 | // Start the form submission test. As the form is submitted, the page is |
michael@0 | 169 | // loaded again. Bind to the load event to catch when this is done. |
michael@0 | 170 | requestCallback = function() { |
michael@0 | 171 | ok(lastRequest, "testFormSubmission() was logged"); |
michael@0 | 172 | is(lastRequest.request.method, "POST", "Method is correct"); |
michael@0 | 173 | isnot(lastRequest.request.postData.text. |
michael@0 | 174 | indexOf("Content-Type: application/x-www-form-urlencoded"), -1, |
michael@0 | 175 | "Content-Type is correct"); |
michael@0 | 176 | isnot(lastRequest.request.postData.text. |
michael@0 | 177 | indexOf("Content-Length: 20"), -1, "Content-length is correct"); |
michael@0 | 178 | isnot(lastRequest.request.postData.text. |
michael@0 | 179 | indexOf("name=foo+bar&age=144"), -1, "Form data is correct"); |
michael@0 | 180 | is(lastRequest.response.content.text.indexOf("<!DOCTYPE HTML>"), 0, |
michael@0 | 181 | "Response body's beginning is okay"); |
michael@0 | 182 | |
michael@0 | 183 | executeSoon(testNetworkPanel); |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | let form = content.document.querySelector("form"); |
michael@0 | 187 | ok(form, "we have the HTML form"); |
michael@0 | 188 | form.submit(); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | function testNetworkPanel() |
michael@0 | 192 | { |
michael@0 | 193 | // Open the NetworkPanel. The functionality of the NetworkPanel is tested |
michael@0 | 194 | // within separate test files. |
michael@0 | 195 | let networkPanel = hud.ui.openNetworkPanel(hud.ui.filterBox, lastRequest); |
michael@0 | 196 | |
michael@0 | 197 | networkPanel.panel.addEventListener("popupshown", function onPopupShown() { |
michael@0 | 198 | networkPanel.panel.removeEventListener("popupshown", onPopupShown, true); |
michael@0 | 199 | |
michael@0 | 200 | is(hud.ui.filterBox._netPanel, networkPanel, |
michael@0 | 201 | "Network panel stored on anchor node"); |
michael@0 | 202 | ok(true, "NetworkPanel was opened"); |
michael@0 | 203 | |
michael@0 | 204 | // All tests are done. Shutdown. |
michael@0 | 205 | networkPanel.panel.hidePopup(); |
michael@0 | 206 | lastRequest = null; |
michael@0 | 207 | HUDService.lastFinishedRequest.callback = null; |
michael@0 | 208 | executeSoon(finishTest); |
michael@0 | 209 | }, true); |
michael@0 | 210 | } |
michael@0 | 211 |