michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Tests that the network panel works. michael@0: michael@0: const TEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/test-console.html"; michael@0: const TEST_IMG = "http://example.com/browser/browser/devtools/webconsole/test/test-image.png"; michael@0: const TEST_ENCODING_ISO_8859_1 = "http://example.com/browser/browser/devtools/webconsole/test/test-encoding-ISO-8859-1.html"; michael@0: michael@0: const TEST_IMG_BASE64 = michael@0: "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAVRJREFU" + michael@0: "OI2lk7FLw0AUxr+YpC1CBqcMWfsvCCLdXFzqEJCgDl1EQRGxg9AhSBEJONhFhG52UCuFDjq5dxD8" + michael@0: "FwoO0qGDOBQkl7vLOeWa2EQDffDBvTu+373Hu1OEEJgntGgxGD6J+7fLXKbt5VNUyhsKAChRBQcP" + michael@0: "FVFeWskFGH694mZroCQqCLlAwPxcgJBP254CmAD5B7C7dgHLMLF3uzoL4DQEod+Z5sP1FizDxGgy" + michael@0: "BqfhLID9AahX29J89bwPFgMsSEAQglAf9WobhPpScbPXr4FQHyzIADTsDizDRMPuIOC+zEeTMZo9" + michael@0: "BwH3EfAMACccbtfGaDKGZZg423yUZrdrg3EqxQlPr0BTdTR7joREN2uqnlBmCwW1hIJagtev4f3z" + michael@0: "A16/JvfiigMSYyzqJXlw/XKUyOORMUaBor6YavgdjKa8xGOnidadmwtwsnMu18q83/kHSou+bFND" + michael@0: "Dr4AAAAASUVORK5CYII="; michael@0: michael@0: let testDriver; michael@0: michael@0: function test() { michael@0: addTab(TEST_URI); michael@0: browser.addEventListener("load", function onLoad() { michael@0: browser.removeEventListener("load", onLoad, true); michael@0: openConsole(null, testNetworkPanel); michael@0: }, true); michael@0: } michael@0: michael@0: function testNetworkPanel() { michael@0: testDriver = testGen(); michael@0: testDriver.next(); michael@0: } michael@0: michael@0: function checkIsVisible(aPanel, aList) { michael@0: for (let id in aList) { michael@0: let node = aPanel.document.getElementById(id); michael@0: let isVisible = aList[id]; michael@0: is(node.style.display, (isVisible ? "block" : "none"), id + " isVisible=" + isVisible); michael@0: } michael@0: } michael@0: michael@0: function checkNodeContent(aPanel, aId, aContent) { michael@0: let node = aPanel.document.getElementById(aId); michael@0: if (node == null) { michael@0: ok(false, "Tried to access node " + aId + " that doesn't exist!"); michael@0: } michael@0: else if (node.textContent.indexOf(aContent) != -1) { michael@0: ok(true, "checking content of " + aId); michael@0: } michael@0: else { michael@0: ok(false, "Got false value for " + aId + ": " + node.textContent + " doesn't have " + aContent); michael@0: } michael@0: } michael@0: michael@0: function checkNodeKeyValue(aPanel, aId, aKey, aValue) { michael@0: let node = aPanel.document.getElementById(aId); michael@0: michael@0: let headers = node.querySelectorAll("th"); michael@0: for (let i = 0; i < headers.length; i++) { michael@0: if (headers[i].textContent == (aKey + ":")) { michael@0: is(headers[i].nextElementSibling.textContent, aValue, michael@0: "checking content of " + aId + " for key " + aKey); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: ok(false, "content check failed for " + aId + ", key " + aKey); michael@0: } michael@0: michael@0: function testGen() { michael@0: let hud = HUDService.getHudByWindow(content); michael@0: let filterBox = hud.ui.filterBox; michael@0: michael@0: let httpActivity = { michael@0: updates: [], michael@0: discardRequestBody: true, michael@0: discardResponseBody: true, michael@0: startedDateTime: (new Date()).toISOString(), michael@0: request: { michael@0: url: "http://www.testpage.com", michael@0: method: "GET", michael@0: cookies: [], michael@0: headers: [ michael@0: { name: "foo", value: "bar" }, michael@0: ], michael@0: }, michael@0: response: { michael@0: headers: [], michael@0: content: {}, michael@0: cookies: [], michael@0: }, michael@0: timings: {}, michael@0: }; michael@0: michael@0: let networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); michael@0: michael@0: is(filterBox._netPanel, networkPanel, michael@0: "Network panel stored on the anchor object"); michael@0: michael@0: networkPanel._onUpdate = function() { michael@0: networkPanel._onUpdate = null; michael@0: executeSoon(function() { michael@0: testDriver.next(); michael@0: }); michael@0: }; michael@0: michael@0: yield undefined; michael@0: michael@0: info("test 1"); michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestCookie: false, michael@0: requestFormData: false, michael@0: requestBody: false, michael@0: responseContainer: false, michael@0: responseBody: false, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: false michael@0: }); michael@0: michael@0: checkNodeContent(networkPanel, "header", "http://www.testpage.com"); michael@0: checkNodeContent(networkPanel, "header", "GET"); michael@0: checkNodeKeyValue(networkPanel, "requestHeadersContent", "foo", "bar"); michael@0: michael@0: // Test request body. michael@0: info("test 2: request body"); michael@0: httpActivity.discardRequestBody = false; michael@0: httpActivity.request.postData = { text: "hello world" }; michael@0: networkPanel.update(); michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: true, michael@0: requestFormData: false, michael@0: requestCookie: false, michael@0: responseContainer: false, michael@0: responseBody: false, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: false michael@0: }); michael@0: checkNodeContent(networkPanel, "requestBodyContent", "hello world"); michael@0: michael@0: // Test response header. michael@0: info("test 3: response header"); michael@0: httpActivity.timings.wait = 10; michael@0: httpActivity.response.httpVersion = "HTTP/3.14"; michael@0: httpActivity.response.status = 999; michael@0: httpActivity.response.statusText = "earthquake win"; michael@0: httpActivity.response.content.mimeType = "text/html"; michael@0: httpActivity.response.headers.push( michael@0: { name: "Content-Type", value: "text/html" }, michael@0: { name: "leaveHouses", value: "true" } michael@0: ); michael@0: michael@0: networkPanel.update(); michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: true, michael@0: requestFormData: false, michael@0: requestCookie: false, michael@0: responseContainer: true, michael@0: responseBody: false, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: false michael@0: }); michael@0: michael@0: checkNodeContent(networkPanel, "header", "HTTP/3.14 999 earthquake win"); michael@0: checkNodeKeyValue(networkPanel, "responseHeadersContent", "leaveHouses", "true"); michael@0: checkNodeContent(networkPanel, "responseHeadersInfo", "10ms"); michael@0: michael@0: info("test 4"); michael@0: michael@0: httpActivity.discardResponseBody = false; michael@0: httpActivity.timings.receive = 2; michael@0: networkPanel.update(); michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: true, michael@0: requestCookie: false, michael@0: requestFormData: false, michael@0: responseContainer: true, michael@0: responseBody: false, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: false michael@0: }); michael@0: michael@0: info("test 5"); michael@0: michael@0: httpActivity.updates.push("responseContent", "eventTimings"); michael@0: networkPanel.update(); michael@0: michael@0: checkNodeContent(networkPanel, "responseNoBodyInfo", "2ms"); michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: true, michael@0: requestCookie: false, michael@0: responseContainer: true, michael@0: responseBody: false, michael@0: responseNoBody: true, michael@0: responseImage: false, michael@0: responseImageCached: false michael@0: }); michael@0: michael@0: networkPanel.panel.hidePopup(); michael@0: michael@0: // Second run: Test for cookies and response body. michael@0: info("test 6: cookies and response body"); michael@0: httpActivity.request.cookies.push( michael@0: { name: "foo", value: "bar" }, michael@0: { name: "hello", value: "world" } michael@0: ); michael@0: httpActivity.response.content.text = "get out here"; michael@0: michael@0: networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); michael@0: is(filterBox._netPanel, networkPanel, michael@0: "Network panel stored on httpActivity object"); michael@0: michael@0: networkPanel._onUpdate = function() { michael@0: networkPanel._onUpdate = null; michael@0: executeSoon(function() { michael@0: testDriver.next(); michael@0: }); michael@0: }; michael@0: michael@0: yield undefined; michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: true, michael@0: requestFormData: false, michael@0: requestCookie: true, michael@0: responseContainer: true, michael@0: responseCookie: false, michael@0: responseBody: true, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: false michael@0: }); michael@0: michael@0: checkNodeKeyValue(networkPanel, "requestCookieContent", "foo", "bar"); michael@0: checkNodeKeyValue(networkPanel, "requestCookieContent", "hello", "world"); michael@0: checkNodeContent(networkPanel, "responseBodyContent", "get out here"); michael@0: checkNodeContent(networkPanel, "responseBodyInfo", "2ms"); michael@0: michael@0: networkPanel.panel.hidePopup(); michael@0: michael@0: // Third run: Test for response cookies. michael@0: info("test 6b: response cookies"); michael@0: httpActivity.response.cookies.push( michael@0: { name: "foobar", value: "boom" }, michael@0: { name: "foobaz", value: "omg" } michael@0: ); michael@0: michael@0: networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); michael@0: is(filterBox._netPanel, networkPanel, michael@0: "Network panel stored on httpActivity object"); michael@0: michael@0: networkPanel._onUpdate = function() { michael@0: networkPanel._onUpdate = null; michael@0: executeSoon(function() { michael@0: testDriver.next(); michael@0: }); michael@0: }; michael@0: michael@0: yield undefined; michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: true, michael@0: requestFormData: false, michael@0: requestCookie: true, michael@0: responseContainer: true, michael@0: responseCookie: true, michael@0: responseBody: true, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: false, michael@0: responseBodyFetchLink: false, michael@0: }); michael@0: michael@0: checkNodeKeyValue(networkPanel, "responseCookieContent", "foobar", "boom"); michael@0: checkNodeKeyValue(networkPanel, "responseCookieContent", "foobaz", "omg"); michael@0: michael@0: networkPanel.panel.hidePopup(); michael@0: michael@0: // Check image request. michael@0: info("test 7: image request"); michael@0: httpActivity.response.headers[1].value = "image/png"; michael@0: httpActivity.response.content.mimeType = "image/png"; michael@0: httpActivity.response.content.text = TEST_IMG_BASE64; michael@0: httpActivity.request.url = TEST_IMG; michael@0: michael@0: networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); michael@0: networkPanel._onUpdate = function() { michael@0: networkPanel._onUpdate = null; michael@0: executeSoon(function() { michael@0: testDriver.next(); michael@0: }); michael@0: }; michael@0: michael@0: yield undefined; michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: true, michael@0: requestFormData: false, michael@0: requestCookie: true, michael@0: responseContainer: true, michael@0: responseBody: false, michael@0: responseNoBody: false, michael@0: responseImage: true, michael@0: responseImageCached: false, michael@0: responseBodyFetchLink: false, michael@0: }); michael@0: michael@0: let imgNode = networkPanel.document.getElementById("responseImageNode"); michael@0: is(imgNode.getAttribute("src"), "data:image/png;base64," + TEST_IMG_BASE64, michael@0: "Displayed image is correct"); michael@0: michael@0: function checkImageResponseInfo() { michael@0: checkNodeContent(networkPanel, "responseImageInfo", "2ms"); michael@0: checkNodeContent(networkPanel, "responseImageInfo", "16x16px"); michael@0: } michael@0: michael@0: // Check if the image is loaded already. michael@0: imgNode.addEventListener("load", function onLoad() { michael@0: imgNode.removeEventListener("load", onLoad, false); michael@0: checkImageResponseInfo(); michael@0: networkPanel.panel.hidePopup(); michael@0: testDriver.next(); michael@0: }, false); michael@0: yield undefined; michael@0: michael@0: // Check cached image request. michael@0: info("test 8: cached image request"); michael@0: httpActivity.response.httpVersion = "HTTP/1.1"; michael@0: httpActivity.response.status = 304; michael@0: httpActivity.response.statusText = "Not Modified"; michael@0: michael@0: networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); michael@0: networkPanel._onUpdate = function() { michael@0: networkPanel._onUpdate = null; michael@0: executeSoon(function() { michael@0: testDriver.next(); michael@0: }); michael@0: }; michael@0: michael@0: yield undefined; michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: true, michael@0: requestFormData: false, michael@0: requestCookie: true, michael@0: responseContainer: true, michael@0: responseBody: false, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: true michael@0: }); michael@0: michael@0: let imgNode = networkPanel.document.getElementById("responseImageCachedNode"); michael@0: is(imgNode.getAttribute("src"), "data:image/png;base64," + TEST_IMG_BASE64, michael@0: "Displayed image is correct"); michael@0: michael@0: networkPanel.panel.hidePopup(); michael@0: michael@0: // Test sent form data. michael@0: info("test 9: sent form data"); michael@0: httpActivity.request.postData.text = [ michael@0: "Content-Type: application/x-www-form-urlencoded", michael@0: "Content-Length: 59", michael@0: "name=rob&age=20" michael@0: ].join("\n"); michael@0: michael@0: networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); michael@0: networkPanel._onUpdate = function() { michael@0: networkPanel._onUpdate = null; michael@0: executeSoon(function() { michael@0: testDriver.next(); michael@0: }); michael@0: }; michael@0: michael@0: yield undefined; michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: false, michael@0: requestFormData: true, michael@0: requestCookie: true, michael@0: responseContainer: true, michael@0: responseBody: false, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: true michael@0: }); michael@0: michael@0: checkNodeKeyValue(networkPanel, "requestFormDataContent", "name", "rob"); michael@0: checkNodeKeyValue(networkPanel, "requestFormDataContent", "age", "20"); michael@0: networkPanel.panel.hidePopup(); michael@0: michael@0: // Test no space after Content-Type: michael@0: info("test 10: no space after Content-Type header in post data"); michael@0: httpActivity.request.postData.text = "Content-Type:application/x-www-form-urlencoded\n"; michael@0: michael@0: networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); michael@0: networkPanel._onUpdate = function() { michael@0: networkPanel._onUpdate = null; michael@0: executeSoon(function() { michael@0: testDriver.next(); michael@0: }); michael@0: }; michael@0: michael@0: yield undefined; michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: false, michael@0: requestFormData: true, michael@0: requestCookie: true, michael@0: responseContainer: true, michael@0: responseBody: false, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: true michael@0: }); michael@0: michael@0: networkPanel.panel.hidePopup(); michael@0: michael@0: // Test cached data. michael@0: michael@0: info("test 11: cached data"); michael@0: michael@0: httpActivity.request.url = TEST_ENCODING_ISO_8859_1; michael@0: httpActivity.response.headers[1].value = "application/json"; michael@0: httpActivity.response.content.mimeType = "application/json"; michael@0: httpActivity.response.content.text = "my cached data is here!"; michael@0: michael@0: networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); michael@0: networkPanel._onUpdate = function() { michael@0: networkPanel._onUpdate = null; michael@0: executeSoon(function() { michael@0: testDriver.next(); michael@0: }); michael@0: }; michael@0: michael@0: yield undefined; michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: false, michael@0: requestFormData: true, michael@0: requestCookie: true, michael@0: responseContainer: true, michael@0: responseBody: false, michael@0: responseBodyCached: true, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: false michael@0: }); michael@0: michael@0: checkNodeContent(networkPanel, "responseBodyCachedContent", michael@0: "my cached data is here!"); michael@0: michael@0: networkPanel.panel.hidePopup(); michael@0: michael@0: // Test a response with a content type that can't be displayed in the michael@0: // NetworkPanel. michael@0: info("test 12: unknown content type"); michael@0: httpActivity.response.headers[1].value = "application/x-shockwave-flash"; michael@0: httpActivity.response.content.mimeType = "application/x-shockwave-flash"; michael@0: michael@0: networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); michael@0: networkPanel._onUpdate = function() { michael@0: networkPanel._onUpdate = null; michael@0: executeSoon(function() { michael@0: testDriver.next(); michael@0: }); michael@0: }; michael@0: michael@0: yield undefined; michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: false, michael@0: requestFormData: true, michael@0: requestCookie: true, michael@0: responseContainer: true, michael@0: responseBody: false, michael@0: responseBodyCached: false, michael@0: responseBodyUnknownType: true, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: false michael@0: }); michael@0: michael@0: let responseString = michael@0: WCU_l10n.getFormatStr("NetworkPanel.responseBodyUnableToDisplay.content", michael@0: ["application/x-shockwave-flash"]); michael@0: checkNodeContent(networkPanel, "responseBodyUnknownTypeContent", responseString); michael@0: networkPanel.panel.hidePopup(); michael@0: michael@0: /* michael@0: michael@0: // This test disabled. See bug 603620. michael@0: michael@0: // Test if the NetworkPanel figures out the content type based on an URL as michael@0: // well. michael@0: delete httpActivity.response.header["Content-Type"]; michael@0: httpActivity.url = "http://www.test.com/someCrazyFile.swf?done=right&ending=txt"; michael@0: michael@0: networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); michael@0: networkPanel.isDoneCallback = function NP_doneCallback() { michael@0: networkPanel.isDoneCallback = null; michael@0: testDriver.next(); michael@0: } michael@0: michael@0: yield undefined; michael@0: michael@0: checkIsVisible(networkPanel, { michael@0: requestBody: false, michael@0: requestFormData: true, michael@0: requestCookie: true, michael@0: responseContainer: true, michael@0: responseBody: false, michael@0: responseBodyCached: false, michael@0: responseBodyUnknownType: true, michael@0: responseNoBody: false, michael@0: responseImage: false, michael@0: responseImageCached: false michael@0: }); michael@0: michael@0: // Systems without Flash installed will return an empty string here. Ignore. michael@0: if (networkPanel.document.getElementById("responseBodyUnknownTypeContent").textContent !== "") michael@0: checkNodeContent(networkPanel, "responseBodyUnknownTypeContent", responseString); michael@0: else michael@0: ok(true, "Flash not installed"); michael@0: michael@0: networkPanel.panel.hidePopup(); */ michael@0: michael@0: // All done! michael@0: testDriver = null; michael@0: executeSoon(finishTest); michael@0: michael@0: yield undefined; michael@0: }