[OpenLayers-Commits] r12413 - in trunk/openlayers: lib/OpenLayers tests

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Thu Sep 22 23:42:47 EDT 2011


Author: openlayersgit
Date: 2011-09-22 20:42:46 -0700 (Thu, 22 Sep 2011)
New Revision: 12413

Modified:
   trunk/openlayers/lib/OpenLayers/Request.js
   trunk/openlayers/tests/Request.html
Log:
Give our requests the popular but non-standard X-Requested-With
header (r=crschmidt, closes #3491).

Modified: trunk/openlayers/lib/OpenLayers/Request.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Request.js	2011-09-23 03:42:34 UTC (rev 12412)
+++ trunk/openlayers/lib/OpenLayers/Request.js	2011-09-23 03:42:46 UTC (rev 12413)
@@ -122,6 +122,25 @@
             {proxy: OpenLayers.ProxyHost}
         );
         config = OpenLayers.Util.applyDefaults(config, defaultConfig);
+        
+        // Always set the "X-Requested-With" header to signal that this request
+        // was issued through the XHR-object. Since header keys are case 
+        // insensitive and we want to allow overriding of the "X-Requested-With"
+        // header through the user we cannot use applyDefaults, but have to 
+        // check manually whether we were called with a "X-Requested-With"
+        // header.
+        var customRequestedWithHeader = false;
+        for(headerKey in config.headers) {
+            if (config.headers.hasOwnProperty( headerKey )) {
+                if (headerKey.toLowerCase() === 'x-requested-with') {
+                    customRequestedWithHeader = true;
+                }
+            }
+        }
+        if (customRequestedWithHeader === false) {
+            // we did not have a custom "X-Requested-With" header
+            config.headers['X-Requested-With'] = 'XMLHttpRequest';
+        }
 
         // create request, open, and set headers
         var request = new OpenLayers.Request.XMLHttpRequest();

Modified: trunk/openlayers/tests/Request.html
===================================================================
--- trunk/openlayers/tests/Request.html	2011-09-23 03:42:34 UTC (rev 12412)
+++ trunk/openlayers/tests/Request.html	2011-09-23 03:42:46 UTC (rev 12413)
@@ -20,7 +20,7 @@
     function test_issue(t) {
         setup();
 
-        t.plan(22);
+        t.plan(25);
         var request, config;
         var proto = OpenLayers.Request.XMLHttpRequest.prototype;
         var issue = OpenLayers.Function.bind(OpenLayers.Request.issue,
@@ -96,19 +96,35 @@
         // reset open method
         proto.open = _open;
         
-        // test that headers are correctly set - 4 tests
+        // test that headers are correctly set - 6 tests
         var _setRequestHeader = proto.setRequestHeader;
         config = {
             headers: {
                 foo: "bar",
-                chicken: "soup"
+                chicken: "soup",
+                // This checks whether the autoadded 'X-Requested-With'-header
+                // can be overridden, even though the given key here is spelled
+                // in lowercase. 
+                'x-requested-with': 'humpty'
             }
         };
+        // we also track how often setRequestHeader is being called, it should
+        // be called once for every header, even with the above defined
+        // custom 'x-requested-with' header which we usually autoadd.
+        // If the numbers match, we make sure to not send duplicate headers like
+        //   x-requested-with: humpty         AND
+        //   X-Requested-With: XMLHttpRequest
+        var actualSetHeaderCnt = 0;
+        var expectedSetHeaderCnt = 3; // and not four!
         proto.setRequestHeader = function(key, value) {
+            actualSetHeaderCnt++;
             t.ok(key in config.headers, "setRequestHeader called with key: " + key);
             t.eq(value, config.headers[key], "setRequestHeader called with correct value: " + value);
-        }
+        };
         request = issue(config);
+        
+        t.eq(actualSetHeaderCnt, expectedSetHeaderCnt, 'A custom "x-requested-with" header overrides the default "X-Requested-With" header.');
+
         proto.setRequestHeader = _setRequestHeader;
         
         // test that callback is called (no scope) - 1 test
@@ -444,6 +460,35 @@
         var req = OpenLayers.Request.GET();
         req.abort();
     }
+    
+    function test_XRequestedWithHeaderAutoadded(t) {
+        t.plan( 2 );
+        
+        var headerSet = false;
+        var headerGot = '';
+        var headerExpected = 'XMLHttpRequest';
+        
+        // save to be able to restore later
+        var _setRequestHeader = OpenLayers.Request.XMLHttpRequest.prototype.setRequestHeader;
+        
+        OpenLayers.Request.XMLHttpRequest.prototype.setRequestHeader = function(field, value) {
+            if (field === 'X-Requested-With') {
+                headerSet = true;
+                headerGot = value;
+            }
+        };
+        
+        var req = OpenLayers.Request.issue({
+            url: location.href,
+            async: false
+        });
+                
+        t.ok( headerSet, 'We call the method "setRequestHeader" to set a "X-Requested-With"-header' );
+        t.eq( headerGot, headerExpected,  'The "X-Requested-With"-header is set to "' + headerExpected + '" as expected.' );
+        
+        // restore old setRequestHeader
+        OpenLayers.Request.XMLHttpRequest.prototype.setRequestHeader = _setRequestHeader;
+    }
     </script>
 </head>
 <body>



More information about the Commits mailing list