Skip to content

Commit

Permalink
Ajax: Fix getResponseHeader(key) for IE11
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Andrei15193 authored and timmywil committed Nov 26, 2018
1 parent 3ac9078 commit e0d9411
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/ajax.js
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/data/mock.php
Expand Up @@ -114,6 +114,9 @@ protected function headers( $req ) {
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
Expand Down
5 changes: 4 additions & 1 deletion test/middleware-mockserver.js
Expand Up @@ -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() ] ) {
Expand Down
5 changes: 4 additions & 1 deletion test/unit/ajax.js
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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" );
}
};
} );
Expand Down

0 comments on commit e0d9411

Please sign in to comment.