Skip to content

Commit

Permalink
fix content-length header calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasassisrosa committed Oct 22, 2019
1 parent 1b8f3c0 commit e30fe62
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
11 changes: 6 additions & 5 deletions lib/TelnyxResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var path = require('path');

var utils = require('./utils');
var Error = require('./Error');
var Buffer = require('safe-buffer').Buffer;

var hasOwn = {}.hasOwnProperty;

Expand Down Expand Up @@ -296,7 +297,7 @@ TelnyxResource.prototype = {
return sleepSeconds * 1000;
},

_defaultHeaders: function(auth, contentLength) {
_defaultHeaders: function(auth, requestData) {
var userAgentString = 'Telnyx/v1 NodeBindings/' + this._telnyx.getConstant('PACKAGE_VERSION');

if (this._telnyx._appInfo) {
Expand All @@ -310,7 +311,7 @@ TelnyxResource.prototype = {
this._telnyx.getApiField('auth'),
'Accept': 'application/json',
'Content-Type': 'application/json',
'Content-Length': contentLength,
'Content-Length': Buffer.byteLength(requestData),
'User-Agent': userAgentString,
};

Expand Down Expand Up @@ -339,7 +340,7 @@ TelnyxResource.prototype = {
requestData = data;
}

headers = self._defaultHeaders(auth, requestData.length);
headers = self._defaultHeaders(auth, requestData);
self._telnyx.getClientUserAgent(function(cua) {
headers['X-Telnyx-Client-User-Agent'] = cua;

Expand All @@ -354,11 +355,11 @@ TelnyxResource.prototype = {
if (self.requestDataProcessor) {
self.requestDataProcessor(method, data, options.headers, makeRequestWithData);
} else if (method == 'GET') {
makeRequestWithData(null, utils.stringifyRequestData(data || {}));
makeRequestWithData(null, data ? utils.stringifyRequestData(data) : '');
} else if (method == 'DELETE') {
makeRequestWithData(null, '');
} else {
makeRequestWithData(null, utils.stringifyRequestBodyData(data || {}));
makeRequestWithData(null, data ? JSON.stringify(data) : '');
}

function retryRequest(requestFn, headers, requestRetries) {
Expand Down
10 changes: 0 additions & 10 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ var utils = module.exports = {
.replace(/%5B/g, '[').replace(/%5D/g, ']');
},

/**
* Stringifies an Object, accommodating nested objects
* (forming the conventional key 'parent[child]=value')
*/
stringifyRequestBodyData: function(data) {
return JSON.stringify(data)
// encode EM Dash (ENGDESK-2825)
.replace(//g, '-');
},

/**
* Outputs a new function with interpolated object property values.
* Use like so:
Expand Down
25 changes: 23 additions & 2 deletions test/TelnyxResource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,34 @@ describe('TelnyxResource', function() {

describe('_defaultHeaders', function() {
it('sets the Authorization header with Bearer auth using the global API key', function() {
var headers = telnyx.messagingProfiles._defaultHeaders(null, 0, null);
var headers = telnyx.messagingProfiles._defaultHeaders(null, '');
expect(headers.Authorization).to.equal('Bearer fakeAuthToken');
});
it('sets the Authorization header with Bearer auth using the specified API key', function() {
var headers = telnyx.messagingProfiles._defaultHeaders('anotherFakeAuthToken', 0, null);
var headers = telnyx.messagingProfiles._defaultHeaders('anotherFakeAuthToken', '');
expect(headers.Authorization).to.equal('Bearer anotherFakeAuthToken');
});

it('sets the content length for empty body', function() {
var headers = telnyx.messagingProfiles._defaultHeaders(null, '');
expect(headers['Content-Length']).to.equal(0);
});

it('sets the content length for UTF-8 body', function() {
var headers = telnyx.messagingProfiles._defaultHeaders(
null,
'"{\"messaging_profile_id\":\"uuid\",\"text\":\"Hi There! This is Collin with Polling \",\"to\":\"+18332784547\"}"'
);
expect(headers['Content-Length']).to.equal(101);
});

it('sets the content length for UTF-8 body with multi-bytes chars', function() {
var headers = telnyx.messagingProfiles._defaultHeaders(
null,
'"{\"messaging_profile_id\":\"uuid\",\"text\":\"Hi There! This is Collin with Polling – ‣\",\"to\":\"+18332784547\"}"'
);
expect(headers['Content-Length']).to.equal(108);
});
});

describe('Parameter encoding', function() {
Expand Down
11 changes: 0 additions & 11 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,6 @@ describe('utils', function() {
});
});

describe('stringifyRequestBodyData', function() {
it('Handles EM Dash', function() {
expect(utils.stringifyRequestBodyData({
from: 'NUMBER',
text: 'Hi Susan! This is Collin with Polling for Progress – do you ?',
to: 'NUMBER',
webhook_url: ''
})).to.equal('{"from":"NUMBER","text":"Hi Susan! This is Collin with Polling for Progress - do you ?","to":"NUMBER","webhook_url":""}');
});
});

describe('protoExtend', function() {
it('Provides an extension mechanism', function() {
function A() {}
Expand Down

0 comments on commit e30fe62

Please sign in to comment.