Browse files

Add http#req timeout handling, default timeout changed to 2000 millis…

…econds.
  • Loading branch information...
1 parent ee586c5 commit 54b6b9153f46011b019a04b5064e9bed18113063 @cliffano committed Apr 22, 2013
Showing with 20 additions and 4 deletions.
  1. +1 −1 CHANGELOG.md
  2. +6 −3 lib/http.js
  3. +13 −0 test/http.js
View
2 CHANGELOG.md
@@ -1,5 +1,5 @@
### 0.1.5-pre
-*
+* Add http#req timeout handling, default timeout changed to 2000 milliseconds
### 0.1.4
* Add http#req headers, payload (body, form, json, multipart), and requestOpts handling
View
9 lib/http.js
@@ -1,3 +1,4 @@
+/*jshint esnext: true */
var request = require('request');
/**
@@ -14,14 +15,16 @@ var request = require('request');
* - handlers: response handlers with format { statuscode: function(result, cb) }
* - headers: object containing http headers, exact map to mikeal/request headers opt
* - body, form, json, multipart: request payload, exact map to mikeal/request module body, form, json, multipart opts
+ * - timeout: request timeout in milliseconds, default to 2000 milliseconds
* - requestOpts: mikeal/request module opts, these opts will override any params set by http#req
* @param {Function} cb: standard cb(err, result) callback
*/
function req(method, url, opts, cb) {
opts.handlers = opts.handlers || {};
- // init default settings with follow redirections on all methods, and 1 minute timeout
- var params = { url: url, followAllRedirects: true, timeout: 60000 },
+ const TIMEOUT = 2000;
+ // init default settings with follow redirections on all methods, and 2 seconds timeout
+ var params = { url: url, followAllRedirects: true, timeout: opts.timeout || TIMEOUT },
envProxy = proxy(url);
// set proxy setting based on environment variable
@@ -109,4 +112,4 @@ function proxy(url) {
}
exports.request = req;
-exports.proxy = proxy;
+exports.proxy = proxy;
View
13 test/http.js
@@ -132,6 +132,19 @@ buster.testCase('http - request', {
done();
});
},
+ 'should set timeout': function (done) {
+ this.stub(request, 'post', function (params, cb) {
+ assert.equals(params.timeout, 10000);
+ cb(null, { statusCode: 200 });
+ });
+ function _success(result, cb) {
+ cb(null, result);
+ }
+ http.request('POST', 'http://someurl', { timeout: 10000, handlers: { 200: _success }}, function (err, result) {
+ assert.isNull(err);
+ done();
+ });
+ },
'should set headers': function (done) {
this.stub(request, 'post', function (params, cb) {
assert.equals(params.headers.foo, 'bar');

0 comments on commit 54b6b91

Please sign in to comment.