Skip to content
Browse files

Added Content-Length header to all requests, tests

  • Loading branch information...
1 parent 0635e85 commit 54f7eff95143d6eaedeec36c7be244057c146a6d @derek-watson derek-watson committed Feb 6, 2013
Showing with 75 additions and 41 deletions.
  1. +1 −0 .gitignore
  2. +44 −38 lib/postageapp.js
  3. +3 −3 package.json
  4. +24 −0 test/postageapp.js
  5. +3 −0 test/settings.json.example
View
1 .gitignore
@@ -0,0 +1 @@
+test/settings.json
View
82 lib/postageapp.js
@@ -1,22 +1,12 @@
var http = require('http');
-var postageVersion = '1.1.0';
+var postageVersion = '1.1.1';
module.exports = function(apiKey) {
return {
- sendMessage: function (options, callback) {
- var request = http.request({
- 'port': 80,
- 'host': 'api.postageapp.com',
- 'method': 'POST',
- 'path': '/v.1.0/send_message.json',
- 'headers': {
- 'host': 'api.postageapp.com',
- 'content-type': 'application/json',
- 'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')'
- }
- });
+ version: function() { return postageVersion; },
+ sendMessage: function (options, callback) {
var recipients = options.recipients;
var subject = options.subject;
@@ -32,7 +22,7 @@ module.exports = function(apiKey) {
Creates a string of numbers to be used for the UID, which has to be a unique identifier in order
to prevent duplicate sending through PostageApp.
*/
- var date = new Date;
+ var date = new Date();
var epochDate = date.getTime();
/*
@@ -55,6 +45,21 @@ module.exports = function(apiKey) {
}
};
+ payload = JSON.stringify(payload);
+
+ var request = http.request({
+ 'port': 80,
+ 'host': 'api.postageapp.com',
+ 'method': 'POST',
+ 'path': '/v.1.0/send_message.json',
+ 'headers': {
+ 'host': 'api.postageapp.com',
+ 'content-type': 'application/json',
+ 'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')',
+ 'content-length': payload.length
+ }
+ });
+
request.on('response', function (response) {
response.setEncoding('utf8');
if (typeof callback !== 'undefined') {
@@ -63,10 +68,16 @@ module.exports = function(apiKey) {
});
}
});
- request.end(JSON.stringify(payload));
+ request.end(payload);
},
accountInfo: function () {
+ var payload = {
+ api_key: apiKey
+ };
+
+ payload = JSON.stringify(payload);
+
var request = http.request({
'port': 80,
'host': 'api.postageapp.com',
@@ -75,57 +86,52 @@ module.exports = function(apiKey) {
'headers': {
'host': 'api.postageapp.com',
'content-type': 'application/json',
- 'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')'
+ 'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')',
+ 'content-length': payload.length
}
});
- var payload = {
- api_key: apiKey
- };
-
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
- request.end(JSON.stringify(payload));
+ request.end(payload);
},
-
+
messageStatus: function (options) {
- var request = http.request({
+ var desiredUID = options.desiredUID;
+
+ var payload = {
+ api_key: apiKey,
+ uid: desiredUID,
+ };
+
+ payload = JSON.stringify(payload);
+
+ var request = http.request({
'port': 80,
'host': 'api.postageapp.com',
'method': 'POST',
'path': '/v.1.0/message_status.json',
'headers': {
'host': 'api.postageapp.com',
'content-type': 'application/json',
- 'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')'
+ 'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')',
+ 'content-length': payload.length
}
});
- var desiredUID = options.desiredUID;
-
- var date = new Date;
- var epochDate = date.getTime();
-
- var payload = {
- api_key: apiKey,
- uid: desiredUID,
- }
-
- console.log(JSON.stringify(payload));
-
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
- });
- request.end(JSON.stringify(payload));
+ });
+ request.end(payload);
}
}
};
View
6 package.json
@@ -1,15 +1,15 @@
{
"name": "postageapp",
- "version": "0.1.0",
+ "version": "1.1.1",
"node": ">= 0.4.0",
"homepage": "https://github.com/postageapp/postageapp-nodejs",
"repository": {
- "type":"git",
+ "type":"git",
"url":"https://github.com/postageapp/postageapp-nodejs"
},
"author": {"name":"PostageApp", "url":"http://postageapp.com"},
"contributors": [{"name":"Jon Lim", "email": "jon@postageapp.com", "url":"http://postageapp.com"}],
"description": "Node.JS package for sending emails through PostageApp",
"licenses": [{"type": "MIT", "url": "https://github.com/postageapp/postageapp-nodejs/blob/master/LICENSE"}],
"main": "index"
-}
+}
View
24 test/postageapp.js
@@ -0,0 +1,24 @@
+var assert = require("assert"),
+ settings = require('./settings.json'),
+ postageapp = require('../lib/postageapp')(settings.apikey);
+
+describe('postageapp', function () {
+ describe('version', function() {
+ it('should return a version string', function() {
+ assert.equal('string', typeof(postageapp.version()));
+ });
+ });
+
+ describe('sendMessage', function () {
+ it('should get a successful response back from the server', function (done) {
+ postageapp.sendMessage({
+ content: "hello world",
+ recipients: 'test@null.postageapp.com'
+ }, function(throwaway, r) {
+ var r = JSON.parse(r);
+ assert.equal('ok', r.response.status);
+ done();
+ });
+ });
+ });
+});
View
3 test/settings.json.example
@@ -0,0 +1,3 @@
+{
+ "apikey": "YOUR_API_KEY_HERE"
+}

0 comments on commit 54f7eff

Please sign in to comment.
Something went wrong with that request. Please try again.