Skip to content

Commit 064741b

Browse files
committed
#9 - Made it possible to pass config to underlaying request object
1 parent b732a50 commit 064741b

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

lib/connection.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ var pckage = require('../package.json'),
2222

2323

2424

25-
var Connection = module.exports = function (keys) {
25+
var Connection = module.exports = function (keys, options) {
2626
var self = this;
2727

2828
this.keys = keys;
29+
this.options = options;
2930
this.request = undefined;
3031

3132

@@ -53,7 +54,7 @@ Connection.prototype.connect = function (endpoint, params, callback) {
5354
self = this,
5455
conf = {
5556
headers: {
56-
'User-Agent': 'twitter-stream-api/' + pckage.version + ', node.js',
57+
'User-Agent': pckage.name + '/' + pckage.version + ', node.js',
5758
'content-length': '0'
5859
},
5960
url: uri,
@@ -62,6 +63,17 @@ Connection.prototype.connect = function (endpoint, params, callback) {
6263
};
6364

6465

66+
// Merge constructor options into request config Object
67+
68+
if (!util.isNullOrUndefined(self.options)) {
69+
Object.keys(self.options).forEach(function (key) {
70+
if (key === 'pool') {
71+
conf[key] = self.options[key];
72+
}
73+
});
74+
}
75+
76+
6577
// Input parameters is invalid
6678

6779
if (util.isNullOrUndefined(params)) {
@@ -79,12 +91,12 @@ Connection.prototype.connect = function (endpoint, params, callback) {
7991

8092

8193
// Connection has stalled if data has not received after 90 seconds
82-
94+
/*
8395
self.request.setTimeout(90000, function () {
8496
self.emit('connection error stall');
8597
self.destroy();
8698
});
87-
99+
*/
88100

89101
self.request.on('response', function (response) {
90102

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"devDependencies": {
3232
"request-debug": "0.2.0",
3333
"jshint": "2.8.0",
34+
"nock": "2.15.0",
3435
"tap": "2.2.0"
3536
}
3637
}

test/connection.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* jshint node: true, strict: true */
2+
3+
"use strict";
4+
5+
var tap = require('tap'),
6+
nock = require('nock'),
7+
Connection = require('../lib/connection.js');
8+
9+
10+
var keys = {
11+
consumer_key: "a",
12+
consumer_secret: "b",
13+
token: "c",
14+
token_secret: "d"
15+
};
16+
17+
18+
19+
tap.test('Connection() - constructor has "pool" attribute - "maxSockets" is set in internal request.js Object', function (t) {
20+
var connection = new Connection(keys, {pool: {maxSockets: 100}});
21+
var mockResponse = nock('https://sitestream.twitter.com')
22+
.get('/1.1/site.json?follow=1&follow=2')
23+
.reply(200, 'ok');
24+
25+
connection.connect('site', {
26+
follow: ['1', '2']
27+
}, function () {
28+
t.equal(connection.request.pool.maxSockets, 100);
29+
t.end();
30+
});
31+
});
32+
33+
34+
35+
tap.test('Connection() - constructor has no "pool" attribute - "maxSockets" is "undefined" ', function (t) {
36+
var connection = new Connection(keys);
37+
var mockResponse = nock('https://sitestream.twitter.com')
38+
.get('/1.1/site.json?follow=1&follow=2')
39+
.reply(200, 'ok');
40+
41+
connection.connect('site', {
42+
follow: ['1', '2']
43+
}, function () {
44+
t.equal(connection.request.pool.maxSockets, undefined);
45+
t.end();
46+
});
47+
});

0 commit comments

Comments
 (0)