browser/devtools/webconsole/test/browser_webconsole_network_panel.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* vim:set ts=2 sw=2 sts=2 et: */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 // Tests that the network panel works.
     8 const TEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/test-console.html";
     9 const TEST_IMG = "http://example.com/browser/browser/devtools/webconsole/test/test-image.png";
    10 const TEST_ENCODING_ISO_8859_1 = "http://example.com/browser/browser/devtools/webconsole/test/test-encoding-ISO-8859-1.html";
    12 const TEST_IMG_BASE64 =
    13   "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAVRJREFU" +
    14   "OI2lk7FLw0AUxr+YpC1CBqcMWfsvCCLdXFzqEJCgDl1EQRGxg9AhSBEJONhFhG52UCuFDjq5dxD8" +
    15   "FwoO0qGDOBQkl7vLOeWa2EQDffDBvTu+373Hu1OEEJgntGgxGD6J+7fLXKbt5VNUyhsKAChRBQcP" +
    16   "FVFeWskFGH694mZroCQqCLlAwPxcgJBP254CmAD5B7C7dgHLMLF3uzoL4DQEod+Z5sP1FizDxGgy" +
    17   "BqfhLID9AahX29J89bwPFgMsSEAQglAf9WobhPpScbPXr4FQHyzIADTsDizDRMPuIOC+zEeTMZo9" +
    18   "BwH3EfAMACccbtfGaDKGZZg423yUZrdrg3EqxQlPr0BTdTR7joREN2uqnlBmCwW1hIJagtev4f3z" +
    19   "A16/JvfiigMSYyzqJXlw/XKUyOORMUaBor6YavgdjKa8xGOnidadmwtwsnMu18q83/kHSou+bFND" +
    20   "Dr4AAAAASUVORK5CYII=";
    22 let testDriver;
    24 function test() {
    25   addTab(TEST_URI);
    26   browser.addEventListener("load", function onLoad() {
    27     browser.removeEventListener("load", onLoad, true);
    28     openConsole(null, testNetworkPanel);
    29   }, true);
    30 }
    32 function testNetworkPanel() {
    33   testDriver = testGen();
    34   testDriver.next();
    35 }
    37 function checkIsVisible(aPanel, aList) {
    38   for (let id in aList) {
    39     let node = aPanel.document.getElementById(id);
    40     let isVisible = aList[id];
    41     is(node.style.display, (isVisible ? "block" : "none"), id + " isVisible=" + isVisible);
    42   }
    43 }
    45 function checkNodeContent(aPanel, aId, aContent) {
    46   let node = aPanel.document.getElementById(aId);
    47   if (node == null) {
    48     ok(false, "Tried to access node " + aId + " that doesn't exist!");
    49   }
    50   else if (node.textContent.indexOf(aContent) != -1) {
    51     ok(true, "checking content of " + aId);
    52   }
    53   else {
    54     ok(false, "Got false value for " + aId + ": " + node.textContent + " doesn't have " + aContent);
    55   }
    56 }
    58 function checkNodeKeyValue(aPanel, aId, aKey, aValue) {
    59   let node = aPanel.document.getElementById(aId);
    61   let headers = node.querySelectorAll("th");
    62   for (let i = 0; i < headers.length; i++) {
    63     if (headers[i].textContent == (aKey + ":")) {
    64       is(headers[i].nextElementSibling.textContent, aValue,
    65          "checking content of " + aId + " for key " + aKey);
    66       return;
    67     }
    68   }
    70   ok(false, "content check failed for " + aId + ", key " + aKey);
    71 }
    73 function testGen() {
    74   let hud = HUDService.getHudByWindow(content);
    75   let filterBox = hud.ui.filterBox;
    77   let httpActivity = {
    78     updates: [],
    79     discardRequestBody: true,
    80     discardResponseBody: true,
    81     startedDateTime: (new Date()).toISOString(),
    82     request: {
    83       url: "http://www.testpage.com",
    84       method: "GET",
    85       cookies: [],
    86       headers: [
    87         { name: "foo", value: "bar" },
    88       ],
    89     },
    90     response: {
    91       headers: [],
    92       content: {},
    93       cookies: [],
    94     },
    95     timings: {},
    96   };
    98   let networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity);
   100   is(filterBox._netPanel, networkPanel,
   101      "Network panel stored on the anchor object");
   103   networkPanel._onUpdate = function() {
   104     networkPanel._onUpdate = null;
   105     executeSoon(function() {
   106       testDriver.next();
   107     });
   108   };
   110   yield undefined;
   112   info("test 1");
   114   checkIsVisible(networkPanel, {
   115     requestCookie: false,
   116     requestFormData: false,
   117     requestBody: false,
   118     responseContainer: false,
   119     responseBody: false,
   120     responseNoBody: false,
   121     responseImage: false,
   122     responseImageCached: false
   123   });
   125   checkNodeContent(networkPanel, "header", "http://www.testpage.com");
   126   checkNodeContent(networkPanel, "header", "GET");
   127   checkNodeKeyValue(networkPanel, "requestHeadersContent", "foo", "bar");
   129   // Test request body.
   130   info("test 2: request body");
   131   httpActivity.discardRequestBody = false;
   132   httpActivity.request.postData = { text: "hello world" };
   133   networkPanel.update();
   135   checkIsVisible(networkPanel, {
   136     requestBody: true,
   137     requestFormData: false,
   138     requestCookie: false,
   139     responseContainer: false,
   140     responseBody: false,
   141     responseNoBody: false,
   142     responseImage: false,
   143     responseImageCached: false
   144   });
   145   checkNodeContent(networkPanel, "requestBodyContent", "hello world");
   147   // Test response header.
   148   info("test 3: response header");
   149   httpActivity.timings.wait = 10;
   150   httpActivity.response.httpVersion = "HTTP/3.14";
   151   httpActivity.response.status = 999;
   152   httpActivity.response.statusText = "earthquake win";
   153   httpActivity.response.content.mimeType = "text/html";
   154   httpActivity.response.headers.push(
   155     { name: "Content-Type", value: "text/html" },
   156     { name: "leaveHouses", value: "true" }
   157   );
   159   networkPanel.update();
   161   checkIsVisible(networkPanel, {
   162     requestBody: true,
   163     requestFormData: false,
   164     requestCookie: false,
   165     responseContainer: true,
   166     responseBody: false,
   167     responseNoBody: false,
   168     responseImage: false,
   169     responseImageCached: false
   170   });
   172   checkNodeContent(networkPanel, "header", "HTTP/3.14 999 earthquake win");
   173   checkNodeKeyValue(networkPanel, "responseHeadersContent", "leaveHouses", "true");
   174   checkNodeContent(networkPanel, "responseHeadersInfo", "10ms");
   176   info("test 4");
   178   httpActivity.discardResponseBody = false;
   179   httpActivity.timings.receive = 2;
   180   networkPanel.update();
   182   checkIsVisible(networkPanel, {
   183     requestBody: true,
   184     requestCookie: false,
   185     requestFormData: false,
   186     responseContainer: true,
   187     responseBody: false,
   188     responseNoBody: false,
   189     responseImage: false,
   190     responseImageCached: false
   191   });
   193   info("test 5");
   195   httpActivity.updates.push("responseContent", "eventTimings");
   196   networkPanel.update();
   198   checkNodeContent(networkPanel, "responseNoBodyInfo", "2ms");
   199   checkIsVisible(networkPanel, {
   200     requestBody: true,
   201     requestCookie: false,
   202     responseContainer: true,
   203     responseBody: false,
   204     responseNoBody: true,
   205     responseImage: false,
   206     responseImageCached: false
   207   });
   209   networkPanel.panel.hidePopup();
   211   // Second run: Test for cookies and response body.
   212   info("test 6: cookies and response body");
   213   httpActivity.request.cookies.push(
   214     { name: "foo", value: "bar" },
   215     { name: "hello", value: "world" }
   216   );
   217   httpActivity.response.content.text = "get out here";
   219   networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity);
   220   is(filterBox._netPanel, networkPanel,
   221      "Network panel stored on httpActivity object");
   223   networkPanel._onUpdate = function() {
   224     networkPanel._onUpdate = null;
   225     executeSoon(function() {
   226       testDriver.next();
   227     });
   228   };
   230   yield undefined;
   232   checkIsVisible(networkPanel, {
   233     requestBody: true,
   234     requestFormData: false,
   235     requestCookie: true,
   236     responseContainer: true,
   237     responseCookie: false,
   238     responseBody: true,
   239     responseNoBody: false,
   240     responseImage: false,
   241     responseImageCached: false
   242   });
   244   checkNodeKeyValue(networkPanel, "requestCookieContent", "foo", "bar");
   245   checkNodeKeyValue(networkPanel, "requestCookieContent", "hello", "world");
   246   checkNodeContent(networkPanel, "responseBodyContent", "get out here");
   247   checkNodeContent(networkPanel, "responseBodyInfo", "2ms");
   249   networkPanel.panel.hidePopup();
   251   // Third run: Test for response cookies.
   252   info("test 6b: response cookies");
   253   httpActivity.response.cookies.push(
   254     { name: "foobar", value: "boom" },
   255     { name: "foobaz", value: "omg" }
   256   );
   258   networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity);
   259   is(filterBox._netPanel, networkPanel,
   260      "Network panel stored on httpActivity object");
   262   networkPanel._onUpdate = function() {
   263     networkPanel._onUpdate = null;
   264     executeSoon(function() {
   265       testDriver.next();
   266     });
   267   };
   269   yield undefined;
   271   checkIsVisible(networkPanel, {
   272     requestBody: true,
   273     requestFormData: false,
   274     requestCookie: true,
   275     responseContainer: true,
   276     responseCookie: true,
   277     responseBody: true,
   278     responseNoBody: false,
   279     responseImage: false,
   280     responseImageCached: false,
   281     responseBodyFetchLink: false,
   282   });
   284   checkNodeKeyValue(networkPanel, "responseCookieContent", "foobar", "boom");
   285   checkNodeKeyValue(networkPanel, "responseCookieContent", "foobaz", "omg");
   287   networkPanel.panel.hidePopup();
   289   // Check image request.
   290   info("test 7: image request");
   291   httpActivity.response.headers[1].value = "image/png";
   292   httpActivity.response.content.mimeType = "image/png";
   293   httpActivity.response.content.text = TEST_IMG_BASE64;
   294   httpActivity.request.url = TEST_IMG;
   296   networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity);
   297   networkPanel._onUpdate = function() {
   298     networkPanel._onUpdate = null;
   299     executeSoon(function() {
   300       testDriver.next();
   301     });
   302   };
   304   yield undefined;
   306   checkIsVisible(networkPanel, {
   307     requestBody: true,
   308     requestFormData: false,
   309     requestCookie: true,
   310     responseContainer: true,
   311     responseBody: false,
   312     responseNoBody: false,
   313     responseImage: true,
   314     responseImageCached: false,
   315     responseBodyFetchLink: false,
   316   });
   318   let imgNode = networkPanel.document.getElementById("responseImageNode");
   319   is(imgNode.getAttribute("src"), "data:image/png;base64," + TEST_IMG_BASE64,
   320       "Displayed image is correct");
   322   function checkImageResponseInfo() {
   323     checkNodeContent(networkPanel, "responseImageInfo", "2ms");
   324     checkNodeContent(networkPanel, "responseImageInfo", "16x16px");
   325   }
   327   // Check if the image is loaded already.
   328   imgNode.addEventListener("load", function onLoad() {
   329     imgNode.removeEventListener("load", onLoad, false);
   330     checkImageResponseInfo();
   331     networkPanel.panel.hidePopup();
   332     testDriver.next();
   333   }, false);
   334   yield undefined;
   336   // Check cached image request.
   337   info("test 8: cached image request");
   338   httpActivity.response.httpVersion = "HTTP/1.1";
   339   httpActivity.response.status = 304;
   340   httpActivity.response.statusText = "Not Modified";
   342   networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity);
   343   networkPanel._onUpdate = function() {
   344     networkPanel._onUpdate = null;
   345     executeSoon(function() {
   346       testDriver.next();
   347     });
   348   };
   350   yield undefined;
   352   checkIsVisible(networkPanel, {
   353     requestBody: true,
   354     requestFormData: false,
   355     requestCookie: true,
   356     responseContainer: true,
   357     responseBody: false,
   358     responseNoBody: false,
   359     responseImage: false,
   360     responseImageCached: true
   361   });
   363   let imgNode = networkPanel.document.getElementById("responseImageCachedNode");
   364   is(imgNode.getAttribute("src"), "data:image/png;base64," + TEST_IMG_BASE64,
   365      "Displayed image is correct");
   367   networkPanel.panel.hidePopup();
   369   // Test sent form data.
   370   info("test 9: sent form data");
   371   httpActivity.request.postData.text = [
   372     "Content-Type:      application/x-www-form-urlencoded",
   373     "Content-Length: 59",
   374     "name=rob&age=20"
   375   ].join("\n");
   377   networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity);
   378   networkPanel._onUpdate = function() {
   379     networkPanel._onUpdate = null;
   380     executeSoon(function() {
   381       testDriver.next();
   382     });
   383   };
   385   yield undefined;
   387   checkIsVisible(networkPanel, {
   388     requestBody: false,
   389     requestFormData: true,
   390     requestCookie: true,
   391     responseContainer: true,
   392     responseBody: false,
   393     responseNoBody: false,
   394     responseImage: false,
   395     responseImageCached: true
   396   });
   398   checkNodeKeyValue(networkPanel, "requestFormDataContent", "name", "rob");
   399   checkNodeKeyValue(networkPanel, "requestFormDataContent", "age", "20");
   400   networkPanel.panel.hidePopup();
   402   // Test no space after Content-Type:
   403   info("test 10: no space after Content-Type header in post data");
   404   httpActivity.request.postData.text = "Content-Type:application/x-www-form-urlencoded\n";
   406   networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity);
   407   networkPanel._onUpdate = function() {
   408     networkPanel._onUpdate = null;
   409     executeSoon(function() {
   410       testDriver.next();
   411     });
   412   };
   414   yield undefined;
   416   checkIsVisible(networkPanel, {
   417     requestBody: false,
   418     requestFormData: true,
   419     requestCookie: true,
   420     responseContainer: true,
   421     responseBody: false,
   422     responseNoBody: false,
   423     responseImage: false,
   424     responseImageCached: true
   425   });
   427   networkPanel.panel.hidePopup();
   429   // Test cached data.
   431   info("test 11: cached data");
   433   httpActivity.request.url = TEST_ENCODING_ISO_8859_1;
   434   httpActivity.response.headers[1].value = "application/json";
   435   httpActivity.response.content.mimeType = "application/json";
   436   httpActivity.response.content.text = "my cached data is here!";
   438   networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity);
   439   networkPanel._onUpdate = function() {
   440     networkPanel._onUpdate = null;
   441     executeSoon(function() {
   442       testDriver.next();
   443     });
   444   };
   446   yield undefined;
   448   checkIsVisible(networkPanel, {
   449     requestBody: false,
   450     requestFormData: true,
   451     requestCookie: true,
   452     responseContainer: true,
   453     responseBody: false,
   454     responseBodyCached: true,
   455     responseNoBody: false,
   456     responseImage: false,
   457     responseImageCached: false
   458   });
   460   checkNodeContent(networkPanel, "responseBodyCachedContent",
   461                    "my cached data is here!");
   463   networkPanel.panel.hidePopup();
   465   // Test a response with a content type that can't be displayed in the
   466   // NetworkPanel.
   467   info("test 12: unknown content type");
   468   httpActivity.response.headers[1].value = "application/x-shockwave-flash";
   469   httpActivity.response.content.mimeType = "application/x-shockwave-flash";
   471   networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity);
   472   networkPanel._onUpdate = function() {
   473     networkPanel._onUpdate = null;
   474     executeSoon(function() {
   475       testDriver.next();
   476     });
   477   };
   479   yield undefined;
   481   checkIsVisible(networkPanel, {
   482     requestBody: false,
   483     requestFormData: true,
   484     requestCookie: true,
   485     responseContainer: true,
   486     responseBody: false,
   487     responseBodyCached: false,
   488     responseBodyUnknownType: true,
   489     responseNoBody: false,
   490     responseImage: false,
   491     responseImageCached: false
   492   });
   494   let responseString =
   495     WCU_l10n.getFormatStr("NetworkPanel.responseBodyUnableToDisplay.content",
   496                       ["application/x-shockwave-flash"]);
   497   checkNodeContent(networkPanel, "responseBodyUnknownTypeContent", responseString);
   498   networkPanel.panel.hidePopup();
   500   /*
   502   // This test disabled. See bug 603620.
   504   // Test if the NetworkPanel figures out the content type based on an URL as
   505   // well.
   506   delete httpActivity.response.header["Content-Type"];
   507   httpActivity.url = "http://www.test.com/someCrazyFile.swf?done=right&ending=txt";
   509   networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity);
   510   networkPanel.isDoneCallback = function NP_doneCallback() {
   511     networkPanel.isDoneCallback = null;
   512     testDriver.next();
   513   }
   515   yield undefined;
   517   checkIsVisible(networkPanel, {
   518     requestBody: false,
   519     requestFormData: true,
   520     requestCookie: true,
   521     responseContainer: true,
   522     responseBody: false,
   523     responseBodyCached: false,
   524     responseBodyUnknownType: true,
   525     responseNoBody: false,
   526     responseImage: false,
   527     responseImageCached: false
   528   });
   530   // Systems without Flash installed will return an empty string here. Ignore.
   531   if (networkPanel.document.getElementById("responseBodyUnknownTypeContent").textContent !== "")
   532     checkNodeContent(networkPanel, "responseBodyUnknownTypeContent", responseString);
   533   else
   534     ok(true, "Flash not installed");
   536   networkPanel.panel.hidePopup(); */
   538   // All done!
   539   testDriver = null;
   540   executeSoon(finishTest);
   542   yield undefined;
   543 }

mercurial