1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/webconsole/test/browser_webconsole_network_panel.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,543 @@ 1.4 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +// Tests that the network panel works. 1.10 + 1.11 +const TEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/test-console.html"; 1.12 +const TEST_IMG = "http://example.com/browser/browser/devtools/webconsole/test/test-image.png"; 1.13 +const TEST_ENCODING_ISO_8859_1 = "http://example.com/browser/browser/devtools/webconsole/test/test-encoding-ISO-8859-1.html"; 1.14 + 1.15 +const TEST_IMG_BASE64 = 1.16 + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAVRJREFU" + 1.17 + "OI2lk7FLw0AUxr+YpC1CBqcMWfsvCCLdXFzqEJCgDl1EQRGxg9AhSBEJONhFhG52UCuFDjq5dxD8" + 1.18 + "FwoO0qGDOBQkl7vLOeWa2EQDffDBvTu+373Hu1OEEJgntGgxGD6J+7fLXKbt5VNUyhsKAChRBQcP" + 1.19 + "FVFeWskFGH694mZroCQqCLlAwPxcgJBP254CmAD5B7C7dgHLMLF3uzoL4DQEod+Z5sP1FizDxGgy" + 1.20 + "BqfhLID9AahX29J89bwPFgMsSEAQglAf9WobhPpScbPXr4FQHyzIADTsDizDRMPuIOC+zEeTMZo9" + 1.21 + "BwH3EfAMACccbtfGaDKGZZg423yUZrdrg3EqxQlPr0BTdTR7joREN2uqnlBmCwW1hIJagtev4f3z" + 1.22 + "A16/JvfiigMSYyzqJXlw/XKUyOORMUaBor6YavgdjKa8xGOnidadmwtwsnMu18q83/kHSou+bFND" + 1.23 + "Dr4AAAAASUVORK5CYII="; 1.24 + 1.25 +let testDriver; 1.26 + 1.27 +function test() { 1.28 + addTab(TEST_URI); 1.29 + browser.addEventListener("load", function onLoad() { 1.30 + browser.removeEventListener("load", onLoad, true); 1.31 + openConsole(null, testNetworkPanel); 1.32 + }, true); 1.33 +} 1.34 + 1.35 +function testNetworkPanel() { 1.36 + testDriver = testGen(); 1.37 + testDriver.next(); 1.38 +} 1.39 + 1.40 +function checkIsVisible(aPanel, aList) { 1.41 + for (let id in aList) { 1.42 + let node = aPanel.document.getElementById(id); 1.43 + let isVisible = aList[id]; 1.44 + is(node.style.display, (isVisible ? "block" : "none"), id + " isVisible=" + isVisible); 1.45 + } 1.46 +} 1.47 + 1.48 +function checkNodeContent(aPanel, aId, aContent) { 1.49 + let node = aPanel.document.getElementById(aId); 1.50 + if (node == null) { 1.51 + ok(false, "Tried to access node " + aId + " that doesn't exist!"); 1.52 + } 1.53 + else if (node.textContent.indexOf(aContent) != -1) { 1.54 + ok(true, "checking content of " + aId); 1.55 + } 1.56 + else { 1.57 + ok(false, "Got false value for " + aId + ": " + node.textContent + " doesn't have " + aContent); 1.58 + } 1.59 +} 1.60 + 1.61 +function checkNodeKeyValue(aPanel, aId, aKey, aValue) { 1.62 + let node = aPanel.document.getElementById(aId); 1.63 + 1.64 + let headers = node.querySelectorAll("th"); 1.65 + for (let i = 0; i < headers.length; i++) { 1.66 + if (headers[i].textContent == (aKey + ":")) { 1.67 + is(headers[i].nextElementSibling.textContent, aValue, 1.68 + "checking content of " + aId + " for key " + aKey); 1.69 + return; 1.70 + } 1.71 + } 1.72 + 1.73 + ok(false, "content check failed for " + aId + ", key " + aKey); 1.74 +} 1.75 + 1.76 +function testGen() { 1.77 + let hud = HUDService.getHudByWindow(content); 1.78 + let filterBox = hud.ui.filterBox; 1.79 + 1.80 + let httpActivity = { 1.81 + updates: [], 1.82 + discardRequestBody: true, 1.83 + discardResponseBody: true, 1.84 + startedDateTime: (new Date()).toISOString(), 1.85 + request: { 1.86 + url: "http://www.testpage.com", 1.87 + method: "GET", 1.88 + cookies: [], 1.89 + headers: [ 1.90 + { name: "foo", value: "bar" }, 1.91 + ], 1.92 + }, 1.93 + response: { 1.94 + headers: [], 1.95 + content: {}, 1.96 + cookies: [], 1.97 + }, 1.98 + timings: {}, 1.99 + }; 1.100 + 1.101 + let networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); 1.102 + 1.103 + is(filterBox._netPanel, networkPanel, 1.104 + "Network panel stored on the anchor object"); 1.105 + 1.106 + networkPanel._onUpdate = function() { 1.107 + networkPanel._onUpdate = null; 1.108 + executeSoon(function() { 1.109 + testDriver.next(); 1.110 + }); 1.111 + }; 1.112 + 1.113 + yield undefined; 1.114 + 1.115 + info("test 1"); 1.116 + 1.117 + checkIsVisible(networkPanel, { 1.118 + requestCookie: false, 1.119 + requestFormData: false, 1.120 + requestBody: false, 1.121 + responseContainer: false, 1.122 + responseBody: false, 1.123 + responseNoBody: false, 1.124 + responseImage: false, 1.125 + responseImageCached: false 1.126 + }); 1.127 + 1.128 + checkNodeContent(networkPanel, "header", "http://www.testpage.com"); 1.129 + checkNodeContent(networkPanel, "header", "GET"); 1.130 + checkNodeKeyValue(networkPanel, "requestHeadersContent", "foo", "bar"); 1.131 + 1.132 + // Test request body. 1.133 + info("test 2: request body"); 1.134 + httpActivity.discardRequestBody = false; 1.135 + httpActivity.request.postData = { text: "hello world" }; 1.136 + networkPanel.update(); 1.137 + 1.138 + checkIsVisible(networkPanel, { 1.139 + requestBody: true, 1.140 + requestFormData: false, 1.141 + requestCookie: false, 1.142 + responseContainer: false, 1.143 + responseBody: false, 1.144 + responseNoBody: false, 1.145 + responseImage: false, 1.146 + responseImageCached: false 1.147 + }); 1.148 + checkNodeContent(networkPanel, "requestBodyContent", "hello world"); 1.149 + 1.150 + // Test response header. 1.151 + info("test 3: response header"); 1.152 + httpActivity.timings.wait = 10; 1.153 + httpActivity.response.httpVersion = "HTTP/3.14"; 1.154 + httpActivity.response.status = 999; 1.155 + httpActivity.response.statusText = "earthquake win"; 1.156 + httpActivity.response.content.mimeType = "text/html"; 1.157 + httpActivity.response.headers.push( 1.158 + { name: "Content-Type", value: "text/html" }, 1.159 + { name: "leaveHouses", value: "true" } 1.160 + ); 1.161 + 1.162 + networkPanel.update(); 1.163 + 1.164 + checkIsVisible(networkPanel, { 1.165 + requestBody: true, 1.166 + requestFormData: false, 1.167 + requestCookie: false, 1.168 + responseContainer: true, 1.169 + responseBody: false, 1.170 + responseNoBody: false, 1.171 + responseImage: false, 1.172 + responseImageCached: false 1.173 + }); 1.174 + 1.175 + checkNodeContent(networkPanel, "header", "HTTP/3.14 999 earthquake win"); 1.176 + checkNodeKeyValue(networkPanel, "responseHeadersContent", "leaveHouses", "true"); 1.177 + checkNodeContent(networkPanel, "responseHeadersInfo", "10ms"); 1.178 + 1.179 + info("test 4"); 1.180 + 1.181 + httpActivity.discardResponseBody = false; 1.182 + httpActivity.timings.receive = 2; 1.183 + networkPanel.update(); 1.184 + 1.185 + checkIsVisible(networkPanel, { 1.186 + requestBody: true, 1.187 + requestCookie: false, 1.188 + requestFormData: false, 1.189 + responseContainer: true, 1.190 + responseBody: false, 1.191 + responseNoBody: false, 1.192 + responseImage: false, 1.193 + responseImageCached: false 1.194 + }); 1.195 + 1.196 + info("test 5"); 1.197 + 1.198 + httpActivity.updates.push("responseContent", "eventTimings"); 1.199 + networkPanel.update(); 1.200 + 1.201 + checkNodeContent(networkPanel, "responseNoBodyInfo", "2ms"); 1.202 + checkIsVisible(networkPanel, { 1.203 + requestBody: true, 1.204 + requestCookie: false, 1.205 + responseContainer: true, 1.206 + responseBody: false, 1.207 + responseNoBody: true, 1.208 + responseImage: false, 1.209 + responseImageCached: false 1.210 + }); 1.211 + 1.212 + networkPanel.panel.hidePopup(); 1.213 + 1.214 + // Second run: Test for cookies and response body. 1.215 + info("test 6: cookies and response body"); 1.216 + httpActivity.request.cookies.push( 1.217 + { name: "foo", value: "bar" }, 1.218 + { name: "hello", value: "world" } 1.219 + ); 1.220 + httpActivity.response.content.text = "get out here"; 1.221 + 1.222 + networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); 1.223 + is(filterBox._netPanel, networkPanel, 1.224 + "Network panel stored on httpActivity object"); 1.225 + 1.226 + networkPanel._onUpdate = function() { 1.227 + networkPanel._onUpdate = null; 1.228 + executeSoon(function() { 1.229 + testDriver.next(); 1.230 + }); 1.231 + }; 1.232 + 1.233 + yield undefined; 1.234 + 1.235 + checkIsVisible(networkPanel, { 1.236 + requestBody: true, 1.237 + requestFormData: false, 1.238 + requestCookie: true, 1.239 + responseContainer: true, 1.240 + responseCookie: false, 1.241 + responseBody: true, 1.242 + responseNoBody: false, 1.243 + responseImage: false, 1.244 + responseImageCached: false 1.245 + }); 1.246 + 1.247 + checkNodeKeyValue(networkPanel, "requestCookieContent", "foo", "bar"); 1.248 + checkNodeKeyValue(networkPanel, "requestCookieContent", "hello", "world"); 1.249 + checkNodeContent(networkPanel, "responseBodyContent", "get out here"); 1.250 + checkNodeContent(networkPanel, "responseBodyInfo", "2ms"); 1.251 + 1.252 + networkPanel.panel.hidePopup(); 1.253 + 1.254 + // Third run: Test for response cookies. 1.255 + info("test 6b: response cookies"); 1.256 + httpActivity.response.cookies.push( 1.257 + { name: "foobar", value: "boom" }, 1.258 + { name: "foobaz", value: "omg" } 1.259 + ); 1.260 + 1.261 + networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); 1.262 + is(filterBox._netPanel, networkPanel, 1.263 + "Network panel stored on httpActivity object"); 1.264 + 1.265 + networkPanel._onUpdate = function() { 1.266 + networkPanel._onUpdate = null; 1.267 + executeSoon(function() { 1.268 + testDriver.next(); 1.269 + }); 1.270 + }; 1.271 + 1.272 + yield undefined; 1.273 + 1.274 + checkIsVisible(networkPanel, { 1.275 + requestBody: true, 1.276 + requestFormData: false, 1.277 + requestCookie: true, 1.278 + responseContainer: true, 1.279 + responseCookie: true, 1.280 + responseBody: true, 1.281 + responseNoBody: false, 1.282 + responseImage: false, 1.283 + responseImageCached: false, 1.284 + responseBodyFetchLink: false, 1.285 + }); 1.286 + 1.287 + checkNodeKeyValue(networkPanel, "responseCookieContent", "foobar", "boom"); 1.288 + checkNodeKeyValue(networkPanel, "responseCookieContent", "foobaz", "omg"); 1.289 + 1.290 + networkPanel.panel.hidePopup(); 1.291 + 1.292 + // Check image request. 1.293 + info("test 7: image request"); 1.294 + httpActivity.response.headers[1].value = "image/png"; 1.295 + httpActivity.response.content.mimeType = "image/png"; 1.296 + httpActivity.response.content.text = TEST_IMG_BASE64; 1.297 + httpActivity.request.url = TEST_IMG; 1.298 + 1.299 + networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); 1.300 + networkPanel._onUpdate = function() { 1.301 + networkPanel._onUpdate = null; 1.302 + executeSoon(function() { 1.303 + testDriver.next(); 1.304 + }); 1.305 + }; 1.306 + 1.307 + yield undefined; 1.308 + 1.309 + checkIsVisible(networkPanel, { 1.310 + requestBody: true, 1.311 + requestFormData: false, 1.312 + requestCookie: true, 1.313 + responseContainer: true, 1.314 + responseBody: false, 1.315 + responseNoBody: false, 1.316 + responseImage: true, 1.317 + responseImageCached: false, 1.318 + responseBodyFetchLink: false, 1.319 + }); 1.320 + 1.321 + let imgNode = networkPanel.document.getElementById("responseImageNode"); 1.322 + is(imgNode.getAttribute("src"), "data:image/png;base64," + TEST_IMG_BASE64, 1.323 + "Displayed image is correct"); 1.324 + 1.325 + function checkImageResponseInfo() { 1.326 + checkNodeContent(networkPanel, "responseImageInfo", "2ms"); 1.327 + checkNodeContent(networkPanel, "responseImageInfo", "16x16px"); 1.328 + } 1.329 + 1.330 + // Check if the image is loaded already. 1.331 + imgNode.addEventListener("load", function onLoad() { 1.332 + imgNode.removeEventListener("load", onLoad, false); 1.333 + checkImageResponseInfo(); 1.334 + networkPanel.panel.hidePopup(); 1.335 + testDriver.next(); 1.336 + }, false); 1.337 + yield undefined; 1.338 + 1.339 + // Check cached image request. 1.340 + info("test 8: cached image request"); 1.341 + httpActivity.response.httpVersion = "HTTP/1.1"; 1.342 + httpActivity.response.status = 304; 1.343 + httpActivity.response.statusText = "Not Modified"; 1.344 + 1.345 + networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); 1.346 + networkPanel._onUpdate = function() { 1.347 + networkPanel._onUpdate = null; 1.348 + executeSoon(function() { 1.349 + testDriver.next(); 1.350 + }); 1.351 + }; 1.352 + 1.353 + yield undefined; 1.354 + 1.355 + checkIsVisible(networkPanel, { 1.356 + requestBody: true, 1.357 + requestFormData: false, 1.358 + requestCookie: true, 1.359 + responseContainer: true, 1.360 + responseBody: false, 1.361 + responseNoBody: false, 1.362 + responseImage: false, 1.363 + responseImageCached: true 1.364 + }); 1.365 + 1.366 + let imgNode = networkPanel.document.getElementById("responseImageCachedNode"); 1.367 + is(imgNode.getAttribute("src"), "data:image/png;base64," + TEST_IMG_BASE64, 1.368 + "Displayed image is correct"); 1.369 + 1.370 + networkPanel.panel.hidePopup(); 1.371 + 1.372 + // Test sent form data. 1.373 + info("test 9: sent form data"); 1.374 + httpActivity.request.postData.text = [ 1.375 + "Content-Type: application/x-www-form-urlencoded", 1.376 + "Content-Length: 59", 1.377 + "name=rob&age=20" 1.378 + ].join("\n"); 1.379 + 1.380 + networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); 1.381 + networkPanel._onUpdate = function() { 1.382 + networkPanel._onUpdate = null; 1.383 + executeSoon(function() { 1.384 + testDriver.next(); 1.385 + }); 1.386 + }; 1.387 + 1.388 + yield undefined; 1.389 + 1.390 + checkIsVisible(networkPanel, { 1.391 + requestBody: false, 1.392 + requestFormData: true, 1.393 + requestCookie: true, 1.394 + responseContainer: true, 1.395 + responseBody: false, 1.396 + responseNoBody: false, 1.397 + responseImage: false, 1.398 + responseImageCached: true 1.399 + }); 1.400 + 1.401 + checkNodeKeyValue(networkPanel, "requestFormDataContent", "name", "rob"); 1.402 + checkNodeKeyValue(networkPanel, "requestFormDataContent", "age", "20"); 1.403 + networkPanel.panel.hidePopup(); 1.404 + 1.405 + // Test no space after Content-Type: 1.406 + info("test 10: no space after Content-Type header in post data"); 1.407 + httpActivity.request.postData.text = "Content-Type:application/x-www-form-urlencoded\n"; 1.408 + 1.409 + networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); 1.410 + networkPanel._onUpdate = function() { 1.411 + networkPanel._onUpdate = null; 1.412 + executeSoon(function() { 1.413 + testDriver.next(); 1.414 + }); 1.415 + }; 1.416 + 1.417 + yield undefined; 1.418 + 1.419 + checkIsVisible(networkPanel, { 1.420 + requestBody: false, 1.421 + requestFormData: true, 1.422 + requestCookie: true, 1.423 + responseContainer: true, 1.424 + responseBody: false, 1.425 + responseNoBody: false, 1.426 + responseImage: false, 1.427 + responseImageCached: true 1.428 + }); 1.429 + 1.430 + networkPanel.panel.hidePopup(); 1.431 + 1.432 + // Test cached data. 1.433 + 1.434 + info("test 11: cached data"); 1.435 + 1.436 + httpActivity.request.url = TEST_ENCODING_ISO_8859_1; 1.437 + httpActivity.response.headers[1].value = "application/json"; 1.438 + httpActivity.response.content.mimeType = "application/json"; 1.439 + httpActivity.response.content.text = "my cached data is here!"; 1.440 + 1.441 + networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); 1.442 + networkPanel._onUpdate = function() { 1.443 + networkPanel._onUpdate = null; 1.444 + executeSoon(function() { 1.445 + testDriver.next(); 1.446 + }); 1.447 + }; 1.448 + 1.449 + yield undefined; 1.450 + 1.451 + checkIsVisible(networkPanel, { 1.452 + requestBody: false, 1.453 + requestFormData: true, 1.454 + requestCookie: true, 1.455 + responseContainer: true, 1.456 + responseBody: false, 1.457 + responseBodyCached: true, 1.458 + responseNoBody: false, 1.459 + responseImage: false, 1.460 + responseImageCached: false 1.461 + }); 1.462 + 1.463 + checkNodeContent(networkPanel, "responseBodyCachedContent", 1.464 + "my cached data is here!"); 1.465 + 1.466 + networkPanel.panel.hidePopup(); 1.467 + 1.468 + // Test a response with a content type that can't be displayed in the 1.469 + // NetworkPanel. 1.470 + info("test 12: unknown content type"); 1.471 + httpActivity.response.headers[1].value = "application/x-shockwave-flash"; 1.472 + httpActivity.response.content.mimeType = "application/x-shockwave-flash"; 1.473 + 1.474 + networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); 1.475 + networkPanel._onUpdate = function() { 1.476 + networkPanel._onUpdate = null; 1.477 + executeSoon(function() { 1.478 + testDriver.next(); 1.479 + }); 1.480 + }; 1.481 + 1.482 + yield undefined; 1.483 + 1.484 + checkIsVisible(networkPanel, { 1.485 + requestBody: false, 1.486 + requestFormData: true, 1.487 + requestCookie: true, 1.488 + responseContainer: true, 1.489 + responseBody: false, 1.490 + responseBodyCached: false, 1.491 + responseBodyUnknownType: true, 1.492 + responseNoBody: false, 1.493 + responseImage: false, 1.494 + responseImageCached: false 1.495 + }); 1.496 + 1.497 + let responseString = 1.498 + WCU_l10n.getFormatStr("NetworkPanel.responseBodyUnableToDisplay.content", 1.499 + ["application/x-shockwave-flash"]); 1.500 + checkNodeContent(networkPanel, "responseBodyUnknownTypeContent", responseString); 1.501 + networkPanel.panel.hidePopup(); 1.502 + 1.503 + /* 1.504 + 1.505 + // This test disabled. See bug 603620. 1.506 + 1.507 + // Test if the NetworkPanel figures out the content type based on an URL as 1.508 + // well. 1.509 + delete httpActivity.response.header["Content-Type"]; 1.510 + httpActivity.url = "http://www.test.com/someCrazyFile.swf?done=right&ending=txt"; 1.511 + 1.512 + networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); 1.513 + networkPanel.isDoneCallback = function NP_doneCallback() { 1.514 + networkPanel.isDoneCallback = null; 1.515 + testDriver.next(); 1.516 + } 1.517 + 1.518 + yield undefined; 1.519 + 1.520 + checkIsVisible(networkPanel, { 1.521 + requestBody: false, 1.522 + requestFormData: true, 1.523 + requestCookie: true, 1.524 + responseContainer: true, 1.525 + responseBody: false, 1.526 + responseBodyCached: false, 1.527 + responseBodyUnknownType: true, 1.528 + responseNoBody: false, 1.529 + responseImage: false, 1.530 + responseImageCached: false 1.531 + }); 1.532 + 1.533 + // Systems without Flash installed will return an empty string here. Ignore. 1.534 + if (networkPanel.document.getElementById("responseBodyUnknownTypeContent").textContent !== "") 1.535 + checkNodeContent(networkPanel, "responseBodyUnknownTypeContent", responseString); 1.536 + else 1.537 + ok(true, "Flash not installed"); 1.538 + 1.539 + networkPanel.panel.hidePopup(); */ 1.540 + 1.541 + // All done! 1.542 + testDriver = null; 1.543 + executeSoon(finishTest); 1.544 + 1.545 + yield undefined; 1.546 +}