From 699d750cdb4b987dc97db6311970238634e2ebdb Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 12 Aug 2015 19:56:46 +0800 Subject: [PATCH] fix: add alias urllib.curl() --- .travis.yml | 5 +++-- lib/urllib.js | 3 +++ package.json | 6 +++--- test/urllib.test.js | 45 +++++++++++++++++++++++++++------------------ 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index a7990451..af7e9ba1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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' diff --git a/lib/urllib.js b/lib/urllib.js index ff81dcd0..a6519d7a 100644 --- a/lib/urllib.js +++ b/lib/urllib.js @@ -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) { diff --git a/package.json b/package.json index 60e42d61..0f7fa550 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/urllib.test.js b/test/urllib.test.js index e736694c..f4a235f4 100644 --- a/test/urllib.test.js +++ b/test/urllib.test.js @@ -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); @@ -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) { @@ -547,7 +556,7 @@ 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); @@ -555,7 +564,7 @@ describe('urllib.test.js', function () { 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(); @@ -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":'); @@ -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":');