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

fix($http): fix response headers with an empty value #10091

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function defaultHttpResponseTransform(data, headers) {
* @returns {Object} Parsed headers as key value object
*/
function parseHeaders(headers) {
var parsed = {}, key, val, i;
var parsed = createMap(), key, val, i;

if (!headers) return parsed;

Expand Down Expand Up @@ -63,7 +63,8 @@ function headersGetter(headers) {
if (!headersObj) headersObj = parseHeaders(headers);

if (name) {
return headersObj[lowercase(name)] || null;
name = lowercase(name);
return hasOwnProperty.call(headersObj, name) ? headersObj[name] : null;
}

return headersObj;
Expand Down
11 changes: 11 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,17 @@ describe('$http', function() {
});


it('should handle empty response header', function() {
$httpBackend.expect('GET', '/url', undefined)
.respond(200, '', { 'Custom-Empty-Response-Header': '', 'Constructor': '' });
$http.get('/url').success(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();
expect(callback.mostRecentCall.args[2]('custom-empty-response-Header')).toBe('');
expect(callback.mostRecentCall.args[2]('ToString')).toBe(null);
expect(callback.mostRecentCall.args[2]('Constructor')).toBe('');
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this change uses hasOwnProperty, add a test for header values in the object prototype, like ToString or Constructor, too.

it('should have delete()', function() {
$httpBackend.expect('DELETE', '/url').respond('');
$http['delete']('/url');
Expand Down