From 590b1acae54dc13253ae11825be3f2d5afaf17a5 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 20 Sep 2020 17:57:05 +0800 Subject: [PATCH 1/3] feat: add lookup method --- src/node/index.js | 16 ++++++++++++++++ test/node/lookup.js | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 test/node/lookup.js diff --git a/src/node/index.js b/src/node/index.js index 14f743aae..b3818591a 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -165,6 +165,7 @@ function Request(method, url) { this.qsRaw = this._query; // Unused, for backwards compatibility only this._redirectList = []; this._streamRequest = false; + this._lookup = null; this.once('end', this.clearTimeout.bind(this)); } @@ -300,6 +301,20 @@ Request.prototype.agent = function (agent) { return this; }; +/** + * Gets/sets the `lookup` function to use custom DNS resolver. + * + * @param {Function} lookup + * @return {Function} + * @api public + */ + +Request.prototype.lookup = function (lookup) { + if (arguments.length === 0) return this._lookup; + this._lookup = lookup; + return this; +}; + /** * Set _Content-Type_ response header passed through `mime.getType()`. * @@ -759,6 +774,7 @@ Request.prototype.request = function () { options.cert = this._cert; options.passphrase = this._passphrase; options.agent = this._agent; + options.lookup = this._lookup; options.rejectUnauthorized = typeof this._disableTLSCerts === 'boolean' ? !this._disableTLSCerts diff --git a/test/node/lookup.js b/test/node/lookup.js new file mode 100644 index 000000000..0958d5ba3 --- /dev/null +++ b/test/node/lookup.js @@ -0,0 +1,21 @@ +'use strict'; +const assert = require('assert'); +const dns = require('dns'); +const request = require('../support/client'); +const setup = require('../support/setup'); + +const base = setup.uri; + +function myLookup(hostname, options, callback) { + dns.lookup(hostname, options, callback); +} + +describe('req.lookup()', () => { + it('should set a custom lookup', () => { + const r = request.get(`${base}/ok`).lookup(myLookup); + assert(r.lookup() === myLookup); + r.then((res) => { + res.text.should.equal('ok'); + }); + }); +}); From 07e748f8d663de0d9a69f8f455c5ed3d3ab79020 Mon Sep 17 00:00:00 2001 From: yunnysunny Date: Thu, 20 Jan 2022 13:34:39 +0800 Subject: [PATCH 2/3] fix: fixed the error of null value of lookup --- src/node/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/index.js b/src/node/index.js index c478ac9f3..0787f156d 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -167,7 +167,7 @@ function Request(method, url) { this.qsRaw = this._query; // Unused, for backwards compatibility only this._redirectList = []; this._streamRequest = false; - this._lookup = null; + this._lookup = undefined; this.once('end', this.clearTimeout.bind(this)); } From 356e52561afa8ec7cf6e5d8a7b6007dba3b24aab Mon Sep 17 00:00:00 2001 From: yunnysunny Date: Fri, 21 Jan 2022 20:28:46 +0800 Subject: [PATCH 3/3] test: fixed not trigger req in lookup testcase --- test/node/lookup.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/node/lookup.js b/test/node/lookup.js index 0958d5ba3..0eafef210 100644 --- a/test/node/lookup.js +++ b/test/node/lookup.js @@ -2,20 +2,25 @@ const assert = require('assert'); const dns = require('dns'); const request = require('../support/client'); -const setup = require('../support/setup'); +const getSetup = require('../support/setup'); -const base = setup.uri; +let base = null; function myLookup(hostname, options, callback) { dns.lookup(hostname, options, callback); } describe('req.lookup()', () => { - it('should set a custom lookup', () => { + before(async () => { + const setup = await getSetup(); + base = setup.uri; + }); + it('should set a custom lookup', (done) => { const r = request.get(`${base}/ok`).lookup(myLookup); assert(r.lookup() === myLookup); r.then((res) => { res.text.should.equal('ok'); + done(); }); }); });