Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom headers #17

Merged
merged 4 commits into from
Jan 14, 2012
Merged
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
28 changes: 18 additions & 10 deletions lib/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ var _ = require('underscore');
var fs = require('fs');
var SmtpapiHeaders = require('./smtpapi_headers');

function EmailHeaders() {

}

function Email(params) {
/*
* Default parameters for sending mail
Expand All @@ -26,7 +22,7 @@ function Email(params) {
date: new Date(),
files: {},
file_data: {},
headers: new EmailHeaders()
headers: {}
};

_.extend(this, default_mail_params, params);
Expand All @@ -39,7 +35,19 @@ function Email(params) {
* @return {Boolean} The result of the validation
*/
Email.prototype.validate = function() {
return false;
//TODO: add validation
}

Email.prototype.setHeaders = function(val) {
if (_.isObject(val)) {
this.headers = val;
}
}

Email.prototype.addHeaders = function(val){
if (_.isObject(val)) {
_.extend(this.headers, val);
}
}

Email.prototype.addTo = function(to) {
Expand Down Expand Up @@ -114,7 +122,8 @@ Email.prototype.toWebFormat = function() {
text: this.text,
html: this.html,
bcc: this.bcc,
replyto: this.replyto
replyto: this.replyto,
headers: JSON.stringify(this.headers)
};

// there needs to be at least 1 to address.
Expand All @@ -134,10 +143,9 @@ Email.prototype.toSmtpFormat = function() {
body: this.text,
html: this.html,
reply_to: this.replyto,
headers: {
"x-smtpapi": this.smtpapi.toJson()
}
headers: this.headers
};
data.headers['x-smtpapi'] = this.smtpapi.toJson();

if (_.size(this.file_data) > 0) {
var attachments = [];
Expand Down
51 changes: 51 additions & 0 deletions test/integration/custom_headers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var SendGrid = require('../../lib/sendgrid');
var Email = require('../../lib/email');

var api_user = 'kylep';
var api_key = 'testing';

describe('custom headers', function() {
var sendgrid;
var custom_headers = {cow: 'moo', panda: 'brawr'};
beforeEach(function() {
sendgrid = new SendGrid(api_user, api_key);
});

describe('Smtp', function() {
it('should allow an email with custom headers to be sent', function(done) {
var mail = new Email({
to: 'kyle.partridge@sendgrid.com',
from: 'david.tomberlin@sendgrid.com',
subject: '[SMTP]Testing custom headers',
text: 'Custom headers in email'
});

mail.setHeaders(custom_headers);
mail.headers.should.eql(custom_headers);

sendgrid.send(mail, function(success, message) {
if (!success) assert.ok(false, message);
done();
});
});
});

describe('Web', function() {
it('should allow an email with custom headers to be sent', function(done) {
var mail = new Email({
to: 'kyle.partridge@sendgrid.com',
from: 'david.tomberlin@sendgrid.com',
subject: '[WEB]Testing custom headers',
text: 'Custom headers in email'
});

mail.setHeaders(custom_headers);
mail.headers.should.eql(custom_headers);

sendgrid.smtp(mail, function(success, message) {
if (!success) assert.ok(false, message);
done();
});
});
});
});
38 changes: 32 additions & 6 deletions test/lib/email.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,42 @@ describe('Email', function () {
});

describe('validation', function() {
it('should invalidate when there are no parameters', function() {
var mail = new Email();
it('should invalidate when there are no parameters');

mail.validate().should.be.false;
it('should return true when the mail is valid');
});

describe('custom headers', function() {
var mail;
var custom_headers = {cow: 'moo', panda: 'brawr'};
beforeEach(function() {
mail = new Email();
});

it('should allow setting custom headers via setHeaders', function() {
mail.setHeaders(custom_headers);
mail.headers.should.eql(custom_headers);
});

it('should return true when the mail is valid', function() {
var mail = new Email(text_params);
it('should allow setting custom headers one at a time with addHeaders', function() {
for(var key in custom_headers) {
var args = {};
args[key] = custom_headers[key];
mail.addHeaders(args);
}

mail.validate().should.be.true;
mail.headers.should.eql(custom_headers);
mail.addHeaders({fox: 'hound'});
mail.headers.fox.should.eql('hound');
});

it('should overwrite headers when calling addHeaders with the same value', function() {
mail.addHeaders(custom_headers);
mail.headers.should.eql(custom_headers);
mail.addHeaders({cow: 'in my mind'});
mail.headers.should.not.eql(custom_headers);
mail.headers.cow.should.eql('in my mind');
});

});
});