shindig-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ssiev...@apache.org
Subject svn commit: r1355476 - in /shindig/trunk/features/src: main/javascript/features/core.io/io.js test/javascript/features/core.io/iotest.js
Date Fri, 29 Jun 2012 17:15:18 GMT
Author: ssievers
Date: Fri Jun 29 17:15:16 2012
New Revision: 1355476

URL: http://svn.apache.org/viewvc?rev=1355476&view=rev
Log:
SHINDIG-1808 | makeRequest fails on IE when ActiveX is disabled | Patch from Matthew Reiter.
 Thanks!

Modified:
    shindig/trunk/features/src/main/javascript/features/core.io/io.js
    shindig/trunk/features/src/test/javascript/features/core.io/iotest.js

Modified: shindig/trunk/features/src/main/javascript/features/core.io/io.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/core.io/io.js?rev=1355476&r1=1355475&r2=1355476&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/core.io/io.js (original)
+++ shindig/trunk/features/src/main/javascript/features/core.io/io.js Fri Jun 29 17:15:16
2012
@@ -51,14 +51,17 @@ gadgets.io = function() {
         shindig.xhrwrapper.createXHR) {
       return shindig.xhrwrapper.createXHR();
     } else if (typeof ActiveXObject != 'undefined') {
-      x = new ActiveXObject('Msxml2.XMLHTTP');
-      if (!x) {
-        x = new ActiveXObject('Microsoft.XMLHTTP');
-      }
-      return x;
+      try {
+        x = new ActiveXObject('Msxml2.XMLHTTP');
+        if (!x) {
+          x = new ActiveXObject('Microsoft.XMLHTTP');
+        }
+        return x;
+      } catch (e) {} // An exception will be thrown if ActiveX is disabled
     }
+
     // The second construct is for the benefit of jsunit...
-    else if (typeof XMLHttpRequest != 'undefined' || window.XMLHttpRequest) {
+    if (typeof XMLHttpRequest != 'undefined' || window.XMLHttpRequest) {
       return new window.XMLHttpRequest();
     }
     else throw ('no xhr available');
@@ -199,7 +202,16 @@ gadgets.io = function() {
           break;
         case 'DOM':
           var dom;
-          if (typeof ActiveXObject != 'undefined') {
+          if (typeof DOMParser != 'undefined') {
+            var parser = new DOMParser();
+            dom = parser.parseFromString(resp['text'], 'text/xml');
+            if ('parsererror' === dom.documentElement.nodeName) {
+              resp['errors'].push('500 Failed to parse XML');
+              resp['rc'] = 500;
+            } else {
+              resp['data'] = dom;
+            }
+          } else if (typeof ActiveXObject != 'undefined') {
             dom = new ActiveXObject('Microsoft.XMLDOM');
             dom.async = false;
             dom.validateOnParse = false;
@@ -211,14 +223,8 @@ gadgets.io = function() {
               resp['data'] = dom;
             }
           } else {
-            var parser = new DOMParser();
-            dom = parser.parseFromString(resp['text'], 'text/xml');
-            if ('parsererror' === dom.documentElement.nodeName) {
-              resp['errors'].push('500 Failed to parse XML');
-              resp['rc'] = 500;
-            } else {
-              resp['data'] = dom;
-            }
+            resp['errors'].push('500 Failed to parse XML because no DOM parser was available');
+            resp['rc'] = 500;
           }
           break;
         default:

Modified: shindig/trunk/features/src/test/javascript/features/core.io/iotest.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/test/javascript/features/core.io/iotest.js?rev=1355476&r1=1355475&r2=1355476&view=diff
==============================================================================
--- shindig/trunk/features/src/test/javascript/features/core.io/iotest.js (original)
+++ shindig/trunk/features/src/test/javascript/features/core.io/iotest.js Fri Jun 29 17:15:16
2012
@@ -356,6 +356,59 @@ IoTest.prototype.testPut = function() {
   this.assertEquals('some data', resp.text);
 };
 
+/**
+ * Tests using makeRequest on IE with ActiveX disabled, which results in ActiveXObject throwing
an
+ * exception. We should then fall back to XMLHttpRequest.
+ *
+ * See https://issues.apache.org/jira/browse/SHINDIG-1808 for details.
+ */
+IoTest.prototype.testPut_IEActiveXDisabled = function() {
+  ActiveXObject = function() {
+    throw "error";
+  }
+
+  try {
+    this.testPut();
+  } finally {
+    ActiveXObject = undefined;
+  }
+};
+
+/**
+ * Tests using makeRequest on IE with ActiveX enabled, which results in an ActiveXObject
being
+ * created instead of XMLHttpRequest.
+ */
+IoTest.prototype.testPut_IEActiveXEnabled = function() {
+  ActiveXObject = function() {
+    var xhrConstructor = this.fakeXhrs.getXhrConstructor();
+    return new xhrConstructor();
+  }
+
+  try {
+    this.testPut();
+  } finally {
+    ActiveXObject = undefined;
+  }
+};
+
+/**
+ * Tests using makeRequest if there is no available mechanism for performing XML HTTP requests.
+ */
+IoTest.prototype.testPut_NoXhrAvailable = function() {
+  var error = null;
+
+  window.XMLHttpRequest = undefined;
+  try {
+    this.testPut();
+  } catch (e) {
+    error = e;
+  } finally {
+    window.XMLHttpRequest = this.fakeXhrs.getXhrConstructor();
+  }
+
+  this.assertEquals('no xhr available', error);
+};
+
 IoTest.prototype.testPut_noBody = function() {
   var req = new fakeXhr.Expectation("POST", "http://example.com/json");
   this.setStandardArgs(req, true);
@@ -876,6 +929,152 @@ IoTest.prototype.testJson_malformed = fu
   this.assertEquals("500 Failed to parse JSON", resp.errors[0]);
 };
 
+/**
+ * Tests parsing XML if the DOMParser class is defined.
+ */
+IoTest.prototype.testDom_DomParserDefined = function() {
+  var req = new fakeXhr.Expectation("GET", "http://example.com/json");
+  this.setStandardArgs(req, false);
+  req.setQueryArg("url", "http://target.example.com/somepage");
+  req.setQueryArg("contentType", "DOM");
+
+  var body = '<data>some text</data>';
+  var resp = this.makeFakeResponse(gadgets.json.stringify(
+      { 'http://target.example.com/somepage' : {
+          'body' : body,
+          'rc' : 200
+         }
+      }));
+
+  this.fakeXhrs.expect(req, resp);
+
+  var test = this;
+  DOMParser = function() {
+    this.parseFromString = function(content, contentType) {
+      test.assertEquals(body, content);
+      test.assertEquals("text/xml", contentType);
+
+      return {documentElement: {nodeName: "data", text: "some text"}};
+    }
+  }
+
+  try {
+    var resp = null;
+    gadgets.io.makeRequest("http://target.example.com/somepage",
+        function(data) {
+          resp = data;
+        },
+        {
+          "CONTENT_TYPE" : "DOM"
+        });
+  } finally {
+    DOMParser = undefined;
+  }
+
+  this.assertEquals(200, resp.rc);
+  this.assertEquals("some text", resp.data.documentElement.text);
+};
+
+/**
+ * Tests parsing XML if the DOMParser and ActiveXObject classes are both defined - DOMParser
+ * should be used.
+ */
+IoTest.prototype.testDom_DomParserAndActiveXObjectDefined = function() {
+  ActiveXObject = function() {
+    if (type === "Microsoft.XMLDOM") {
+      throw "should not be called";
+    } else {
+      return new window.XMLHttpRequest();
+    }
+  };
+
+  try {
+    this.testDom_DomParserDefined();
+  } finally {
+    ActiveXObject = undefined;
+  }
+};
+
+/**
+ * Tests parsing XML if the ActiveXObject class is defined.
+ */
+IoTest.prototype.testDom_ActiveXObjectDefined = function() {
+  var req = new fakeXhr.Expectation("GET", "http://example.com/json");
+  this.setStandardArgs(req, false);
+  req.setQueryArg("url", "http://target.example.com/somepage");
+  req.setQueryArg("contentType", "DOM");
+
+  var body = '<data>some text</data>';
+  var resp = this.makeFakeResponse(gadgets.json.stringify(
+      { 'http://target.example.com/somepage' : {
+          'body' : body,
+          'rc' : 200
+         }
+      }));
+
+  this.fakeXhrs.expect(req, resp);
+
+  var test = this;
+  ActiveXObject = function(type) {
+    if (type === "Microsoft.XMLDOM") {
+      this.loadXML = function(content) {
+        test.assertEquals(body, content);
+
+        this.documentElement = {nodeName: "data", text: "some text"};
+        return true;
+      }
+    } else {
+      return new window.XMLHttpRequest();
+    }
+  }
+
+  try {
+    var resp = null;
+    gadgets.io.makeRequest("http://target.example.com/somepage",
+        function(data) {
+          resp = data;
+        },
+        {
+          "CONTENT_TYPE" : "DOM"
+        });
+  } finally {
+    ActiveXObject = undefined;
+  }
+
+  this.assertEquals(200, resp.rc);
+  this.assertEquals("some text", resp.data.documentElement.text);
+};
+
+/**
+ * Tests parsing XML if there is no available mechanism for doing so.
+ */
+IoTest.prototype.testDom_NoParser = function() {
+  var req = new fakeXhr.Expectation("GET", "http://example.com/json");
+  this.setStandardArgs(req, false);
+  req.setQueryArg("url", "http://target.example.com/somepage");
+  req.setQueryArg("contentType", "DOM");
+
+  var resp = this.makeFakeResponse(gadgets.json.stringify(
+      { 'http://target.example.com/somepage' : {
+          'body' : '<wrapper><text>some text</text></wrapper>',
+          'rc' : 200
+         }
+      }));
+
+  this.fakeXhrs.expect(req, resp);
+
+  var resp = null;
+  gadgets.io.makeRequest("http://target.example.com/somepage",
+      function(data) {
+        resp = data;
+      },
+      {
+        "CONTENT_TYPE" : "DOM"
+      });
+  this.assertEquals(500, resp.rc);
+  this.assertEquals(["500 Failed to parse XML because no DOM parser was available"], resp.errors);
+};
+
 IoTest.prototype.testPreload = function() {
   gadgets.io.preloaded_ = [
     {



Mime
View raw message