Skip to content

Commit

Permalink
Merge pull request #80 from groupon/jk-less-aggressive-search-domain
Browse files Browse the repository at this point in the history
Ignore searchDomain for localhost and IP
  • Loading branch information
jkrems authored Mar 29, 2018
2 parents 2f15715 + 8cfb230 commit ae53ec9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ function defaultStatusCode(value, defaultValue) {
return defaultValue;
}

var IPv4 = /^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$/;
function canApplySearchDomain(hostname) {
if (hostname === 'localhost') return false;
if (hostname[hostname.length - 1] === '.') return false;
// A hostname shouldn't contain ":" (IPv6 adddress) or be an IPv4 address
return hostname.indexOf(':') === -1 && !IPv4.test(hostname);
}

function buildHostname(hostname, searchDomain) {
if (!hostname || typeof hostname !== 'string') {
throw new Error('Invalid URI ' + JSON.stringify(hostname));
}
if (searchDomain && canApplySearchDomain(hostname)) {
return hostname + '.' + searchDomain + '.';
}
return hostname;
}

function fetchUrlObj(urlObj, options) {
if (options.baseUrl && typeof options.baseUrl === 'string') {
urlObj = applyBaseUrl(urlObj, options.baseUrl);
Expand Down Expand Up @@ -204,14 +222,7 @@ function fetchUrlObj(urlObj, options) {
body = qsParser.stringify(form);
}

var hostname = urlObj.hostname;
if (!hostname || typeof hostname !== 'string') {
throw new Error('Invalid URI ' + JSON.stringify(hostname));
}

if (options.searchDomain && hostname[hostname.length - 1] !== '.') {
hostname += '.' + options.searchDomain + '.';
}
var hostname = buildHostname(urlObj.hostname, options.searchDomain);

var agent = getAgent(options, urlObj);
assign(agent.options, {
Expand Down
36 changes: 36 additions & 0 deletions test/fetch-search-domain.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var http = require('http');

var assert = require('assertive');

var fetch = require('../').fetch;
Expand Down Expand Up @@ -64,4 +66,38 @@ describe('fetch: searchDomain', function() {
}
});
});

describe('localhost and IP', function() {
var server = http.createServer(function(req, res) {
res.end('{"ok":true}');
});

before(function(done) {
server.listen(done);
});

it('never appends the searchDomain to localhost', function() {
var options = {
baseUrl: 'http://localhost:' + server.address().port,
searchDomain: 'bar123',
};
return fetch('/path', options)
.json()
.then(function(result) {
assert.deepEqual({ ok: true }, result);
});
});

it('never appends the searchDomain to an IP address', function() {
var options = {
baseUrl: 'http://127.0.0.1:' + server.address().port,
searchDomain: 'bar123',
};
return fetch('/path', options)
.json()
.then(function(result) {
assert.deepEqual({ ok: true }, result);
});
});
});
});

0 comments on commit ae53ec9

Please sign in to comment.