diff --git a/src/ng/http.js b/src/ng/http.js index a3cb265e3d10..dc37c1625a09 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -37,19 +37,24 @@ function isJsonLike(str) { * @returns {Object} Parsed headers as key value object */ function parseHeaders(headers) { - var parsed = createMap(), key, val, i; - - if (!headers) return parsed; - - forEach(headers.split('\n'), function(line) { - i = line.indexOf(':'); - key = lowercase(trim(line.substr(0, i))); - val = trim(line.substr(i + 1)); + var parsed = createMap(), i; + function fillInParsed(key, val) { if (key) { parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val; } - }); + } + + if (isString(headers)) { + forEach(headers.split('\n'), function(line) { + i = line.indexOf(':'); + fillInParsed(lowercase(trim(line.substr(0, i))), trim(line.substr(i + 1))); + }); + } else if (isObject(headers)) { + forEach(headers, function(headerVal, headerKey) { + fillInParsed(lowercase(headerKey), trim(headerVal)); + }); + } return parsed; } @@ -68,7 +73,7 @@ function parseHeaders(headers) { * - if called with no arguments returns an object containing all headers. */ function headersGetter(headers) { - var headersObj = isObject(headers) ? headers : undefined; + var headersObj; return function(name) { if (!headersObj) headersObj = parseHeaders(headers); diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index e3096edbb1b4..1a394f3e3fbf 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -1038,6 +1038,18 @@ describe('$http', function() { expect(callback).toHaveBeenCalledOnce(); }); + it('should have access to request headers with mixed case', function() { + $httpBackend.expect('POST', '/url', 'header1').respond(200); + $http.post('/url', 'req', { + headers: {H1: 'header1'}, + transformRequest: function(data, headers) { + return headers('H1'); + } + }).success(callback); + $httpBackend.flush(); + + expect(callback).toHaveBeenCalledOnce(); + }); it('should pipeline more functions', function() { function first(d, h) {return d + '-first' + ':' + h('h1');}