summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorscottmotte <scott@scottmotte.com>2013-07-18 22:53:15 +0000
committerscottmotte <scott@scottmotte.com>2013-07-19 23:52:27 +0000
commitb423fd9b313991cdd2fdcd24ec05f725ad76870a (patch)
tree016657730dafd83bf2c130f0ba433041d60e0688
parent07e93f39d4c82019cd270a9a9331748f5d40d1c4 (diff)
downloadsendgrid-nodejs-b423fd9b313991cdd2fdcd24ec05f725ad76870a.zip
sendgrid-nodejs-b423fd9b313991cdd2fdcd24ec05f725ad76870a.tar.gz
sendgrid-nodejs-b423fd9b313991cdd2fdcd24ec05f725ad76870a.tar.bz2
Switch to using request, additional specs, and a bit less code by switching and trusting request to the web api
-rw-r--r--lib/email.js65
-rw-r--r--lib/sendgrid.js205
-rw-r--r--package.json2
-rw-r--r--test/integration/sendgrid.test.js246
-rw-r--r--test/lib/email.test.js143
-rw-r--r--test/lib/sendgrid.test.js32
6 files changed, 423 insertions, 270 deletions
diff --git a/lib/email.js b/lib/email.js
index f4b7f76..dca2b11 100644
--- a/lib/email.js
+++ b/lib/email.js
@@ -1,7 +1,5 @@
"use strict";
-var https = require('https');
-var http = require('http');
var Step = require('step');
var SmtpapiHeaders = require('./smtpapi_headers');
var FileHandler = require('./file_handler');
@@ -213,33 +211,42 @@ Email.prototype.processFiles = function(callback) {
};
/**
- * This method returns the email object is a format to be consumed by the SendGrid.send
+ * This method returns the email object is a format to be consumed by the SendGrid.web
* using the web api
*
- * @see SendGrid.send
+ * @see SendGrid.web
*/
Email.prototype.toWebFormat = function() {
- var data = {
- to: this.to,
- from: this.from,
- 'x-smtpapi': this.smtpapi.toJson(),
- subject: this.subject,
- text: this.text,
- html: this.html,
- bcc: this.bcc,
- replyto: this.replyto,
- headers: JSON.stringify(this.headers)
- };
-
- if (this.fromname != null) {
- data.fromname = this.fromname;
+ var web = {
+ to : this.to,
+ from : this.from,
+ 'x-smtpapi' : this.smtpapi.toJson(),
+ subject : this.subject,
+ text : this.text,
+ html : this.html,
+ headers : JSON.stringify(this.headers)
}
- if (this.toname != null) {
- data.toname = this.toname;
+ if (this.bcc) { web.bcc = this.bcc; }
+ if (this.html) { web.html = this.html; }
+ if (this.toname) { web.toname = this.toname; }
+ if (this.fromname) { web.fromname = this.fromname; }
+ if (this.replyto) { web.replyto = this.replyto; }
+
+ this.updateMissingTo(web);
+
+ if (this.files) {
+ for (var index in this.files) {
+ var file = this.files[index];
+ web['files[' + file.filename + ']'] = {
+ filename : file.filename,
+ content : file.content || " ",
+ contentType : file.contentType,
+ cid : file.cid
+ };
+ }
}
- this.updateMissingTo(data);
- return data;
+ return web;
};
/**
@@ -249,7 +256,7 @@ Email.prototype.toWebFormat = function() {
* @see SendGrid.smtp
*/
Email.prototype.toSmtpFormat = function() {
- var data = {
+ var smtp = {
to : this.to,
sender : this.from,
subject : this.subject,
@@ -259,14 +266,14 @@ Email.prototype.toSmtpFormat = function() {
headers : this.headers
};
- data.headers['x-smtpapi'] = this.smtpapi.toJson();
+ smtp.headers['x-smtpapi'] = this.smtpapi.toJson();
this._formatFilesForNodeMailer(this.files);
- this.updateMissingTo(data);
- data.attachments = this.files;
+ this.updateMissingTo(smtp);
+ smtp.attachments = this.files;
- return data;
+ return smtp;
};
Email.prototype._formatFilesForNodeMailer = function(files) {
@@ -303,7 +310,7 @@ Email.prototype._formatFileForNodeMailer = function(file) {
* @param {object} data The data parameter to send via Rest or SMTP
*/
Email.prototype.updateMissingTo = function(data) {
- if (_.isEmpty(data.to) && this.smtpapi.to && !_.isEmpty(this.smtpapi.to)) {
+ if (data.to.length <= 0 && this.smtpapi.to && this.smtpapi.to.length > 0) {
data.to = this.from;
}
};
@@ -314,7 +321,7 @@ Email.prototype.updateMissingTo = function(data) {
* @return boolean
*/
Email.prototype.hasFiles = function() {
- return _(this.files).size() > 0;
+ return this.files.length > 0;
};
// export the object as the only object in this module
diff --git a/lib/sendgrid.js b/lib/sendgrid.js
index 84135b5..68752a9 100644
--- a/lib/sendgrid.js
+++ b/lib/sendgrid.js
@@ -1,19 +1,12 @@
"use strict";
var package_json = require('./../package.json');
-var querystring = require('querystring');
-var https = require('https');
-var nodemailer = require('nodemailer');
-var _ = require('underscore');
-var path = require('path');
-var mime = require('mime');
+var nodemailer = require('nodemailer');
+var request = require('request');
var Email = require('./email');
-var SMTP = "SMTP";
-if (process.env.NODE_ENV == "test") {
- SMTP = "STUB";
-}
+
/*
* Class for handling communications with SendGrid.
*
@@ -24,10 +17,22 @@ function SendGrid(api_user, api_key) {
this.api_user = api_user;
this.api_key = api_key;
this.version = package_json.version;
+ this.SMTP = "SMTP";
+ if (process.env.NODE_ENV == "test") {
+ this.SMTP = "STUB";
+ }
+}
+
+/*
+ * Sends an email via web. See .web method for more details.
+ *
+ */
+SendGrid.prototype.send = function(email, callback) {
+ this.web(email, callback);
}
/*
- * Sends an email via REST and returns true if the
+ * Sends an email via web and returns true if the
* message was sent successfully.
*
* @param {Email|Object} email An email object or a hash that has
@@ -35,83 +40,64 @@ function SendGrid(api_user, api_key) {
* @param {Function} callback A function to call when the processing is done.
* This parameter is optional.
*/
-SendGrid.prototype.send = function(email, callback) {
+SendGrid.prototype.web = function(email, callback) {
+ var api_user = this.api_user;
+ var api_key = this.api_key;
+
var self = this
, cb = callback || function() { };
- var boundary = Math.random();
-
if (email.constructor !== Email) {
email = new Email(email);
}
- function send_rest() {
- var post_data;
- var options = {
- host: 'sendgrid.com',
- path: '/api/mail.send.json',
- method: 'POST'
- };
-
- if (email.hasFiles()) {
- post_data = self.getMultipartData(email, boundary);
- var length = 0;
- for (var buf in post_data) {
- length += post_data[buf].length;
- }
- options.headers = {
- 'Content-Type': 'multipart/form-data; boundary=' + boundary,
- 'Content-Length': length
- };
- } else {
- post_data = self.getPostData(email);
- options.headers = {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Content-Length': post_data.length
- };
- }
-
- var request = https.request(options, function(res) {
- res.setEncoding('utf8');
- var content = '';
- res.on('data', function(chunk) {
- content += chunk;
- });
- res.on('end', function() {
+ function send_web() {
+ var req = request({
+ method : 'POST',
+ uri : "https://sendgrid.com/api/mail.send.json"
+ }, function(err, resp, body) {
+ if (err) {
+ return cb(false, err);
+ } else {
try {
- var json = JSON.parse(content);
- cb(json.message == 'success', json.errors);
- } catch (e) {
+ var json = JSON.parse(body);
+ return cb(json.message == 'success', json.errors);
+ } catch (err) {
cb(false, "Invalid JSON response from server");
}
- });
- }).on('error', function(e) {
- cb(false, e);
- });
-
- // If the email has files, it will be a multipart request.
- // TODO: make this feel less dirty.
- if (email.hasFiles()) {
- for (var key in post_data) {
- request.write(post_data[key]);
+ }
+ });
+
+ var form = email.toWebFormat();
+ form['api_user'] = api_user;
+ form['api_key'] = api_key;
+
+ var reqForm = req.form();
+ for (var field in form) {
+ var value = form[field];
+ if (value.filename) {
+ if (value.cid) {
+ reqForm.append("content["+value.filename+"]", value.cid);
+ }
+ reqForm.append("files["+value.filename+"]", value.content, {filename: value.filename, contentType: value.contentType});
+ } else {
+ try {
+ reqForm.append(field, value);
+ } catch(err) {}
}
- } else {
- request.write(post_data);
}
-
- request.end();
}
if (email.hasFiles()) {
email.processFiles(function(success, message) {
if (success) {
- send_rest();
+ send_web();
} else {
cb(false, message);
}
});
} else {
- send_rest();
+ send_web();
}
};
@@ -130,7 +116,7 @@ SendGrid.prototype.smtp = function(email, callback) {
, cb = callback || function() { };
// SMTP settings
- smtpTransport = nodemailer.createTransport(SMTP, {
+ smtpTransport = nodemailer.createTransport(this.SMTP, {
service: 'SendGrid',
auth: {
user: this.api_user,
@@ -155,89 +141,4 @@ SendGrid.prototype.smtp = function(email, callback) {
send_smtp();
};
-/*
- * Function for internal use.
- *
- * Used for returning the parameters for sending an email via REST.
- *
- * This method is only used when there are no attachments on the email object.
- *
- * @param {Email} email The email object to be sent via REST.
- * @return {String} Querystring format of the email to be sent.
- */
-SendGrid.prototype.getPostData = function(email) {
- var data = {
- api_user: this.api_user,
- api_key: this.api_key
- }
-
- _.extend(data, email.toWebFormat());
-
- return querystring.stringify(data);
-};
-
-/*
- * Function for internal use.
- *
- * Used for returning the parameters for sending an email via REST.
- *
- * This method is used when there are attachments on the email object.
- *
- * @param {Email} email The email object to be sent via REST.
- * @param {String} boundary The boundary to use between multipart sections.
- * @return {Array[Buffer]} An array of buffers for each section of
- * the multipart/form-data request.
- */
-SendGrid.prototype.getMultipartData = function(email, boundary) {
- var data = [];
- data.push(new Buffer(encodeField(boundary, 'api_user', this.api_user)));
- data.push(new Buffer(encodeField(boundary, 'api_key', this.api_key)));
-
- _(email.toWebFormat()).each(function(v, k) {
- data.push(new Buffer(encodeField(boundary, k, v)));
- });
-
- _(email.files).each(function(file) {
- if (file.cid) {
- data.push(new Buffer(encodeField(boundary, 'content[' + file.filename + ']', file.cid)));
- }
- data.push(encodeFile(boundary, file.contentType, 'files[' + file.filename + ']', file.filename));
- data.push(file.content);
- data.push(new Buffer('\r\n'));
- });
-
- return data;
-};
-
-/*
- * Function for encoding a field as a multipart/form-data request.
- *
- * @param {String} boundary The boundary to use between requests.
- * @param {String} name The name of the parameter.
- * @param {String} value The value of the parameter.
- * @return {String} The string representing the multipart/form-data section.
- */
-function encodeField(boundary, name, value) {
- var return_part = "--" + boundary + "\r\n";
- return_part += "Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n";
- return_part += value + "\r\n";
- return return_part;
-}
-
-/*
- * Function for encoding a file as a multipart/form-data request.
- *
- * @param {String} boundary The boundary to use between requests.
- * @param {String} type The Content-Type of the file
- * @param {String} name The name of the parameter.
- * @param {String} filename The name of the file.
- * @return {String} The string representing the multipart/form-data section.
- */
-function encodeFile(boundary, type, name, filename) {
- var return_part = "--" + boundary + "\r\n";
- return_part += "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\r\n";
- return_part += "Content-Type: " + type + "\r\n\r\n";
- return return_part;
-}
-
module.exports = SendGrid;
diff --git a/package.json b/package.json
index c904175..1a99f9b 100644
--- a/package.json
+++ b/package.json
@@ -22,12 +22,14 @@
"underscore": "1.4.4",
"nodemailer": "0.4.4",
"mime": "1.2.9",
+ "request": "2.22.0",
"step": "0.0.5"
},
"devDependencies": {
"dotenv": "0.0.2",
"mocha": ">= 0.9.0",
"chai": ">= 0.1.6",
+ "formidable": "1.0.14",
"nock": "~0.16.0",
"sinon": "~1.6.0"
},
diff --git a/test/integration/sendgrid.test.js b/test/integration/sendgrid.test.js
index 79faea6..7023e62 100644
--- a/test/integration/sendgrid.test.js
+++ b/test/integration/sendgrid.test.js
@@ -26,6 +26,8 @@ describe('SendGrid #skip', function () {
var payload;
beforeEach(function() {
+ sendgrid.SMTP = "SMTP"; // send it for real
+
payload = Object.create(default_payload);
payload.subject += "rest ";
});
@@ -40,6 +42,28 @@ describe('SendGrid #skip', function () {
done();
});
+ it('encodes unicode like ✔', function(done) {
+ payload.subject += "encodes unicode like ✔";
+
+ sendgrid.send(payload, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('with optional TO name and FROM name', function(done) {
+ payload.subject += "with optional TO name and FROM name";
+ payload.toname = "to name";
+ payload.fromname = "from name";
+
+ sendgrid.send(payload, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
it('handles content files', function(done) {
payload.subject += "handles content files";
payload.files = [
@@ -53,12 +77,232 @@ describe('SendGrid #skip', function () {
});
});
+ it('handles content files addFile approach', function(done) {
+ payload.subject += "handles content files addFile approach";
+ var email = new Email(payload);
+ email.addFile({filename: 'secret.txt', content: new Buffer("File Content")});
+
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles url files', function(done) {
+ payload.subject += "handles url files";
+ payload.files = [
+ {filename: 'icon.jpg', url: "http://i.imgur.com/2fDh8.jpg"}
+ ];
+
+ sendgrid.send(payload, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles url files addFile approach', function(done) {
+ payload.subject += "handles url files addFile approach";
+ var email = new Email(payload);
+ email.addFile({filename: 'icon.jpg', url: "http://i.imgur.com/2fDh8.jpg"});
+
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles path files', function(done) {
+ payload.subject += "handles path files";
+ payload.files = [
+ {path: __dirname + '/../assets/logo.png'}
+ ];
+
+ sendgrid.send(payload, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles path files addFile approach', function(done) {
+ payload.subject += "handles path files addFile approach";
+ var email = new Email(payload);
+ email.addFile({path: __dirname + '/../assets/logo.png'});
+
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles empty files', function(done) {
+ payload.subject += "handles empty files";
+ payload.files = [
+ {filename: 'empty-test'}
+ ]
+
+ sendgrid.send(payload, function(success, message) {
+ console.log(message);
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles empty files addFile approach', function(done) {
+ payload.subject += "handles empty files addFile approach";
+ var email = new Email(payload);
+ email.addFile({filename: 'empty-test'});
+
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles inline content', function(done) {
+ payload.subject += "handles inline content";
+ payload.files = [
+ {
+ filename: 'icon.jpg',
+ cid: 'photo1',
+ url: 'http://i.imgur.com/2fDh8.jpg'
+ }
+ ]
+ payload.html = "<img src='cid:photo1'/>";
+
+ sendgrid.send(payload, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles inline content addFile approach', function(done) {
+ payload.subject += "handles inline content addFile approach";
+ var email = new Email(payload);
+ email.addFile({
+ filename: 'icon.jpg',
+ cid: 'photo1',
+ url: 'http://i.imgur.com/2fDh8.jpg'
+ });
+ email.html = "<img src='cid:photo1'/>";
+
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles file even if contentType is empty', function(done) {
+ payload.subject += "handles file even if contentType is empty";
+ payload.files = [
+ {
+ filename: 'icon.jpg',
+ url: 'http://i.imgur.com/2fDh8.jpg',
+ contentType: ''
+ }
+ ]
+
+ sendgrid.send(payload, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles file even if contentType is empty addFile approach', function(done) {
+ payload.subject += "handles file even if contentType is empty addFile approach";
+ var email = new Email(payload);
+ email.addFile({
+ filename: 'icon.jpg',
+ url: 'http://i.imgur.com/2fDh8.jpg',
+ contentType: ''
+ });
+
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+
+ done();
+ });
+ });
+
+ it('handles the reply_to field', function(done) {
+ payload.subject += "handles the reply_to field";
+
+ var email = new Email(payload);
+ email.replyto = 'noreply@sendgrid.com';
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+ done();
+ });
+ });
+
+ it('handles filters', function(done) {
+ payload.subject += "handles filters";
+
+ var email = new Email(payload);
+ email.addFilterSetting('footer', 'enable', 1);
+ email.addFilterSetting('footer', 'text/plain', 'This is mah footer!');
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+ done();
+ });
+ });
+
+ it('handles filters with unicode parameters', function(done) {
+ payload.subject += "handles filters with unicode parameters";
+
+ var email = new Email(payload);
+ email.addFilterSetting('footer', 'enable', 1);
+ email.addFilterSetting('footer', 'text/plain', 'This is mah footer with a ✔ in it!');
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+ done();
+ });
+ });
+
+ it('handles substitution values', function(done) {
+ payload.subject += "handles substitution values";
+
+ var email = new Email(payload);
+ email.addSubVal('-name-',['Panda', 'Cow']);
+ email.html = 'You are a <strong>-name-</strong>';
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+ done();
+ });
+ });
+
+ it('handles sections being set in the email', function(done) {
+ payload.subject += "handles sections being set in the email";
+
+ var email = new Email(payload);
+ //mail.addTo(setup.multi_to);
+ email.addSubVal('-name-', ['Kyle', 'David']);
+ email.addSubVal('-meme-', ['-kyleSection-', '-davidSection-']);
+ email.addSection({'-kyleSection-': 'I heard you liked batman so I killed your parents'});
+ email.addSection({'-davidSection-': 'Metal gear?!!?!!!!eleven'});
+ email.html = "Yo -name-!<br /> Here's a meme for you:<br /> -meme-";
+ sendgrid.send(email, function(success, message) {
+ expect(success).to.be.true;
+ done();
+ });
+ });
});
describe('#smtp', function() {
var payload;
beforeEach(function() {
+ sendgrid.SMTP = "STUB";
+
payload = Object.create(default_payload);
payload.subject += "smtp ";
});
@@ -231,7 +475,7 @@ describe('SendGrid #skip', function () {
cid: 'photo1',
url: 'http://i.imgur.com/2fDh8.jpg'
});
- payload.html = "<img src='cid:photo1'/>";
+ email.html = "<img src='cid:photo1'/>";
sendgrid.smtp(email, function(success, message) {
expect(success).to.be.true;
diff --git a/test/lib/email.test.js b/test/lib/email.test.js
index cacb6a4..6d776fd 100644
--- a/test/lib/email.test.js
+++ b/test/lib/email.test.js
@@ -1,12 +1,11 @@
var Email = require('../../lib/email');
-var querystring = require('querystring');
var fs = require('fs');
-var text_params = {
- to: 'david.tomberlin@sendgrid.com',
- from: 'kyle.partridge@sendgrid.com',
- subject: 'Subject',
- text: 'This is an email.'
+var default_payload = {
+ to : 'david.tomberlin@sendgrid.com',
+ from : 'kyle.partridge@sendgrid.com',
+ subject : 'Subject',
+ text : 'This is an email.'
};
var files = [
@@ -15,73 +14,89 @@ var files = [
]
describe('Email', function () {
- it('should allow attributes to be set in the constuctor', function() {
- var mail = new Email(text_params);
-
- for (var key in text_params) {
- expect(text_params[key]).to.eql(mail[key]);
+ it('should allow attributes to be set in the constructor', function() {
+ var payload = Object.create(default_payload);
+ var email = new Email(payload);
+ for (var key in payload) {
+ expect(payload[key]).to.eql(email[key]);
}
});
- it('should return a Web Api format as expected', function() {
- var email = new Email(text_params);
- var webFormat = email.toWebFormat();
- expect(webFormat.to).to.equal(text_params.to);
- expect(webFormat.from).to.equal(text_params.from);
- expect(webFormat.subject).to.equal(text_params.subject);
- expect(webFormat.text).to.equal(text_params.text);
- });
+ describe("#toWebFormat", function() {
+ it('should return a Web Api format as expected', function() {
+ var payload = Object.create(default_payload);
+ var email = new Email(payload);
+ var format = email.toWebFormat();
+
+ expect(format.to).to.equal(payload.to);
+ expect(format.from).to.equal(payload.from);
+ expect(format.subject).to.equal(payload.subject);
+ expect(format.text).to.equal(payload.text);
+ expect(format.fromname).to.be.empty;
+ expect(format.toname).to.be.empty;
+ });
- it('should not have a to address if there is no to or no smtpapi.to set via Web Api', function() {
- var email = new Email({from: 'test@test.com', subject: 'testing', text: 'testing'});
- var webFormat = email.toWebFormat();
- expect(webFormat.to).to.be.empty;
- });
+ it('should not have a to address if there is no to or no smtpapi.', function() {
+ var payload = Object.create(default_payload);
+ var email = new Email({from: 'test@test.com', subject: 'testing', text: 'testing'});
+ var format = email.toWebFormat();
+ expect(format.to).to.be.empty;
+ });
- it('should return an Smtp Api format as expected', function() {
- var email = new Email(text_params);
- var smtpFormat = email.toSmtpFormat();
- expect(smtpFormat.to).to.equal(text_params.to);
- expect(smtpFormat.sender).to.equal(text_params.from);
- expect(smtpFormat.subject).to.equal(text_params.subject);
- expect(smtpFormat.body).to.equal(text_params.text);
- });
+ it('should have a to address if there is no to but there is an smtpapi to', function() {
+ var payload = Object.create(default_payload);
+ payload.to = "";
+ var email = new Email(payload);
+ email.addTo("test@test.com");
+ var format = email.toWebFormat();
- it('should not have a to address if there is no to or no smtpapi.to set via Smtp Api', function() {
- var email = new Email({from: 'test@test.com', subject: 'testing', text: 'testing'});
- var smtpFormat = email.toSmtpFormat();
- expect(smtpFormat.to).to.be.empty;
- });
+ expect(JSON.parse(format['x-smtpapi']).to).to.not.be.empty;
+ expect(format.to).to.not.be.empty;
+ });
- it("should not set a fromname if one isn't provided", function() {
- var email = new Email({from: 'test@test.com', subject: 'testing', text: 'testing'});
- var webFormat = email.toWebFormat();
- expect(webFormat.fromname).to.be.empty;
- });
+ it("should set a fromname if one is provided", function() {
+ var payload = Object.create(default_payload);
+ var email = new Email({from: 'test@test.com', fromname:'Tester T. Testerson', subject: 'testing', text: 'testing'});
+ var format = email.toWebFormat();
- it("should set a fromname if one is provided", function() {
- var email = new Email({from: 'test@test.com', fromname:'Tester T. Testerson', subject: 'testing', text: 'testing'});
- var webFormat = email.toWebFormat();
- expect(webFormat.fromname).to.equal('Tester T. Testerson');
- });
+ expect(format.fromname).to.equal('Tester T. Testerson');
+ });
- it("should not set a toname if one isn't provided", function() {
- var email = new Email({from: 'test@test.com', subject: 'testing', text: 'testing'});
- var webFormat = email.toWebFormat();
- expect(webFormat.toname).to.be.empty;
- });
+ it("should set a toname if one is provided", function() {
+ var payload = Object.create(default_payload);
+ var email = new Email({from: 'test@test.com', to:'test@test.com', toname:'Tester T. Testerson', subject: 'testing', text: 'testing'});
+ var format = email.toWebFormat();
- it("should set a toname if one is provided", function() {
- var email = new Email({from: 'test@test.com', to:'test@test.com', toname:'Tester T. Testerson', subject: 'testing', text: 'testing'});
- var webFormat = email.toWebFormat();
- expect(webFormat.toname).to.equal('Tester T. Testerson');
+ expect(format.toname).to.equal('Tester T. Testerson');
+ });
+
+ it("should set multiple tonames if several are provided", function() {
+ var payload = Object.create(default_payload);
+ var email = new Email({from: 'test@test.com', to: ['test@test.com', 'test2@test.com'], toname:['Tester T. Testerson', 'Test2 M. Testerson'], subject: 'testing', text: 'testing'});
+ var format = email.toWebFormat();
+
+ expect(format.toname[0]).to.equal('Tester T. Testerson');
+ expect(format.toname[1]).to.equal('Test2 M. Testerson');
+ });
});
- it("should set multiple tonames if several are provided", function() {
- var email = new Email({from: 'test@test.com', to: ['test@test.com', 'test2@test.com'], toname:['Tester T. Testerson', 'Test2 M. Testerson'], subject: 'testing', text: 'testing'});
- var webFormat = email.toWebFormat();
- expect(webFormat.toname[0]).to.equal('Tester T. Testerson');
- expect(webFormat.toname[1]).to.equal('Test2 M. Testerson');
+ describe("#toSmtpFormat", function() {
+ it('should return an Smtp Api format as expected', function() {
+ var payload = Object.create(default_payload);
+ var email = new Email(payload);
+ var format = email.toSmtpFormat();
+
+ expect(format.to).to.equal(payload.to);
+ expect(format.sender).to.equal(payload.from);
+ expect(format.subject).to.equal(payload.subject);
+ expect(format.body).to.equal(payload.text);
+ });
+
+ it('should not have a to address if there is no to or no smtpapi.to set via Smtp Api', function() {
+ var email = new Email({from: 'test@test.com', subject: 'testing', text: 'testing'});
+ var format = email.toSmtpFormat();
+ expect(format.to).to.be.empty;
+ });
});
describe('files', function() {
@@ -121,12 +136,6 @@ describe('Email', function () {
});
});
- describe('validation', function() {
- it('should invalidate when there are no parameters');
-
- it('should return true when the mail is valid');
- });
-
describe('custom headers', function() {
var mail;
var custom_headers = {cow: 'moo', panda: 'brawr'};
diff --git a/test/lib/sendgrid.test.js b/test/lib/sendgrid.test.js
index d1e01c0..d6281ee 100644
--- a/test/lib/sendgrid.test.js
+++ b/test/lib/sendgrid.test.js
@@ -14,7 +14,6 @@ var default_payload = {
var SendGrid = require('../../lib/sendgrid')
, Email = require('../../lib/email')
- , querystring = require('querystring')
, sinon = require('sinon')
, nock = require('nock');
@@ -35,17 +34,14 @@ describe('SendGrid', function () {
beforeEach(function() {
payload = Object.create(default_payload);
- webApi = nock('https://sendgrid.com:443')
- .matchHeader('Content-Type', 'application/x-www-form-urlencoded')
+ webApi = nock('https://sendgrid.com')
.filteringRequestBody(function(path) {
postParamsString = path;
- postParams = querystring.parse(path);
return '*';
})
.post('/api/mail.send.json', '*');
});
-
it('has an optional callback', function(done) {
expect(function() {
sendgrid.send(payload);
@@ -101,17 +97,13 @@ describe('SendGrid', function () {
mock = webApi.reply(200, { message: "success" });
sendgrid.send(payload, function(success, message) {
- expect(postParams).to.include.keys(['api_user', 'api_key', 'to', 'from', 'subject', 'text', 'html', 'x-smtpapi']);
- expect(postParams).not.to.include.keys(['toname', 'fromname']);
-
- expect(postParams.api_user).to.equal(API_USER);
- expect(postParams.api_key).to.equal(API_KEY);
- expect(postParams.to).to.equal(default_payload.to);
- expect(postParams.from).to.equal(default_payload.from);
- expect(postParams.subject).to.equal(default_payload.subject);
- expect(postParams.text).to.equal(default_payload.text);
- expect(postParams.html).to.equal(default_payload.html);
- expect(postParams['x-smtpapi']).to.equal('{}');
+ expect(postParamsString).to.include(API_USER);
+ expect(postParamsString).to.include(API_KEY);
+ expect(postParamsString).to.include(default_payload.to);
+ expect(postParamsString).to.include(default_payload.from);
+ expect(postParamsString).to.include(default_payload.subject);
+ expect(postParamsString).to.include(default_payload.text);
+ expect(postParamsString).to.include(default_payload.html);
done();
});
@@ -124,10 +116,8 @@ describe('SendGrid', function () {
payload.fromname= "from name";
sendgrid.send(payload, function(success, message) {
- expect(postParams).to.include.keys(['toname', 'fromname']);
-
- expect(postParams.toname).to.equal('to name');
- expect(postParams.fromname).to.equal('from name');
+ expect(postParamsString).to.include('to name');
+ expect(postParamsString).to.include('from name');
done();
});
@@ -139,7 +129,7 @@ describe('SendGrid', function () {
payload.subject = "A unicode ✔ subject";
sendgrid.send(payload, function(success, message) {
- var encodedCheckmark = '%E2%9C%94';
+ var encodedCheckmark = '✔';
expect(postParamsString).to.include(encodedCheckmark);
done();