Skip to content

Commit

Permalink
chore: bump deps version and make test more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 31, 2014
1 parent 78fa88d commit 9df6048
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 60 deletions.
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
logo.png
.travis.yml
.drone.yml
examples/
test/
Makefile
Expand Down
6 changes: 5 additions & 1 deletion examples/co_urllib.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
'use strict';

var co = require('co');
var urllib = require('../');

co(function* () {
var result = yield urllib.requestThunk('http://nodejs.org');
var result = yield urllib.requestThunk('http://nodejs.org', {
gzip: true
});
console.log('status: %s, body size: %d, headers: %j',
result.res.statusCode, result.data.length, result.res.headers);

Expand Down
36 changes: 36 additions & 0 deletions examples/req_parse_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

var zlib = require('zlib');
var http = require('http');
var size = 0;

var options = {
method: 'GET',
hostname: 'www.tmall.com',
port: 80,
path: '/go/market/promotion-act/shengxian1111_mobile.php',
headers: {
// 'Accept-Encoding': 'gzip'
}
};

http.request(options, function (res) {
console.log(res.statusCode, res.headers);
var chunks = [];
res.on('data', function (data) {
console.log('data size: %d', data.length);
size += data.length;
chunks.push(data);
}).on('end', function () {
console.log('response end: gzip %d', size);
// console.log('response end: gzip %d, realsize: %d', size, zlib.gunzipSync(Buffer.concat(chunks)).length);
}).on('close', function () {
console.log('response close: %d', size);
}).on('error', function (err) {
console.log(err.stack);
console.log('response error: %d', size);
});
}).on('error', function (err) {
console.log(err.stack);
console.log('size: %s', size);
}).end();
21 changes: 10 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,24 @@
"url": "git://github.com/node-modules/urllib.git"
},
"scripts": {
"test": "node_modules/.bin/mocha -R spec -t 20000 -r should-http test/*.test.js",
"test-cov": "node --harmony node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -t 20000 -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-http test/*.test.js",
"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",
"jshint": "jshint .",
"cov": "npm run test-cov",
"autod": "autod -w --prefix '~' -t test -e examples && npm run cnpm",
"autod": "autod -w --prefix '~' -t test -e examples",
"cnpm": "npm install --registry=https://registry.npm.taobao.org",
"contributors": "contributors -f plain -o AUTHORS"
},
"dependencies": {
"debug": "~1.0.4",
"debug": "~2.1.0",
"default-user-agent": "~0.0.1",
"digest-header": "~0.0.1",
"native-or-bluebird": "~1.0.0",
"iconv-lite": "~0.4.4",
"media-typer": "~0.3.0",
"iconv-lite": "~0.4.4"
"native-or-bluebird": "~1.1.2"
},
"devDependencies": {
"agentkeepalive": "~0.2.0",
"agentkeepalive": "~1.2.0",
"autod": "*",
"bluebird": "*",
"co": "*",
Expand All @@ -46,9 +45,9 @@
"istanbul": "*",
"jshint": "*",
"mocha": "*",
"pedding": "~1.0.0",
"pedding": "*",
"should": "~4.0.4",
"should-http": "0.0.2"
"should-http": "*"
},
"engines": {
"node": ">= 0.10.0"
Expand Down
3 changes: 3 additions & 0 deletions test/digest_auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('digest_auth.test.js', function () {
var url = 'http://test.webdav.org/auth-digest/user3';
urllib.request(url, {
digestAuth: 'user3:user3',
timeout: 10000
}, function (err, data, res) {
should.not.exist(err);
res.should.status(404);
Expand All @@ -35,6 +36,7 @@ describe('digest_auth.test.js', function () {
var url = 'http://test.webdav.org/auth-digest/user3';
urllib.request(url, {
digestAuth: 'user3:fail',
timeout: 10000
}, function (err, data, res) {
should.not.exist(err);
res.should.status(401);
Expand All @@ -50,6 +52,7 @@ describe('digest_auth.test.js', function () {
urllib.request(url, {
digestAuth: 'user:passwd',
dataType: 'json',
timeout: 10000
}, function (err, data, res) {
should.not.exist(err);
res.should.status(200);
Expand Down
55 changes: 14 additions & 41 deletions test/urllib.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**!
* urllib - test/urllib.test.js
*
* Copyright(c) 2011 - 2014 fengmk2 and other contributors.
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('urllib.test.js', function () {
});

it('should FollowRedirectError', function (done) {
urllib.request(host + '/redirect_no_location', {followRedirect: true}, function (err, data, res) {
urllib.request(host + '/redirect_no_location', {followRedirect: true}, function (err, data) {
should.exist(err);
err.name.should.equal('FollowRedirectError');
err.message.should.containEql('Got statusCode 302 but cannot resolve next location from headers, GET http://127.0.0.1:');
Expand All @@ -100,7 +100,7 @@ describe('urllib.test.js', function () {
});

it('should MaxRedirectError', function (done) {
urllib.request(host + '/loop_redirect', {followRedirect: true}, function (err, data, res) {
urllib.request(host + '/loop_redirect', {followRedirect: true}, function (err, data) {
should.exist(err);
err.name.should.equal('MaxRedirectError');
err.message.should.containEql('Exceeded maxRedirects. Probably stuck in a redirect loop ');
Expand Down Expand Up @@ -162,36 +162,14 @@ describe('urllib.test.js', function () {
}, 1);
});

// it('should res.socket.destroy() after `response` event emit', function (done) {
// urllib.request(host + '/socket.destroy', function (err, data, res) {
// should.exist(err);
// err.name.should.equal('RemoteSocketClosedError');
// err.message.should.containEql('Remote socket was terminated before `response.end()` was called, GET http://127.0.0.1:');
// data.toString().should.equal('foo haha\nfoo haha 2');
// should.ok(res.aborted);
// should.ok(err.res.aborted);
// done();
// });
// });
//
// it('should handle server socket end() will normal after `response` event emit', function (done) {
// urllib.request(host + '/socket.end', function (err, data, res) {
// should.exist(err);
// err.name.should.equal('RemoteSocketClosedError');
// err.message.should.containEql('Remote socket was terminated before `response.end()` was called, GET http://127.0.0.1:');
// data.toString().should.equal('foo haha\nfoo haha 2');
// should.ok(res.aborted);
// done();
// });
// });

it('should handle server socket end("balabal") will error', function (done) {
urllib.request(host + '/socket.end.error', function (err, data, res) {
urllib.request(host + '/socket.end.error', function (err, data) {
should.exist(err);
err.name.should.equal('RequestError');
err.code && err.code.should.equal('HPE_INVALID_CHUNK_SIZE');
err.message.should.containEql('Parse Error (req "error"), GET http://127.0.0.1:');
err.bytesParsed.should.equal(2);
should.not.exist(data);
done();
});
});
Expand Down Expand Up @@ -579,12 +557,12 @@ describe('urllib.test.js', function () {
data = data.toString();
data.should.containEql('你好urllib\r\n----------------------------');
data.should.containEql('Content-Disposition: form-data; name="file"; filename="urllib.test.js"');
res.should.status(200);
done();
});
});

it('should post not exists file stream', function (done) {
var stat = fs.statSync(__filename);
var stream = fs.createReadStream(__filename + 'abc');
urllib.request(host + '/stream', {
type: 'POST',
Expand Down Expand Up @@ -653,7 +631,7 @@ describe('urllib.test.js', function () {
});
urllib.request(host + '/writestream', {
writeStream: writeStream
}, function (err, data, res) {
}, function (err) {
should.exist(err);
err.message.should.containEql('ENOENT, open');
done();
Expand Down Expand Up @@ -682,7 +660,7 @@ describe('urllib.test.js', function () {

describe('https request', function () {
it('GET github page', function (done) {
urllib.request('https://github.com/node-modules/urllib', { timeout: 10000 },
urllib.request('https://github.com/node-modules/urllib', { timeout: 15000 },
function (err, data, res) {
should.not.exist(err);
data.toString().should.containEql('node-modules/urllib');
Expand Down Expand Up @@ -792,8 +770,8 @@ describe('urllib.test.js', function () {
});
});

it.skip('should auto accept and custom decode gzip response content', function (done) {
urllib.request('http://r.cnpmjs.org/byte', {
it('should auto accept and custom decode gzip response content', function (done) {
urllib.request('https://www.nodejitsu.com/company/contact/', {
dataType: 'json', gzip: true, timeout: 10000,
headers: {
'accept-encoding': 'gzip'
Expand All @@ -803,11 +781,9 @@ describe('urllib.test.js', function () {
data.should.be.a.Buffer;
data.length.should.above(0);
res.should.have.header('content-encoding', 'gzip');
res.should.have.header('content-type', 'application/json; charset=utf-8');
zlib.gunzip(data, function (err, buf) {
should.not.exist(err);
buf.should.be.a.Buffer;
JSON.parse(buf).name.should.equal('byte');
done();
});
});
Expand All @@ -818,6 +794,7 @@ describe('urllib.test.js', function () {
{followRedirect: true, gzip: true, timeout: 10000}, function (err, data, res) {
should.not.exist(err);
data.toString().should.containEql('e213170fe5ec7721b31149fba1a7a691c50b5379');
res.should.status(200);
// res.should.have.header('content-encoding', 'gzip');
// res.should.have.header('content-type', 'text/plain');
done();
Expand All @@ -830,34 +807,30 @@ describe('urllib.test.js', function () {
should.not.exist(err);
should.not.exist(res.headers['content-encoding']);
res.should.have.header('content-type', 'application/octet-stream');
// console.log(res.headers);
done();
});
});

it('should not return gzip response content', function (done) {
done = pedding(3, done);
urllib.request('https://www.nodejitsu.com/company/contact/',
urllib.request('https://www.nodejitsu.com/company/contact/', {timeout: 10000},
function (err, data, res) {
should.not.exist(err);
should.not.exist(res.headers['content-encoding']);
// res.should.have.header('content-type', 'application/json');
done();
});

urllib.request('https://www.nodejitsu.com/company/contact/', {gzip: false},
urllib.request('https://www.nodejitsu.com/company/contact/', {gzip: false, timeout: 10000},
function (err, data, res) {
should.not.exist(err);
should.not.exist(res.headers['content-encoding']);
// res.should.have.header('content-type', 'application/json');
done();
});

urllib.request('https://www.nodejitsu.com/company/contact/', {gzip: true},
urllib.request('https://www.nodejitsu.com/company/contact/', {gzip: true, timeout: 10000},
function (err, data, res) {
should.not.exist(err);
res.should.have.header('content-encoding', 'gzip');
// res.should.have.header('content-type', 'application/json');
done();
});
});
Expand Down
12 changes: 6 additions & 6 deletions test/urllib_promise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('urllib_promise.test.js', function () {
result.res.rt.should.above(0);
result.res.size.should.above(0);
done();
}).error(done);
}).catch(done);
});

it('should work with args', function (done) {
Expand All @@ -51,20 +51,20 @@ describe('urllib_promise.test.js', function () {
result.res.should.status(200);
result.res.should.have.header('connection', 'keep-alive');
done();
}).error(done);
}).catch(done);
});

it('should throw error', function (done) {
urllib.request('http://nodejsnot-exists-ooll.abcsdf123.org', {
urllib.request('http://127.0.0.1:12312', {
data: {
q: 'foo'
}
})
.then(function (result) {
.then(function () {
throw new Error('should not run this');
}).error(function (err) {
}).catch(function (err) {
should.exist(err);
err.code.should.equal('ENOTFOUND');
err.code.should.equal('ECONNREFUSED');
err.status.should.equal(-1);
err.headers.should.eql({});
err.res.should.have.keys('status', 'statusCode', 'headers', 'rt', 'size', 'aborted');
Expand Down

0 comments on commit 9df6048

Please sign in to comment.