Skip to content

Commit

Permalink
Test CRLF style
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed May 6, 2020
1 parent 17d03fd commit 615412c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[crlfHosts.txt]
end_of_line = crlf

[*.yml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto eol=lf
crlfHosts.txt text=auto eol=crlf
17 changes: 17 additions & 0 deletions tests/crlfHosts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# this is a comment
127.0.0.1 helloworld
127.0.0.1 foobar # this is a comment
# this is yet another comment 127.0.0.1 woofwoof
noiphere
127.0.0.1 # no host here
127.0.0.1 foo1 foo2
127.0.0.1 foo3
::1 foo3
127.0.0.1 foo4
127.0.0.2 foo4
127.0.0.1 manywhitespaces
127.0.0.1 startswithwhitespace
127.0.0.1 tab
0.0.0.0 DoNotDeleteThisLineForResetPurposesOnly

127.0.0.1 doublenewline
156 changes: 80 additions & 76 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const test = require('ava');
const Keyv = require('keyv');
const proxyquire = require('proxyquire');

const hostsFiles = ['hosts.txt', 'crlfHosts.txt'];

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

test('respects the `hosts` file', async t => {
const cacheable = new CacheableLookup({
customHostsPath: path.resolve(__dirname, 'hosts.txt')
});

const getAddress = async hostname => {
const result = await cacheable.lookupAsync(hostname);
for (const file of hostsFiles) {
test(`respects the \`hosts\` file - ${file}`, async t => {
const cacheable = new CacheableLookup({
customHostsPath: path.resolve(__dirname, file)
});

t.is(result.family, 4);
t.is(result.ttl, Infinity);
t.is(result.expires, Infinity);
const getAddress = async hostname => {
const result = await cacheable.lookupAsync(hostname);

return result.address;
};
t.is(result.family, 4);
t.is(result.ttl, Infinity);
t.is(result.expires, Infinity);

t.is(await getAddress('helloworld'), '127.0.0.1');
t.is(await getAddress('foobar'), '127.0.0.1');
await t.throwsAsync(getAddress('woofwoof'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('noiphere'), {code: 'ENOTFOUND'});
t.is(await getAddress('foo1'), '127.0.0.1');
t.is(await getAddress('foo2'), '127.0.0.1');
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('doublenewline'), '127.0.0.1');
return result.address;
};

{
const entry = await cacheable.lookupAsync('foo3', {family: 4});
t.is(entry.address, '127.0.0.1');
t.is(entry.family, 4);
t.is(entry.expires, Infinity);
t.is(entry.ttl, Infinity);
}
t.is(await getAddress('helloworld'), '127.0.0.1');
t.is(await getAddress('foobar'), '127.0.0.1');
await t.throwsAsync(getAddress('woofwoof'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('noiphere'), {code: 'ENOTFOUND'});
t.is(await getAddress('foo1'), '127.0.0.1');
t.is(await getAddress('foo2'), '127.0.0.1');
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('doublenewline'), '127.0.0.1');

{
const entry = await cacheable.lookupAsync('foo3', {family: 6});
t.is(entry.address, '::1');
t.is(entry.family, 6);
t.is(entry.expires, Infinity);
t.is(entry.ttl, Infinity);
}
{
const entry = await cacheable.lookupAsync('foo3', {family: 4});
t.is(entry.address, '127.0.0.1');
t.is(entry.family, 4);
t.is(entry.expires, Infinity);
t.is(entry.ttl, Infinity);
}

{
const entries = await cacheable.lookupAsync('foo4', {all: true});
t.deepEqual(entries, [
{
address: '127.0.0.1',
family: 4,
expires: Infinity,
ttl: Infinity
}
]);
}
});
{
const entry = await cacheable.lookupAsync('foo3', {family: 6});
t.is(entry.address, '::1');
t.is(entry.family, 6);
t.is(entry.expires, Infinity);
t.is(entry.ttl, Infinity);
}

test('the `hosts` file support can be turned off', async t => {
const cacheable = new CacheableLookup({
customHostsPath: false,
resolver
{
const entries = await cacheable.lookupAsync('foo4', {all: true});
t.deepEqual(entries, [
{
address: '127.0.0.1',
family: 4,
expires: Infinity,
ttl: Infinity
}
]);
}
});

const getAddress = async hostname => {
const result = await cacheable.lookupAsync(hostname);
test(`the \`hosts\` file support can be turned off - ${file}`, async t => {
const cacheable = new CacheableLookup({
customHostsPath: false,
resolver
});

t.is(result.family, 4);
t.is(result.ttl, Infinity);
t.is(result.expires, Infinity);
const getAddress = async hostname => {
const result = await cacheable.lookupAsync(hostname);

return result.address;
};
t.is(result.family, 4);
t.is(result.ttl, Infinity);
t.is(result.expires, Infinity);

await t.throwsAsync(getAddress('helloworld'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foobar'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('woofwoof'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('noiphere'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foo1'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foo2'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('manywhitespaces'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('startswithwhitespace'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('tab'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('doublenewline'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foo3', {family: 4}), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foo3', {family: 6}), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foo4', {all: true}), {code: 'ENOTFOUND'});

const {address} = await cacheable.lookupAsync('localhost');
t.is(address, '127.0.0.1');
});
return result.address;
};

await t.throwsAsync(getAddress('helloworld'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foobar'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('woofwoof'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('noiphere'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foo1'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foo2'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('manywhitespaces'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('startswithwhitespace'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('tab'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('doublenewline'), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foo3', {family: 4}), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foo3', {family: 6}), {code: 'ENOTFOUND'});
await t.throwsAsync(getAddress('foo4', {all: true}), {code: 'ENOTFOUND'});

const {address} = await cacheable.lookupAsync('localhost');
t.is(address, '127.0.0.1');
});
}

test('custom cache support', async t => {
const cache = new Keyv();
Expand Down

0 comments on commit 615412c

Please sign in to comment.