Skip to content

Commit

Permalink
feat: add lookup method
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Sep 20, 2020
1 parent 1277a88 commit 590b1ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/node/index.js
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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()`.
*
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions 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');
});
});
});

0 comments on commit 590b1ac

Please sign in to comment.