Skip to content

Commit

Permalink
fix: make sure request event url should be a string (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoner authored and fengmk2 committed Dec 28, 2017
1 parent 1e60328 commit 2df6906
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
22 changes: 11 additions & 11 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,6 @@ exports.requestWithCallback = function requestWithCallback(url, args, callback)

args.requestUrls = args.requestUrls || [];

var reqMeta = {
requestId: reqId,
url: url,
args: args,
ctx: args.ctx,
};
if (args.emitter) {
args.emitter.emit('request', reqMeta);
}

args.timeout = args.timeout || exports.TIMEOUTS;
args.maxRedirects = args.maxRedirects || 10;
args.streaming = args.streaming || args.customResponse;
Expand All @@ -219,6 +209,16 @@ exports.requestWithCallback = function requestWithCallback(url, args, callback)
parsedUrl = url;
}

var reqMeta = {
requestId: reqId,
url: parsedUrl.href,
args: args,
ctx: args.ctx,
};
if (args.emitter) {
args.emitter.emit('request', reqMeta);
}

var method = (args.type || args.method || parsedUrl.method || 'GET').toUpperCase();
var port = parsedUrl.port || 80;
var httplib = http;
Expand Down Expand Up @@ -459,7 +459,7 @@ exports.requestWithCallback = function requestWithCallback(url, args, callback)

if (args.emitter) {
// keep to use the same reqMeta object on request event before
reqMeta.url = url;
reqMeta.url = parsedUrl.href;
reqMeta.socket = req && req.connection;
reqMeta.options = options;
reqMeta.size = requestSize;
Expand Down
24 changes: 23 additions & 1 deletion test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ describe('test/urllib.test.js', function () {

describe('gzip content', function () {
it('should auto accept and decode gzip response content', function (done) {
urllib.request('https://www.baidu.com/',
urllib.request('https://www.google.com',
{
gzip: true,
timeout: 25000,
Expand Down Expand Up @@ -1447,6 +1447,28 @@ describe('test/urllib.test.js', function () {
done();
});
});

it('should listen request url, when request url is object', function (done) {
done = pedding(3, done);
var requestUrl = 'https://cn.bing.com/search?q=nodejs';

urllib.on('request', function (info) {
assert(info.url === requestUrl);
done();
});

urllib.on('response', function (info) {
assert(info.req.url === requestUrl);
done();
});

var urlObj = urlutil.parse(requestUrl);

urllib.request(urlObj, function (err) {
assert(!err);
done();
});
});
});

describe('charset support', function () {
Expand Down

0 comments on commit 2df6906

Please sign in to comment.