Skip to content

Commit

Permalink
Merge pull request #75 from node-modules/support-alias-curl
Browse files Browse the repository at this point in the history
fix: add alias urllib.curl()
  • Loading branch information
fengmk2 committed Aug 12, 2015
2 parents 539acb5 + 699d750 commit 2a8b68f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
sudo: false
language: node_js
node_js:
- 'iojs-2'
- 'iojs-1'
- '3'
- '2'
- '1'
- '0.12'
- '0.11.12'
- '0.10'
Expand Down
3 changes: 3 additions & 0 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ exports.request = function (url, args, callback) {
});
};

// alias to curl
exports.curl = exports.request;

function makeCallback(resolve, reject) {
return function (err, data, res) {
if (err) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"url": "git://github.com/node-modules/urllib.git"
},
"scripts": {
"test": "node_modules/.bin/mocha -R spec -t 20000 -r should -r should-http test/*.test.js",
"test-cov": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -t 20000 -r should -r should-http test/*.test.js",
"test-travis": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- -t 20000 -r should -r should-http test/*.test.js",
"test": "node_modules/.bin/mocha -R spec -t 30000 -r should -r should-http test/*.test.js",
"test-cov": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -t 30000 -r should -r should-http test/*.test.js",
"test-travis": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- -t 30000 -r should -r should-http test/*.test.js",
"jshint": "jshint .",
"autod": "autod -w --prefix '~' -t test -e examples",
"cnpm": "npm install --registry=https://registry.npm.taobao.org"
Expand Down
45 changes: 27 additions & 18 deletions test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ describe('urllib.test.js', function () {
});
});

it('should alias curl() work', function (done) {
urllib.curl('http://cnpmjs.org/mirrors/iojs/v1.2.0/SHASUMS256.txt', {timeout: 10000},
function (err, data, res) {
should.not.exist(err);
should.ok(Buffer.isBuffer(data));
res.should.status(200);
done();
});
});

it('should 301', function (done) {
urllib.request(host + '/301', function (err, data, res) {
res.should.status(301);
Expand Down Expand Up @@ -530,14 +540,13 @@ describe('urllib.test.js', function () {
});

var urls = [
'http://r.cnpmjs.org/byte',
'https://cnpmjs.org/',
'http://r.cnpmjs.org/byte',
'https://cnpmjs.org/',
'https://cnpmjs.org/package/pedding',
'https://cnpmjs.org/package/byte',
'http://r.cnpmjs.org/pedding',
'https://cnpmjs.org/package/ms',
'http://registry.npm.taobao.org/byte',
'https://www.npmjs.com',
'http://npm.taobao.org/',
'http://npm.taobao.org/package/byte',
'https://www.npmjs.com/package/byte',
'http://registry.npm.taobao.org/pedding',
'http://npm.taobao.org/package/ms',
];

urls.forEach(function (url, index) {
Expand All @@ -547,15 +556,15 @@ describe('urllib.test.js', function () {
urllib.request(url, {
agent: agent,
httpsAgent: httpsAgent,
timeout: 15000,
timeout: 25000,
}, function (err, data, res) {
should.not.exist(err);
data.should.be.an.instanceof(Buffer);
if (res.statusCode !== 200) {
console.log(res.statusCode, res.headers);
}
res.should.have.header('connection', 'keep-alive');
if (index >= 2) {
if (index >= 3) {
res.keepAliveSocket.should.equal(true);
}
done();
Expand All @@ -566,20 +575,20 @@ describe('urllib.test.js', function () {
it('should request http timeout', function (done) {
var agent = this.agent;
var httpsAgent = this.httpsAgent;
urllib.request('http://r.cnpmjs.org/byte', {
urllib.request('http://registry.npm.taobao.org/byte', {
agent: agent,
httpsAgent: httpsAgent,
timeout: 15000,
timeout: 25000,
}, function (err, data, res) {
should.not.exist(err);
data.should.be.an.instanceof(Buffer);
res.statusCode.should.equal(200);
// make sure free socket release to free list
process.nextTick(function () {
urllib.request('http://r.cnpmjs.org/npm', {
urllib.request('http://registry.npm.taobao.org/npm', {
agent: agent,
httpsAgent: httpsAgent,
timeout: 100,
timeout: 10,
}, function (err) {
should.exist(err);
err.message.should.containEql('(connected: true, keepalive socket: true, agent status: {"createSocketCount":');
Expand All @@ -592,20 +601,20 @@ describe('urllib.test.js', function () {
it('should request https timeout', function (done) {
var agent = this.agent;
var httpsAgent = this.httpsAgent;
urllib.request('https://r.cnpmjs.org/koa', {
urllib.request('https://registry.npmjs.com/koa', {
agent: agent,
httpsAgent: httpsAgent,
timeout: 15000,
timeout: 25000,
}, function (err, data, res) {
should.not.exist(err);
data.should.be.an.instanceof(Buffer);
res.statusCode.should.equal(200);
// make sure free socket release to free list
process.nextTick(function () {
urllib.request('https://r.cnpmjs.org/npm', {
urllib.request('https://registry.npmjs.com/npm', {
agent: agent,
httpsAgent: httpsAgent,
timeout: 100,
timeout: 10,
}, function (err) {
should.exist(err);
err.message.should.containEql('(connected: true, keepalive socket: true, agent status: {"createSocketCount":');
Expand Down

0 comments on commit 2a8b68f

Please sign in to comment.