Skip to content
Browse files

properly setting the content-length header when multibyte characters …

…are involved
  • Loading branch information...
1 parent 7ce0757 commit 3bbac0cfd6f6d3b74b71df6707660f24cf6017af @derek-watson derek-watson committed
Showing with 23 additions and 6 deletions.
  1. +7 −6 lib/postageapp.js
  2. +16 −0 test/postageapp.js
View
13 lib/postageapp.js
@@ -59,9 +59,9 @@ module.exports = function(apiKey) {
'path': '/v.1.0/send_message.json',
'headers': {
'host': 'api.postageapp.com',
- 'content-type': 'application/json',
+ 'content-type': 'application/json; charset=utf8',
'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')',
- 'content-length': payload.length
+ 'content-length': Buffer.byteLength(payload, 'utf8')
}
});
@@ -70,7 +70,6 @@ module.exports = function(apiKey) {
var json;
response.on('data', function (data) {
-
if (response.headers['content-type'].indexOf("application/json") === 0) {
json = JSON.parse(data);
}
@@ -96,7 +95,7 @@ module.exports = function(apiKey) {
error(err, {});
});
- request.end(payload);
+ request.end(payload, 'utf8');
},
accountInfo: function () {
@@ -115,7 +114,7 @@ module.exports = function(apiKey) {
'host': 'api.postageapp.com',
'content-type': 'application/json',
'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')',
- 'content-length': payload.length
+ 'content-length': Buffer.byteLength(payload, 'utf8')
}
});
@@ -126,6 +125,7 @@ module.exports = function(apiKey) {
console.log('BODY: ' + chunk);
});
});
+
request.end(payload);
},
@@ -148,7 +148,7 @@ module.exports = function(apiKey) {
'host': 'api.postageapp.com',
'content-type': 'application/json',
'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')',
- 'content-length': payload.length
+ 'content-length': Buffer.byteLength(payload, 'utf8')
}
});
@@ -159,6 +159,7 @@ module.exports = function(apiKey) {
console.log('BODY: ' + chunk);
});
});
+
request.end(payload);
}
}
View
16 test/postageapp.js
@@ -3,6 +3,8 @@ var assert = require("assert"),
postageapp = require('../lib/postageapp')(settings.apikey);
describe('postageapp', function () {
+ this.timeout(5000);
+
describe('version', function() {
it('should return a version string', function() {
assert.equal('string', typeof(postageapp.version()));
@@ -26,5 +28,19 @@ describe('postageapp', function () {
done();
});
});
+
+ it('should work with utf8-encoded content', function(done) {
+ postageapp.sendMessage({
+ content: "hello snowman ☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃",
+ recipients: "test@null.postageapp.com"
+ }, function(throwaway, r) {
+ assert.equal('ok', r.response.status);
+ done();
+ }, function(err) {
+ console.log(err);
+ assert(false, 'should not have received an error')
+ done();
+ });
+ });
});
});

0 comments on commit 3bbac0c

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