Bug 898760 - Twitter says connected but shows nothing: truly make all options of Http.jsm optional, r=Mossop.
authorPatrick Cloke <clokep@gmail.com>
Tue, 20 Aug 2013 20:16:28 -0400
changeset 144179 5afcb60af43be825e85c3f08c1b4e84e366b984b
parent 144178 bc75fd3095ebc1ba7ad73591a98736b242bc0ce3
child 144180 a26582e3c44d04539477cb3a23abfbfdc33a45a0
push id32897
push userclokep@gmail.com
push dateSat, 24 Aug 2013 00:56:31 +0000
treeherdermozilla-inbound@5afcb60af43b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMossop
bugs898760
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 898760 - Twitter says connected but shows nothing: truly make all options of Http.jsm optional, r=Mossop.
toolkit/modules/Http.jsm
toolkit/modules/tests/xpcshell/test_Http.js
--- a/toolkit/modules/Http.jsm
+++ b/toolkit/modules/Http.jsm
@@ -29,74 +29,72 @@ function percentEncode(aString)
  * Headers or post data are given as an array of arrays, for each each inner
  * array the first value is the key and the second is the value, e.g.
  *  [["key1", "value1"], ["key2", "value2"]].
  */
 function httpRequest(aUrl, aOptions) {
   let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
               .createInstance(Ci.nsIXMLHttpRequest);
   xhr.mozBackgroundRequest = true; // no error dialogs
-  let hasPostData = "postData" in aOptions && aOptions.postData;
-  xhr.open("method" in aOptions ? aOptions.method :
-           (hasPostData ? "POST" : "GET"), aUrl);
+  xhr.open(aOptions.method || (aOptions.postData ? "POST" : "GET"), aUrl);
   xhr.channel.loadFlags = Ci.nsIChannel.LOAD_ANONYMOUS | // don't send cookies
                           Ci.nsIChannel.LOAD_BYPASS_CACHE |
                           Ci.nsIChannel.INHIBIT_CACHING;
   xhr.onerror = function(aProgressEvent) {
-    if ("onError" in aOptions) {
+    if (aOptions.onError) {
       // adapted from toolkit/mozapps/extensions/nsBlocklistService.js
       let request = aProgressEvent.target;
       let status;
       try {
         // may throw (local file or timeout)
         status = request.status;
       }
       catch (e) {
         request = request.channel.QueryInterface(Ci.nsIRequest);
         status = request.status;
       }
       // When status is 0 we don't have a valid channel.
       let statusText = status ? request.statusText : "offline";
       aOptions.onError(statusText, null, this);
     }
   };
-  xhr.onload = function (aRequest) {
+  xhr.onload = function(aRequest) {
     try {
       let target = aRequest.target;
-      if ("logger" in aOptions)
+      if (aOptions.logger)
         aOptions.logger.debug("Received response: " + target.responseText);
       if (target.status < 200 || target.status >= 300) {
         let errorText = target.responseText;
         if (!errorText || /<(ht|\?x)ml\b/i.test(errorText))
           errorText = target.statusText;
         throw target.status + " - " + errorText;
       }
-      if ("onLoad" in aOptions)
+      if (aOptions.onLoad)
         aOptions.onLoad(target.responseText, this);
     } catch (e) {
       Cu.reportError(e);
-      if ("onError" in aOptions)
+      if (aOptions.onError)
         aOptions.onError(e, aRequest.target.responseText, this);
     }
   };
 
-  if ("headers" in aOptions) {
+  if (aOptions.headers) {
     aOptions.headers.forEach(function(header) {
       xhr.setRequestHeader(header[0], header[1]);
     });
   }
 
   // Handle adding postData as defined above.
-  let POSTData = hasPostData ? aOptions.postData : "";
+  let POSTData = aOptions.postData || "";
   if (Array.isArray(POSTData)) {
     xhr.setRequestHeader("Content-Type",
                          "application/x-www-form-urlencoded; charset=utf-8");
     POSTData = POSTData.map(function(p) p[0] + "=" + percentEncode(p[1]))
                        .join("&");
   }
 
-  if ("logger" in aOptions) {
+  if (aOptions.logger) {
     aOptions.logger.log("sending request to " + aUrl + " (POSTData = " +
                         POSTData + ")");
   }
   xhr.send(POSTData);
   return xhr;
 }
--- a/toolkit/modules/tests/xpcshell/test_Http.js
+++ b/toolkit/modules/tests/xpcshell/test_Http.js
@@ -137,16 +137,28 @@ add_test(function test_GetData() {
       do_test_finished();
       run_next_test();
     },
     postData: null
   }
   httpRequest(kGetUrl, options);
 });
 
+add_test(function test_OptionalParameters() {
+  let options = {
+    onLoad: null,
+    onError: null,
+    logger: null
+  };
+  // Just make sure that nothing throws when doing this (i.e. httpRequest
+  // doesn't try to access null options).
+  httpRequest(kGetUrl, options);
+  run_next_test();
+});
+
 function run_test() {
   // Set up a mock HTTP server to serve a success page.
   server = new HttpServer();
   server.registerPathHandler(kSuccessPath, successResult);
   server.registerPathHandler(kPostPath,
                              getDataChecker("POST", kPostDataReceived));
   server.registerPathHandler(kPutPath,
                              getDataChecker("PUT", kPutDataReceived));