Removed non-standard (and not that useful) xhr emulation methods & reworked the heade...
authorjaubourg <aubourg.julian@gmail.com>
Fri, 10 Dec 2010 01:45:08 +0000 (02:45 +0100)
committerjaubourg <aubourg.julian@gmail.com>
Fri, 10 Dec 2010 01:45:08 +0000 (02:45 +0100)
src/xhr.js
test/data/headers.request.php [new file with mode: 0644]
test/unit/ajax.js

index fae1110..401619c 100644 (file)
@@ -147,8 +147,8 @@ jQuery.xhr = function( _native ) {
                                accepts[ "*" ];
                                
                        // Check for headers option
-                       if ( headers ) {
-                               xhr.setRequestHeaders( headers );
+                       for ( i in headers ) {
+                               requestHeaders[ i.toLowerCase() ] = headers[ i ];
                        }                       
                }
                        
@@ -554,21 +554,6 @@ jQuery.xhr = function( _native ) {
                                return xhr;
                        },
                        
-                       // Ditto with an s
-                       setRequestHeaders: function(map) {
-                               checkState(1, !sendFlag);
-                               for ( var name in map ) {
-                                       requestHeaders[ name.toLowerCase() ] = map[name];
-                               }
-                               return xhr;
-                       },
-                       
-                       // Utility method to get headers set
-                       getRequestHeader: function(name) {
-                               checkState(1, !sendFlag);
-                               return requestHeaders[ name.toLowerCase() ];
-                       },
-                       
                        // Raw string
                        getAllResponseHeaders: function() {
                                return xhr.readyState <= 1 ? "" : responseHeadersString;
diff --git a/test/data/headers.request.php b/test/data/headers.request.php
new file mode 100644 (file)
index 0000000..c511a51
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+if (!function_exists('apache_request_headers')) { 
+       function apache_request_headers() { 
+               foreach($_SERVER as $key=>$value) { 
+                       if (substr($key,0,5)=="HTTP_") { 
+                               $key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5))))); 
+                               $out[$key]=$value; 
+                       }else{ 
+                               $out[$key]=$value; 
+                       } 
+               } 
+               return $out; 
+       } 
+} 
+
+$headers = apache_request_headers();
+
+foreach( explode( "_" , $_GET[ "keys" ] ) as $key ) {
+       echo "$key: $headers[$key]\n";
+}
index 87430c9..c01edc0 100644 (file)
@@ -281,10 +281,39 @@ test("jQuery.ajax() - error callbacks", function() {
 test(".ajax() - headers" , function() {
 
        // No multiple line headers in IE
-       expect( jQuery.browser.msie ? 2 : 4 );
+       expect( jQuery.browser.msie ? 3 : 5 );
        
        stop();
        
+       var requestHeaders = {
+               Simple: "value",
+               "Something-Else": "other value",
+               Other: "something else"
+               },
+               list = [],
+               i,
+               sync = 2;
+               
+       for( i in requestHeaders ) {
+               list.push( i.toLowerCase() );
+       }
+       
+       list = list.join( "_" );
+       
+       jQuery.ajax(url("data/headers.request.php?keys="+list), {
+               headers: requestHeaders,
+               success: function( data ) {
+                       var tmp = [];
+                       for ( i in requestHeaders ) {
+                               tmp.push( i.toLowerCase() , ": " , requestHeaders[ i ] , "\n" );
+                       }
+                       tmp = tmp.join( "" );
+                       
+                       equals( data , tmp , "Headers were sent" );
+                       if ( ! --sync ) start();
+               }
+       });
+       
        jQuery.ajax({
                url: url("data/headers.php"),
                success: function( _1 , _2 , xhr ){
@@ -297,11 +326,11 @@ test(".ajax() - headers" , function() {
                                ok( /^Hello\s+World$/.test( xhr.getResponseHeader( "Multiple-Line" ) ) , "Multiple line" );
                                ok( /^Hello\s+Beautiful\s+World$/.test( xhr.getResponseHeader( "Multiple-Multiple-Line" ) ) , "Multiple multiple line" );
                        }
-                       start();
+                       if ( ! --sync ) start();
                },
                error: function(){ ok(false, "error"); }
        });
-       
+               
 });
 
 test(".ajax() - hash", function() {
@@ -1813,23 +1842,6 @@ test("jQuery.ajax - Etag support", function() {
        });
 });
 
-test("jQuery ajax - headers", function() {
-
-       stop();
-       
-       jQuery.ajax(url("data/css.php?wait=1&id=headers"), {
-               headers: {
-                       testKey: "testValue"
-               },
-               beforeSend: function( xhr ) {
-                       equals( xhr.getRequestHeader("testKey") , "testValue" , "Headers properly set" );
-                       setTimeout( start , 13 );
-                       return false;
-               }
-       });
-       
-});
-
 test("jQuery ajax - failing cross-domain", function() {
 
        expect( 2 );