From 590b1acae54dc13253ae11825be3f2d5afaf17a5 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 20 Sep 2020 17:57:05 +0800 Subject: [PATCH] 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'); + }); + }); +});