diff options
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | lib/email.js | 14 | ||||
-rw-r--r-- | test/lib/email.test.js | 23 |
3 files changed, 27 insertions, 14 deletions
@@ -208,8 +208,8 @@ You can add custom headers. This will ADD rather than SET headers. ```javascript var email = new sendgrid.Email(); email.setHeaders({full: 'hearts'}); // headers = {full: 'hearts'} -email.addHeader({spin: 'attack'}); // headers = {full: 'hearts', spin: 'attack'} -email.addHeader({mask: 'salesman'}); // headers = {full: 'hearts', spin: 'attack', mask: 'salesman'} +email.addHeader('spin', 'attack'); // headers = {full: 'hearts', spin: 'attack'} +email.addHeader('mask', 'salesman'); // headers = {full: 'hearts', spin: 'attack', mask: 'salesman'} sendgrid.send(email, function(err, json) { }); ``` diff --git a/lib/email.js b/lib/email.js index c369530..902dd46 100644 --- a/lib/email.js +++ b/lib/email.js @@ -52,9 +52,17 @@ function Email(params) { } } -Email.prototype.addHeader = function(object_literal) { - if (_.isObject(object_literal)) { - _.extend(this.headers, object_literal); +Email.prototype.addHeader = function(object_literal_or_key, value) { + if (typeof value === "undefined" || value === null) { + value = ""; + } + + if (_.isObject(object_literal_or_key)) { + _.extend(this.headers, object_literal_or_key); + } else { + var object_to_add = {} + object_to_add[object_literal_or_key] = value; + _.extend(this.headers, object_to_add); } }; diff --git a/test/lib/email.test.js b/test/lib/email.test.js index 8a659c4..9ecf3c1 100644 --- a/test/lib/email.test.js +++ b/test/lib/email.test.js @@ -221,7 +221,20 @@ describe('Email', function () { expect(mail.headers).to.eql(custom_headers); }); - it('should allow setting custom headers one at a time with addHeader', function() { + it('should allow setting custom headers with key, value approach', function() { + mail.addHeader('fox', 'hound'); + expect(mail.headers.fox).to.eql('hound'); + }); + + it('should overwrite headers when calling addHeader with the same value', function() { + mail.addHeader(custom_headers); + expect(mail.headers).to.eql(custom_headers); + mail.addHeader('cow', 'in my mind'); + expect(mail.headers).not.to.eql(custom_headers); + expect(mail.headers.cow).to.eql('in my mind'); + }); + + it('should allow setting custom headers one at a time with addHeader as a hash (temporary. deprecate this ability for key, value approach instead)', function() { for(var key in custom_headers) { var args = {}; args[key] = custom_headers[key]; @@ -233,14 +246,6 @@ describe('Email', function () { expect(mail.headers.fox).to.eql('hound'); }); - it('should overwrite headers when calling addHeader with the same value', function() { - mail.addHeader(custom_headers); - expect(mail.headers).to.eql(custom_headers); - mail.addHeader({cow: 'in my mind'}); - expect(mail.headers).not.to.eql(custom_headers); - expect(mail.headers.cow).to.eql('in my mind'); - }); - }); describe('file handling the constructor', function() { |