]> source.dussan.org Git - jquery.git/commitdiff
Ajax: Fix getResponseHeader(key) for IE11
authorAndrei Fangli <andrei_fangli@outlook.com>
Mon, 26 Nov 2018 17:00:41 +0000 (19:00 +0200)
committerTimmy Willison <4timmywil@gmail.com>
Mon, 26 Nov 2018 17:00:41 +0000 (12:00 -0500)
- getResponseHeader(key) combines all header values for the provided key into a
single result where values are concatenated by ', '. This does not happen for
IE11 since multiple values for the same header are returned on separate lines.
This makes the function only return the last value of the header for IE11.
- Updated ajax headers test to better cover Object.prototype collisions

Close gh-4173
Fixes gh-3403

src/ajax.js
test/data/mock.php
test/middleware-mockserver.js
test/unit/ajax.js

index aec26830a2f3e24bf4546b9e5a52c7bed2b45acf..4cbefabaaf313df73f6314d2dfdbd3c4cf947ebf 100644 (file)
@@ -459,12 +459,14 @@ jQuery.extend( {
                                                if ( !responseHeaders ) {
                                                        responseHeaders = {};
                                                        while ( ( match = rheaders.exec( responseHeadersString ) ) ) {
-                                                               responseHeaders[ match[ 1 ].toLowerCase() ] = match[ 2 ];
+                                                               responseHeaders[ match[ 1 ].toLowerCase() + " " ] =
+                                                                       ( responseHeaders[ match[ 1 ].toLowerCase() + " " ] || [] )
+                                                                               .concat( match[ 2 ] );
                                                        }
                                                }
-                                               match = responseHeaders[ key.toLowerCase() ];
+                                               match = responseHeaders[ key.toLowerCase() + " " ];
                                        }
-                                       return match == null ? null : match;
+                                       return match == null ? null : match.join( ", " );
                                },
 
                                // Raw string
index 692c417e8c011dd79933f5b6ec2fcb36337e108a..937e331bff0e112b487d482980e390d324e16f2e 100644 (file)
@@ -114,6 +114,9 @@ ok( true, "mock executed");';
                header( 'Sample-Header: Hello World' );
                header( 'Empty-Header: ' );
                header( 'Sample-Header2: Hello World 2' );
+               header( 'List-Header: Item 1' );
+               header( 'list-header: Item 2' );
+               header( 'constructor: prototype collision (constructor)' );
 
                foreach ( explode( '|' , $req->query[ 'keys' ] ) as $key ) {
                        // Only echo if key exists in the header
index 35623761de0cb3a0f50dedcea191203e09a5a2d0..09371bbe90dd694db9231d96895f8bfd5ff23037 100644 (file)
@@ -132,7 +132,10 @@ var mocks = {
                resp.writeHead( 200, {
                        "Sample-Header": "Hello World",
                        "Empty-Header": "",
-                       "Sample-Header2": "Hello World 2"
+                       "Sample-Header2": "Hello World 2",
+                       "List-Header": "Item 1",
+                       "list-header": "Item 2",
+                       "constructor": "prototype collision (constructor)"
                } );
                req.query.keys.split( "|" ).forEach( function( key ) {
                        if ( req.headers[ key.toLowerCase() ] ) {
index b8b46e245c06a5aa5ee9d4d518295842dde7980e..f9cd3668d9ebfa1b2cace21ef85064fbbb429237 100644 (file)
@@ -252,7 +252,7 @@ QUnit.module( "ajax", {
                } );
        } );
 
-       ajaxTest( "jQuery.ajax() - headers", 5, function( assert ) {
+       ajaxTest( "jQuery.ajax() - headers", 8, function( assert ) {
                return {
                        setup: function() {
                                jQuery( document ).ajaxSend( function( evt, xhr ) {
@@ -293,6 +293,9 @@ QUnit.module( "ajax", {
                                        assert.strictEqual( emptyHeader, "", "Empty header received" );
                                }
                                assert.strictEqual( xhr.getResponseHeader( "Sample-Header2" ), "Hello World 2", "Second sample header received" );
+                               assert.strictEqual( xhr.getResponseHeader( "List-Header" ), "Item 1, Item 2", "List header received" );
+                               assert.strictEqual( xhr.getResponseHeader( "constructor" ), "prototype collision (constructor)", "constructor header received" );
+                               assert.strictEqual( xhr.getResponseHeader( "__proto__" ), null, "Undefined __proto__ header not received" );
                        }
                };
        } );