Skip to content

Commit

Permalink
fix: normalize url with WHATWG URL to solve #319 (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake authored and fengmk2 committed May 6, 2019
1 parent 2dbcad0 commit 3384e53
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var dns = require('dns');
var http = require('http');
var https = require('https');
var urlutil = require('url');
var URL = require('whatwg-url').URL;
var util = require('util');
var qs = require('qs');
var ip = require('ip');
Expand Down Expand Up @@ -217,7 +218,7 @@ function requestWithCallback(url, args, callback) {
// Support `request('www.server.com')`
url = 'http://' + url;
}
parsedUrl = urlutil.parse(url);
parsedUrl = urlutil.parse(new URL(url).href);
} else {
parsedUrl = url;
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"pump": "^3.0.0",
"qs": "^6.4.0",
"statuses": "^1.3.1",
"utility": "^1.16.1"
"utility": "^1.16.1",
"whatwg-url": "^7.0.0"
},
"devDependencies": {
"@types/mocha": "^5.2.5",
Expand Down
11 changes: 10 additions & 1 deletion test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ describe('test/urllib.test.js', function () {
});
});

it('should normalize url', function (done) {
urllib.request(config.npmHttpRegistry + '/pedding/./1.0.0', function (err, data, res) {
assert.equal(err, null);
assert.equal(res.statusCode, 200);
done(err);
});
});

describe('ConnectionTimeoutError and ResponseTimeoutError', function () {
it('should connection timeout', function (done) {
urllib.request('http://npm.taobao.org', { timeout: 1 }, function (err, data, res) {
Expand Down Expand Up @@ -1354,6 +1362,7 @@ describe('test/urllib.test.js', function () {
{
gzip: true,
timeout: 25000,
followRedirect: true
}, function (err, data, res) {
assert(!err);
assert(res.headers['content-encoding'] === 'gzip');
Expand Down Expand Up @@ -1783,7 +1792,7 @@ describe('test/urllib.test.js', function () {
});

it('should throw error when request address is ip v6', function(done) {
urllib.request('http://2001:0DB8:02de:0000:0000:0000:0000:0e13/foo/bar', {
urllib.request('http://[2001:0DB8:02de:0000:0000:0000:0000:0e13]/foo/bar', {
checkAddress: function(address, family) {
return family !== 6;
},
Expand Down

0 comments on commit 3384e53

Please sign in to comment.