Permalink
Browse files

Add http#proxy, modify http#request to use environment variable proxy…

… when not specified in opts.
  • Loading branch information...
1 parent 2c854c2 commit f89232261558c8a92a0739070ac35e982d9849e6 @cliffano committed Feb 12, 2013
Showing with 109 additions and 3 deletions.
  1. +1 −1 CHANGELOG.md
  2. +36 −2 lib/http.js
  3. +72 −0 test/http.js
View
@@ -1,5 +1,5 @@
### 0.1.1-pre
-*
+* Add http#proxy, modify http#request to use environment variable proxy when not specified in opts
### 0.1.0
* Remove mock since sandoxed-module is no longer actively maintained
View
@@ -13,10 +13,13 @@ var request = require('request');
* @param {Function} cb: standard cb(err, result) callback
*/
function req(method, url, opts, cb) {
- var params = { url: url };
+ var params = { url: url },
+ envProxy = proxy(url);
if (opts.proxy) {
params.proxy = opts.proxy;
+ } else if (envProxy) {
+ params.proxy = envProxy;
}
if (opts.queryStrings) {
@@ -34,4 +37,35 @@ function req(method, url, opts, cb) {
});
}
-exports.request = req;
+/**
+ * Determines proxy value based on URL and process environment variable (http_proxy, https_proxy).
+ * This allows library clients to control which proxy to use by setting environment variable.
+ * - if url starts with http, use http_proxy when available
+ * - if url starts with https, use https_proxy when available, otherwise fallback to http_proxy
+ * - if url does not have protocol, assume http protocol
+ * - if url is not specified, http_proxy takes precedence over https_proxy
+ *
+ * @param {String} url: URL used to determine which proxy environment variable to use
+ */
+function proxy(url) {
+ var _proxy;
+
+ if (!url) {
+ _proxy = process.env.http_proxy || process.env.https_proxy;
+ } else {
+ if (!url.match(/^https?:\/\//)) {
+ url += 'http://' + url;
+ }
+
+ if (url.match(/^https:\/\//)) {
+ _proxy = process.env.https_proxy || process.env.http_proxy;
+ } else {
+ _proxy = process.env.http_proxy;
+ }
+ }
+
+ return _proxy;
+}
+
+exports.request = req;
+exports.proxy = proxy;
View
@@ -4,6 +4,7 @@ var buster = require('buster'),
buster.testCase('http - request', {
'should pass error to callback when there is an error while sending request': function (done) {
+ this.stub(process, 'env', {});
this.stub(request, 'get', function (params, cb) {
assert.equals(params.url, 'http://someurl');
assert.equals(params.proxy, 'http://someproxy');
@@ -17,6 +18,7 @@ buster.testCase('http - request', {
});
},
'should handle result based on status code': function (done) {
+ this.stub(process, 'env', {});
this.stub(request, 'get', function (params, cb) {
assert.equals(params.url, 'http://someurl');
assert.equals(params.proxy, 'http://someproxy');
@@ -36,6 +38,7 @@ buster.testCase('http - request', {
});
},
'should pass error to callback when result status code is not expected': function (done) {
+ this.stub(process, 'env', {});
this.stub(request, 'get', function (params, cb) {
assert.equals(params.url, 'http://someurl');
assert.equals(params.proxy, undefined);
@@ -47,5 +50,74 @@ buster.testCase('http - request', {
assert.equals(result, undefined);
done();
});
+ },
+ 'should set proxy to environment variable when available': function (done) {
+ this.stub(process, 'env', { http_proxy: 'http://someproxy', https_proxy: 'https://someproxy' });
+ this.stub(request, 'get', function (params, cb) {
+ assert.equals(params.url, 'http://someurl');
+ assert.equals(params.proxy, 'http://someproxy');
+ assert.equals(params.qs, undefined);
+ cb(null, { statusCode: 888, body: 'somebody' });
+ });
+ http.request('GET', 'http://someurl', {}, function (err, result) {
+ assert.equals(err.message, 'Unexpected status code: 888\nResponse body:\nsomebody');
+ assert.equals(result, undefined);
+ done();
+ });
+ }
+});
+
+buster.testCase('http - proxy', {
+ 'should return http proxy when url uses http and both http and https proxy exist': function () {
+ this.stub(process, 'env', { http_proxy: 'http://someproxy', https_proxy: 'https://someproxy' });
+ assert.equals(http.proxy('http://someurl'), 'http://someproxy');
+ },
+ 'should return undefined when url uses http and https proxy exist but not http proxy': function () {
+ this.stub(process, 'env', { https_proxy: 'https://someproxy' });
+ assert.equals(http.proxy('http://someurl'), undefined);
+ },
+ 'should return undefined when url uses http and no proxy environment variable exists': function () {
+ this.stub(process, 'env', {});
+ assert.equals(http.proxy('http://someurl'), undefined);
+ },
+ 'should return https proxy when url uses https and both http and https proxy exist': function () {
+ this.stub(process, 'env', { http_proxy: 'http://someproxy', https_proxy: 'https://someproxy' });
+ assert.equals(http.proxy('https://someurl'), 'https://someproxy');
+ },
+ 'should return http proxy when url uses https and http proxy exists but not https proxy': function () {
+ this.stub(process, 'env', { http_proxy: 'http://someproxy' });
+ assert.equals(http.proxy('https://someurl'), 'http://someproxy');
+ },
+ 'should return undefined when url uses https and no proxy environment variable exist': function () {
+ this.stub(process, 'env', {});
+ assert.equals(http.proxy('http://someurl'), undefined);
+ },
+ 'should return http proxy when url does not specify protocol and both http and https proxy exist': function () {
+ this.stub(process, 'env', { http_proxy: 'http://someproxy', https_proxy: 'https://someproxy' });
+ assert.equals(http.proxy('someurl'), 'http://someproxy');
+ },
+ 'should return undefined when url does not specify protocol and https proxy exists but not http proxy': function () {
+ this.stub(process, 'env', { https_proxy: 'https://someproxy' });
+ assert.equals(http.proxy('someurl'), undefined);
+ },
+ 'should return undefined when url does not specify protocol and no proxy environment variable exists': function () {
+ this.stub(process, 'env', {});
+ assert.equals(http.proxy('someurl'), undefined);
+ },
+ 'should return http proxy when url is not specified and both http and https proxy exist': function () {
+ this.stub(process, 'env', { http_proxy: 'http://someproxy', https_proxy: 'https://someproxy' });
+ assert.equals(http.proxy(), 'http://someproxy');
+ },
+ 'should return http proxy when url is not specified and http proxy exists but not https proxy': function () {
+ this.stub(process, 'env', { http_proxy: 'http://someproxy' });
+ assert.equals(http.proxy(), 'http://someproxy');
+ },
+ 'should return https proxy when url is not specified and https proxy exists but not http proxy': function () {
+ this.stub(process, 'env', { https_proxy: 'https://someproxy' });
+ assert.equals(http.proxy(), 'https://someproxy');
+ },
+ 'should return undefined when url is not specified and no proxy environment variable exists': function () {
+ this.stub(process, 'env', {});
+ assert.equals(http.proxy(), undefined);
}
});

0 comments on commit f892322

Please sign in to comment.