Skip to content

Commit

Permalink
Merge pull request #425 from bertrandom/master
Browse files Browse the repository at this point in the history
Add a new transport that lets you define options on the request module
  • Loading branch information
aoberoi authored Nov 7, 2017
2 parents 47ce689 + c980a60 commit afe0fa0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
IncomingWebhook: require('./lib/clients/incoming-webhook/client'),
LegacyRtmClient: require('./lib/clients/default/legacy-rtm'),
MemoryDataStore: require('./lib/data-store/memory-data-store'),
requestOptionsTransport: require('./lib/clients/transports/request').requestOptionsTransport,
CLIENT_EVENTS: {
WEB: events.CLIENT_EVENTS.WEB,
RTM: events.CLIENT_EVENTS.RTM
Expand Down
10 changes: 8 additions & 2 deletions lib/clients/transports/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
var HttpsProxyAgent = require('https-proxy-agent');
var has = require('lodash').has;
var partial = require('lodash').partial;
var defaults = require('lodash').defaults;
var request = require('request');


var handleRequestTranportRes = function handleRequestTranportRes(cb, err, response, body) {
var headers;
var statusCode;
Expand Down Expand Up @@ -50,12 +50,18 @@ var proxiedRequestTransport = function proxiedRequestTransport(proxyURL) {
};
};

var requestOptionsTransport = function requestOptionsTransport(options) {
return function _requestOptionsTransport(args, cb) {
var requestArgs = defaults(options, getRequestTransportArgs(args));
request.post(requestArgs, partial(handleRequestTranportRes, cb));
};
};

var requestTransport = function requestTransport(args, cb) {
var requestArgs = getRequestTransportArgs(args);
request.post(requestArgs, partial(handleRequestTranportRes, cb));
};


module.exports.proxiedRequestTransport = proxiedRequestTransport;
module.exports.requestOptionsTransport = requestOptionsTransport;
module.exports.requestTransport = requestTransport;
32 changes: 32 additions & 0 deletions test/clients/web/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var retryPolicies = require('../../../lib/clients/retry-policies');
var defaultHTTPResponseHandler =
require('../../../lib/clients/transports/call-transport').handleHttpResponse;

var requestTransport = require('../../../lib/clients/transports/request');

var mockTransport = function (args, cb) {
cb(args.data.err, args.headers, args.data.statusCode, args.data.body);
Expand Down Expand Up @@ -109,6 +110,37 @@ describe('Web API Client', function () {
client._makeAPICall('test', { test: 'test' }, null, null);
});

it('should accept overriding of request options', function () {

// Add Basic Auth

var options = {
auth: {
user: 'slack',
pass: 'slack'
}
};

var requestOptionsTransport = requestTransport.requestOptionsTransport(options);

var client = new WebAPIClient('test-token', { transport: requestOptionsTransport });

// Mock a result where Basic Auth was passed

nock('https://slack.com/api', {
reqheaders: {
authorization: /Basic [0-9A-Za-z]+/i
}
})
.post('/test')
.reply(200, '{"test":"test"}');

client._makeAPICall('test', {}, null, function (e, results) {
expect(results.test).to.equal('test');
});

});

describe('it should retry failed or rate-limited requests', function () {

var attemptAPICall = function (done) {
Expand Down

0 comments on commit afe0fa0

Please sign in to comment.