Skip to content

Commit

Permalink
Merge pull request #1674 from yunnysunny/feature/fix-lookup
Browse files Browse the repository at this point in the history
Feature/fix lookup
  • Loading branch information
titanism authored Mar 29, 2022
2 parents 451cdcd + 356e525 commit 31e0160
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function Request(method, url) {
this.qsRaw = this._query; // Unused, for backwards compatibility only
this._redirectList = [];
this._streamRequest = false;
this._lookup = undefined;
this.once('end', this.clearTimeout.bind(this));
}

Expand Down Expand Up @@ -306,6 +307,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()`.
*
Expand Down Expand Up @@ -765,6 +780,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
Expand Down
26 changes: 26 additions & 0 deletions test/node/lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const assert = require('assert');
const dns = require('dns');
const request = require('../support/client');
const getSetup = require('../support/setup');

let base = null;

function myLookup(hostname, options, callback) {
dns.lookup(hostname, options, callback);
}

describe('req.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();
});
});
});

0 comments on commit 31e0160

Please sign in to comment.