fix($httpBackend mock): getResponseHeader should be case insensitive

This commit is contained in:
Igor Minar 2011-12-01 18:21:45 -05:00
parent 44b2f44f93
commit bb2e7488fa
2 changed files with 37 additions and 1 deletions

14
src/angular-mocks.js vendored
View file

@ -766,7 +766,19 @@ function MockXhr() {
};
this.getResponseHeader = function(name) {
return this.$$headers[name];
// the lookup must be case insensitive, that's why we try two quick lookups and full scan at last
var header = this.$$headers[name];
if (header) return header;
name = angular.lowercase(name);
header = this.$$headers[name];
if (header) return header;
header = undefined;
angular.forEach(this.$$headers, function(headerVal, headerName) {
if (!header && angular.lowercase(headerName) == name) header = headerVal;
});
return header;
};
this.getAllResponseHeaders = function() {

View file

@ -470,6 +470,30 @@ describe('mocks', function() {
});
it('should normalize when header name case when accessed via getResponseHeader', function() {
hb.when('GET', '/u1').respond(200, null, {'X-Fake': 'Header',
'Content-Type': 'application/json',
'Location': '/foo'});
var xhr = hb('GET', '/u1', null, noop, {});
hb.flush();
expect(xhr.getResponseHeader('x-fAKE')).toBe('Header');
expect(xhr.getResponseHeader('content-type')).toBe('application/json');
expect(xhr.getResponseHeader('Location')).toBe('/foo');
});
it('should normalize expect header name case when accessed via getResponseHeader', function() {
hb.expect('GET', '/u1').respond(200, null, {'X-Fake': 'Header',
'Content-Type': 'application/json',
'Location': '/foo'});
var xhr = hb('GET', '/u1', null, noop, {});
hb.flush();
expect(xhr.getResponseHeader('x-fAKE')).toBe('Header');
expect(xhr.getResponseHeader('content-type')).toBe('application/json');
expect(xhr.getResponseHeader('Location')).toBe('/foo');
});
it('should preserve the order of requests', function() {
hb.when('GET', '/url1').respond(200, 'first');
hb.when('GET', '/url2').respond(201, 'second');