Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit bb2e748

Browse files
committed
fix($httpBackend mock): getResponseHeader should be case insensitive
1 parent 44b2f44 commit bb2e748

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/angular-mocks.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,19 @@ function MockXhr() {
766766
};
767767

768768
this.getResponseHeader = function(name) {
769-
return this.$$headers[name];
769+
// the lookup must be case insensitive, that's why we try two quick lookups and full scan at last
770+
var header = this.$$headers[name];
771+
if (header) return header;
772+
773+
name = angular.lowercase(name);
774+
header = this.$$headers[name];
775+
if (header) return header;
776+
777+
header = undefined;
778+
angular.forEach(this.$$headers, function(headerVal, headerName) {
779+
if (!header && angular.lowercase(headerName) == name) header = headerVal;
780+
});
781+
return header;
770782
};
771783

772784
this.getAllResponseHeaders = function() {

test/angular-mocksSpec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,30 @@ describe('mocks', function() {
470470
});
471471

472472

473+
it('should normalize when header name case when accessed via getResponseHeader', function() {
474+
hb.when('GET', '/u1').respond(200, null, {'X-Fake': 'Header',
475+
'Content-Type': 'application/json',
476+
'Location': '/foo'});
477+
var xhr = hb('GET', '/u1', null, noop, {});
478+
hb.flush();
479+
expect(xhr.getResponseHeader('x-fAKE')).toBe('Header');
480+
expect(xhr.getResponseHeader('content-type')).toBe('application/json');
481+
expect(xhr.getResponseHeader('Location')).toBe('/foo');
482+
});
483+
484+
485+
it('should normalize expect header name case when accessed via getResponseHeader', function() {
486+
hb.expect('GET', '/u1').respond(200, null, {'X-Fake': 'Header',
487+
'Content-Type': 'application/json',
488+
'Location': '/foo'});
489+
var xhr = hb('GET', '/u1', null, noop, {});
490+
hb.flush();
491+
expect(xhr.getResponseHeader('x-fAKE')).toBe('Header');
492+
expect(xhr.getResponseHeader('content-type')).toBe('application/json');
493+
expect(xhr.getResponseHeader('Location')).toBe('/foo');
494+
});
495+
496+
473497
it('should preserve the order of requests', function() {
474498
hb.when('GET', '/url1').respond(200, 'first');
475499
hb.when('GET', '/url2').respond(201, 'second');

0 commit comments

Comments
 (0)