Skip to content

Commit

Permalink
Merge 2c15142 into 35b0352
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffynuts committed May 6, 2020
2 parents 35b0352 + 2c15142 commit be62a37
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion source/hosts-resolver.js
Expand Up @@ -55,7 +55,7 @@ class HostsResolver {
lines = lines.replace(whitespaceRegExp, ' ');
lines = lines.replace(tabRegExp, ' ');
lines = lines.replace(startsWithWhitespaceRegExp, '');
lines = lines.split('\n');
lines = lines.split('\n').map(line => line.trim());

this._hosts = {};

Expand Down
4 changes: 3 additions & 1 deletion tests/hosts.txt
Expand Up @@ -10,5 +10,7 @@ noiphere
127.0.0.1 foo4
127.0.0.2 foo4
127.0.0.1 manywhitespaces
127.0.0.1 startswithwhitespace
127.0.0.1 startswithwhitespace
127.0.0.1 tab
127.0.0.1 has-windows-newline

19 changes: 19 additions & 0 deletions tests/test.js
Expand Up @@ -6,6 +6,7 @@ const path = require('path');
const test = require('ava');
const Keyv = require('keyv');
const proxyquire = require('proxyquire');
const fs = require('fs').promises;

const makeRequest = options => new Promise((resolve, reject) => {
http.get(options, resolve).once('error', reject);
Expand Down Expand Up @@ -782,6 +783,23 @@ test.serial('double tick() has no effect', t => {
});

test('respects the `hosts` file', async t => {
const hostFile = path.join(__dirname, 'hosts.txt');
// Ensure that at least one line ends with a windows CRLF
const textContent = await fs.readFile(hostFile, {encoding: 'utf-8'});
const lines = textContent.split('\n').map(l => l.trim());
const altered = lines.map(line => {
if (line.includes('has-windows-newline')) {
return `${line}\r`;
}

return line;
});
if (!altered.find(line => line.includes('has-windows-newline'))) {
throw new Error('Can\'t find "has-windows-newline" hosts entry');
}

await fs.writeFile(hostFile, Buffer.from(altered.join('\n')));

const cacheable = new CacheableLookup({
customHostsPath: path.resolve(__dirname, 'hosts.txt')
});
Expand All @@ -805,6 +823,7 @@ test('respects the `hosts` file', async t => {
t.is(await getAddress('manywhitespaces'), '127.0.0.1');
t.is(await getAddress('startswithwhitespace'), '127.0.0.1');
t.is(await getAddress('tab'), '127.0.0.1');
t.is(await getAddress('has-windows-newline'), '127.0.0.1');

{
const entry = await cacheable.lookupAsync('foo3', {family: 4});
Expand Down

0 comments on commit be62a37

Please sign in to comment.