Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bagofholding

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bagofholding - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

5

CHANGELOG.md

@@ -1,2 +0,5 @@

### 0.1.0-pre
### 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

@@ -3,0 +6,0 @@ * Combine cli#readConfigFileSync and cli#readCustomConfigFileSync into cli#lookupFile

38

lib/http.js

@@ -16,6 +16,9 @@ var request = require('request');

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;
}

@@ -38,2 +41,33 @@

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;

@@ -5,3 +5,3 @@ {

"keywords": [],
"version": "0.1.0",
"version": "0.1.1",
"homepage": "http://github.com/cliffano/bagofholding",

@@ -30,3 +30,3 @@ "author": "Cliffano Subagio <blah@cliffano.com> (http://blog.cliffano.com)",

"request": "2.12.0",
"underscore": "1.4.3"
"underscore": "1.4.4"
},

@@ -33,0 +33,0 @@ "devDependencies": {

@@ -6,3 +6,3 @@ Bag Of Holding [![http://travis-ci.org/cliffano/bagofholding](https://secure.travis-ci.org/cliffano/bagofholding.png?branch=master)](http://travis-ci.org/cliffano/bagofholding)

This is an extract of the reusable parts from various Node.js modules I've written. It contains convenient utilities for command-line tools, unit test mock functions, and data manipulation.
This is an extract of the reusable parts of various Node.js modules I've written.

@@ -27,6 +27,6 @@ Mostly for internal use.

bag.cli.*;
bag.mock.*;
bag.http.*;
bag.obj.*;
bag.text.*;
Check out the source code for a list of functions available for use.
Check out [lib](https://github.com/cliffano/bagofholding/tree/master/lib) for the available methods of each component.

@@ -7,2 +7,3 @@ var buster = require('buster'),

'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) {

@@ -21,2 +22,3 @@ assert.equals(params.url, 'http://someurl');

'should handle result based on status code': function (done) {
this.stub(process, 'env', {});
this.stub(request, 'get', function (params, cb) {

@@ -41,2 +43,3 @@ assert.equals(params.url, 'http://someurl');

'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) {

@@ -53,3 +56,72 @@ assert.equals(params.url, 'http://someurl');

});
},
'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);
}
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc