diff --git a/package.json b/package.json index 033a779..c95a56d 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,6 @@ "c8": "^10.1.2", "eslint": "^9.17.0", "neostandard": "^0.12.0", - "tape": "^5.7.5", "tsd": "^0.32.0" }, "scripts": { @@ -70,6 +69,6 @@ "lint:fix": "eslint --fix", "test": "npm run test:unit && npm run test:typescript", "test:typescript": "tsd", - "test:unit": "c8 tape test/**/*.js" + "test:unit": "c8 --100 node --test" } } diff --git a/test/.eslintrc.yml b/test/.eslintrc.yml deleted file mode 100644 index 9808c3b..0000000 --- a/test/.eslintrc.yml +++ /dev/null @@ -1,2 +0,0 @@ -env: - mocha: true diff --git a/test/all.js b/test/all.test.js similarity index 68% rename from test/all.js rename to test/all.test.js index 44da283..775f5b3 100644 --- a/test/all.js +++ b/test/all.test.js @@ -1,55 +1,48 @@ 'use strict' -const test = require('tape') +const { test } = require('node:test') const proxyaddr = require('..') test('argument req should be required', function (t) { - t.throws(proxyaddr.all, /req.*required/u) - t.end() + t.assert.throws(proxyaddr.all, /req.*required/u) }) test('argument trustshould be optional', function (t) { const req = createReq('127.0.0.1') - t.doesNotThrow(proxyaddr.all.bind(null, req)) - t.end() + t.assert.doesNotThrow(proxyaddr.all.bind(null, req)) }) test('with no headers should return socket address', function (t) { const req = createReq('127.0.0.1') - t.same(proxyaddr.all(req), ['127.0.0.1']) - t.end() + t.assert.deepStrictEqual(proxyaddr.all(req), ['127.0.0.1']) }) test('with x-forwarded-for header should include x-forwarded-for', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.1' }) - t.same(proxyaddr.all(req), ['127.0.0.1', '10.0.0.1']) - t.end() + t.assert.deepStrictEqual(proxyaddr.all(req), ['127.0.0.1', '10.0.0.1']) }) test('with x-forwarded-for header should include x-forwarded-for in correct order', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.1, 10.0.0.2' }) - t.same(proxyaddr.all(req), ['127.0.0.1', '10.0.0.2', '10.0.0.1']) - t.end() + t.assert.deepStrictEqual(proxyaddr.all(req), ['127.0.0.1', '10.0.0.2', '10.0.0.1']) }) test('with trust argument should stop at first untrusted', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.1, 10.0.0.2' }) - t.same(proxyaddr.all(req, '127.0.0.1'), ['127.0.0.1', '10.0.0.2']) - t.end() + t.assert.deepStrictEqual(proxyaddr.all(req, '127.0.0.1'), ['127.0.0.1', '10.0.0.2']) }) test('with trust argument should be only socket address for no trust', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.1, 10.0.0.2' }) - t.same(proxyaddr.all(req, []), ['127.0.0.1']) - t.end() + t.assert.deepStrictEqual(proxyaddr.all(req, []), ['127.0.0.1']) }) function createReq (socketAddr, headers) { diff --git a/test/base.js b/test/base.test.js similarity index 65% rename from test/base.js rename to test/base.test.js index 1c04586..e928b62 100644 --- a/test/base.js +++ b/test/base.test.js @@ -1,108 +1,93 @@ 'use strict' -const test = require('tape') +const { test } = require('node:test') const proxyaddr = require('..') test('req should be required', function (t) { - t.throws(proxyaddr, /req.*required/u) - t.end() + t.assert.throws(proxyaddr, /req.*required/u) }) test('trust should be required', function (t) { const req = createReq('127.0.0.1') - t.throws(proxyaddr.bind(null, req), /trust.*required/u) - t.end() + t.assert.throws(proxyaddr.bind(null, req), /trust.*required/u) }) test('trust should accept a function', function (t) { const req = createReq('127.0.0.1') - t.doesNotThrow(proxyaddr.bind(null, req, all)) - t.end() + t.assert.doesNotThrow(proxyaddr.bind(null, req, all)) }) test('trust should accept an array', function (t) { const req = createReq('127.0.0.1') - t.doesNotThrow(proxyaddr.bind(null, req, [])) - t.end() + t.assert.doesNotThrow(proxyaddr.bind(null, req, [])) }) test('trust should accept a string', function (t) { const req = createReq('127.0.0.1') - t.doesNotThrow(proxyaddr.bind(null, req, '127.0.0.1')) - t.end() + t.assert.doesNotThrow(proxyaddr.bind(null, req, '127.0.0.1')) }) test('trust should reject a number', function (t) { const req = createReq('127.0.0.1') - t.throws(proxyaddr.bind(null, req, 42), /unsupported trust argument/u) - t.end() + t.assert.throws(proxyaddr.bind(null, req, 42), /unsupported trust argument/u) }) test('trust should accept IPv4', function (t) { const req = createReq('127.0.0.1') - t.doesNotThrow(proxyaddr.bind(null, req, '127.0.0.1')) - t.end() + t.assert.doesNotThrow(proxyaddr.bind(null, req, '127.0.0.1')) }) test('trust should accept IPv6', function (t) { const req = createReq('127.0.0.1') - t.doesNotThrow(proxyaddr.bind(null, req, '::1')) - t.end() + t.assert.doesNotThrow(proxyaddr.bind(null, req, '::1')) }) test('trust should accept IPv4-style IPv6', function (t) { const req = createReq('127.0.0.1') - t.doesNotThrow(proxyaddr.bind(null, req, '::ffff:127.0.0.1')) - t.end() + t.assert.doesNotThrow(proxyaddr.bind(null, req, '::ffff:127.0.0.1')) }) test('trust should accept pre-defined names', function (t) { const req = createReq('127.0.0.1') - t.doesNotThrow(proxyaddr.bind(null, req, 'loopback')) - t.end() + t.assert.doesNotThrow(proxyaddr.bind(null, req, 'loopback')) }) test('trust should accept pre-defined names in array', function (t) { const req = createReq('127.0.0.1') - t.doesNotThrow(proxyaddr.bind(null, req, ['loopback', '10.0.0.1'])) - t.end() + t.assert.doesNotThrow(proxyaddr.bind(null, req, ['loopback', '10.0.0.1'])) }) test('trust should not alter input array', function (t) { const arr = ['loopback', '10.0.0.1'] const req = createReq('127.0.0.1') - t.doesNotThrow(proxyaddr.bind(null, req, arr)) - t.same(arr, ['loopback', '10.0.0.1']) - t.end() + t.assert.doesNotThrow(proxyaddr.bind(null, req, arr)) + t.assert.deepStrictEqual(arr, ['loopback', '10.0.0.1']) }) test('trust should reject non-IP', function (t) { const req = createReq('127.0.0.1') - t.throws(proxyaddr.bind(null, req, 'blargh'), /invalid IP address/u) - t.throws(proxyaddr.bind(null, req, '10.0.300.1'), /invalid IP address/u) - t.throws(proxyaddr.bind(null, req, '::ffff:30.168.1.9000'), /invalid IP address/u) - t.throws(proxyaddr.bind(null, req, '-1'), /invalid IP address/u) - t.end() + t.assert.throws(proxyaddr.bind(null, req, 'blargh'), /invalid IP address/u) + t.assert.throws(proxyaddr.bind(null, req, '10.0.300.1'), /invalid IP address/u) + t.assert.throws(proxyaddr.bind(null, req, '::ffff:30.168.1.9000'), /invalid IP address/u) + t.assert.throws(proxyaddr.bind(null, req, '-1'), /invalid IP address/u) }) test('trust should reject bad CIDR', function (t) { const req = createReq('127.0.0.1') - t.throws(proxyaddr.bind(null, req, '10.0.0.1/internet'), /invalid range on address/u) - t.throws(proxyaddr.bind(null, req, '10.0.0.1/6000'), /invalid range on address/u) - t.throws(proxyaddr.bind(null, req, '::1/6000'), /invalid range on address/u) - t.throws(proxyaddr.bind(null, req, '::ffff:a00:2/136'), /invalid range on address/u) - t.throws(proxyaddr.bind(null, req, '::ffff:a00:2/-1'), /invalid range on address/u) - t.end() + t.assert.throws(proxyaddr.bind(null, req, '10.0.0.1/internet'), /invalid range on address/u) + t.assert.throws(proxyaddr.bind(null, req, '10.0.0.1/6000'), /invalid range on address/u) + t.assert.throws(proxyaddr.bind(null, req, '::1/6000'), /invalid range on address/u) + t.assert.throws(proxyaddr.bind(null, req, '::ffff:a00:2/136'), /invalid range on address/u) + t.assert.throws(proxyaddr.bind(null, req, '::ffff:a00:2/-1'), /invalid range on address/u) }) test('trust should reject bad netmask', function (t) { const req = createReq('127.0.0.1') - t.throws(proxyaddr.bind(null, req, '10.0.0.1/255.0.255.0'), /invalid range on address/u) - t.throws(proxyaddr.bind(null, req, '10.0.0.1/ffc0::'), /invalid range on address/u) - t.throws(proxyaddr.bind(null, req, 'fe80::/ffc0::'), /invalid range on address/u) - t.throws(proxyaddr.bind(null, req, 'fe80::/255.255.255.0'), /invalid range on address/u) - t.throws(proxyaddr.bind(null, req, '::ffff:a00:2/255.255.255.0'), /invalid range on address/u) - t.end() + t.assert.throws(proxyaddr.bind(null, req, '10.0.0.1/255.0.255.0'), /invalid range on address/u) + t.assert.throws(proxyaddr.bind(null, req, '10.0.0.1/ffc0::'), /invalid range on address/u) + t.assert.throws(proxyaddr.bind(null, req, 'fe80::/ffc0::'), /invalid range on address/u) + t.assert.throws(proxyaddr.bind(null, req, 'fe80::/255.255.255.0'), /invalid range on address/u) + t.assert.throws(proxyaddr.bind(null, req, '::ffff:a00:2/255.255.255.0'), /invalid range on address/u) }) test('trust should be invoked as trust(addr, i)', function (t) { @@ -115,260 +100,226 @@ test('trust should be invoked as trust(addr, i)', function (t) { return log.push(Array.prototype.slice.call(arguments)) }) - t.same(log, [ + t.assert.deepStrictEqual(log, [ ['127.0.0.1', 0], ['10.0.0.1', 1] ]) - - t.end() }) test('with all trusted should return socket address wtesth no headers', function (t) { const req = createReq('127.0.0.1') - t.equal(proxyaddr(req, all), '127.0.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, all), '127.0.0.1') }) test('with all trusted should return header value', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.1' }) - t.equal(proxyaddr(req, all), '10.0.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, all), '10.0.0.1') }) test('with all trusted should return furthest header value', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, all), '10.0.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, all), '10.0.0.1') }) test('with none trusted should return socket address wtesth no headers', function (t) { const req = createReq('127.0.0.1') - t.equal(proxyaddr(req, none), '127.0.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, none), '127.0.0.1') }) test('with none trusted should return socket address wtesth headers', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, none), '127.0.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, none), '127.0.0.1') }) test('with some trusted should return socket address wtesth no headers', function (t) { const req = createReq('127.0.0.1') - t.equal(proxyaddr(req, trust10x), '127.0.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, trust10x), '127.0.0.1') }) test('with some trusted should return socket address when not trusted', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, trust10x), '127.0.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, trust10x), '127.0.0.1') }) test('with some trusted should return header when socket trusted', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1' }) - t.equal(proxyaddr(req, trust10x), '192.168.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, trust10x), '192.168.0.1') }) test('with some trusted should return first untrusted after trusted', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, trust10x), '192.168.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, trust10x), '192.168.0.1') }) test('with some trusted should not skip untrusted', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '10.0.0.3, 192.168.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, trust10x), '192.168.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, trust10x), '192.168.0.1') }) test('when given array should accept ltesteral IP addresses', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1') }) test('when given array should not trust non-IP addresses', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.2, localhost' }) - t.equal(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), 'localhost') - t.end() + t.assert.strictEqual(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), 'localhost') }) test('when given array should return socket address if none match', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, ['127.0.0.1', '192.168.0.100']), '10.0.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, ['127.0.0.1', '192.168.0.100']), '10.0.0.1') }) test('when array empty should return socket address ', function (t) { const req = createReq('127.0.0.1') - t.equal(proxyaddr(req, []), '127.0.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, []), '127.0.0.1') }) test('when array empty should return socket address wtesth headers', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, []), '127.0.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, []), '127.0.0.1') }) test('when given IPv4 addresses should accept ltesteral IP addresses', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1') }) test('when given IPv4 addresses should accept CIDR notation', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.200' }) - t.equal(proxyaddr(req, '10.0.0.2/26'), '10.0.0.200') - t.end() + t.assert.strictEqual(proxyaddr(req, '10.0.0.2/26'), '10.0.0.200') }) test('when given IPv4 addresses should accept netmask notation', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.200' }) - t.equal(proxyaddr(req, '10.0.0.2/255.255.255.192'), '10.0.0.200') - t.end() + t.assert.strictEqual(proxyaddr(req, '10.0.0.2/255.255.255.192'), '10.0.0.200') }) test('when given IPv6 addresses should accept ltesteral IP addresses', function (t) { const req = createReq('fe80::1', { 'x-forwarded-for': '2002:c000:203::1, fe80::2' }) - t.equal(proxyaddr(req, ['fe80::1', 'fe80::2']), '2002:c000:203::1') - t.end() + t.assert.strictEqual(proxyaddr(req, ['fe80::1', 'fe80::2']), '2002:c000:203::1') }) test('when given IPv6 addresses should accept CIDR notation', function (t) { const req = createReq('fe80::1', { 'x-forwarded-for': '2002:c000:203::1, fe80::ff00' }) - t.equal(proxyaddr(req, 'fe80::/125'), 'fe80::ff00') - t.end() + t.assert.strictEqual(proxyaddr(req, 'fe80::/125'), 'fe80::ff00') }) test('with IP version mixed should match respective versions', function (t) { const req = createReq('::1', { 'x-forwarded-for': '2002:c000:203::1' }) - t.equal(proxyaddr(req, ['127.0.0.1', '::1']), '2002:c000:203::1') - t.end() + t.assert.strictEqual(proxyaddr(req, ['127.0.0.1', '::1']), '2002:c000:203::1') }) test('with IP version mixed should not match IPv4 to IPv6', function (t) { const req = createReq('::1', { 'x-forwarded-for': '2002:c000:203::1' }) - t.equal(proxyaddr(req, '127.0.0.1'), '::1') - t.end() + t.assert.strictEqual(proxyaddr(req, '127.0.0.1'), '::1') }) test('when IPv4-mapped IPv6 addresses should match IPv4 trust to IPv6 request', function (t) { const req = createReq('::ffff:a00:1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, ['10.0.0.1', '10.0.0.2']), '192.168.0.1') }) test('when IPv4-mapped IPv6 addresses should match IPv4 netmask trust to IPv6 request', function (t) { const req = createReq('::ffff:a00:1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, ['10.0.0.1/16']), '192.168.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, ['10.0.0.1/16']), '192.168.0.1') }) test('when IPv4-mapped IPv6 addresses should match IPv6 trust to IPv4 request', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.2' }) - t.equal(proxyaddr(req, ['::ffff:a00:1', '::ffff:a00:2']), '192.168.0.1') - t.end() + t.assert.strictEqual(proxyaddr(req, ['::ffff:a00:1', '::ffff:a00:2']), '192.168.0.1') }) test('when IPv4-mapped IPv6 addresses should match CIDR notation for IPv4-mapped address', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.200' }) - t.equal(proxyaddr(req, '::ffff:a00:2/122'), '10.0.0.200') - t.end() + t.assert.strictEqual(proxyaddr(req, '::ffff:a00:2/122'), '10.0.0.200') }) test('when IPv4-mapped IPv6 addresses should match CIDR notation for IPv4-mapped address mixed wtesth IPv6 CIDR', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.200' }) - t.equal(proxyaddr(req, ['::ffff:a00:2/122', 'fe80::/125']), '10.0.0.200') - t.end() + t.assert.strictEqual(proxyaddr(req, ['::ffff:a00:2/122', 'fe80::/125']), '10.0.0.200') }) test('when IPv4-mapped IPv6 addresses should match CIDR notation for IPv4-mapped address mixed wtesth IPv4 addresses', function (t) { const req = createReq('10.0.0.1', { 'x-forwarded-for': '192.168.0.1, 10.0.0.200' }) - t.equal(proxyaddr(req, ['::ffff:a00:2/122', '127.0.0.1']), '10.0.0.200') - t.end() + t.assert.strictEqual(proxyaddr(req, ['::ffff:a00:2/122', '127.0.0.1']), '10.0.0.200') }) test('when given predefined names should accept single pre-defined name', function (t) { const req = createReq('fe80::1', { 'x-forwarded-for': '2002:c000:203::1, fe80::2' }) - t.equal(proxyaddr(req, 'linklocal'), '2002:c000:203::1') - t.end() + t.assert.strictEqual(proxyaddr(req, 'linklocal'), '2002:c000:203::1') }) test('when given predefined names should accept multiple pre-defined names', function (t) { const req = createReq('::1', { 'x-forwarded-for': '2002:c000:203::1, fe80::2' }) - t.equal(proxyaddr(req, ['loopback', 'linklocal']), '2002:c000:203::1') - t.end() + t.assert.strictEqual(proxyaddr(req, ['loopback', 'linklocal']), '2002:c000:203::1') }) test('when header contains non-ip addresses should stop at first non-ip after trusted', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': 'myrouter, 127.0.0.1, proxy' }) - t.equal(proxyaddr(req, '127.0.0.1'), 'proxy') - t.end() + t.assert.strictEqual(proxyaddr(req, '127.0.0.1'), 'proxy') }) test('when header contains non-ip addresses should stop at first malformed ip after trusted', function (t) { const req = createReq('127.0.0.1', { 'x-forwarded-for': 'myrouter, 127.0.0.1, ::8:8:8:8:8:8:8:8:8' }) - t.equal(proxyaddr(req, '127.0.0.1'), '::8:8:8:8:8:8:8:8:8') - t.end() + t.assert.strictEqual(proxyaddr(req, '127.0.0.1'), '::8:8:8:8:8:8:8:8:8') }) test('when header contains non-ip addresses should provide all values to function', function (t) { @@ -381,26 +332,23 @@ test('when header contains non-ip addresses should provide all values to functio return log.push(Array.prototype.slice.call(arguments)) }) - t.same(log, [ + t.assert.deepStrictEqual(log, [ ['127.0.0.1', 0], ['proxy', 1], ['127.0.0.1', 2] ]) - t.end() }) test('when socket address undefined should return undefined as address', function (t) { const req = createReq(undefined) - t.equal(proxyaddr(req, '127.0.0.1'), undefined) - t.end() + t.assert.strictEqual(proxyaddr(req, '127.0.0.1'), undefined) }) test('when socket address undefined should return undefined even wtesth trusted headers', function (t) { const req = createReq(undefined, { 'x-forwarded-for': '127.0.0.1, 10.0.0.1' }) - t.equal(proxyaddr(req, '127.0.0.1'), undefined) - t.end() + t.assert.strictEqual(proxyaddr(req, '127.0.0.1'), undefined) }) function createReq (socketAddr, headers) { diff --git a/test/compile.js b/test/compile.js deleted file mode 100644 index 599a73d..0000000 --- a/test/compile.js +++ /dev/null @@ -1,70 +0,0 @@ -'use strict' - -const test = require('tape') -const proxyaddr = require('..') - -test('trust arg should be required', function (t) { - t.throws(proxyaddr.compile, /argument.*required/u) - t.end() -}) - -test('trust arg should accept an array', function (t) { - t.equal(typeof proxyaddr.compile([]), 'function') - t.end() -}) - -test('trust arg should accept a string', function (t) { - t.equal(typeof proxyaddr.compile('127.0.0.1'), 'function') - t.end() -}) - -test('trust arg should reject a number', function (t) { - t.throws(proxyaddr.compile.bind(null, 42), /unsupported trust argument/u) - t.end() -}) - -test('trust arg should accept IPv4', function (t) { - t.equal(typeof proxyaddr.compile('127.0.0.1'), 'function') - t.end() -}) - -test('trust arg should accept IPv6', function (t) { - t.equal(typeof proxyaddr.compile('::1'), 'function') - t.end() -}) - -test('trust arg should accept IPv4-style IPv6', function (t) { - t.equal(typeof proxyaddr.compile('::ffff:127.0.0.1'), 'function') - t.end() -}) - -test('trust arg should accept pre-defined names', function (t) { - t.equal(typeof proxyaddr.compile('loopback'), 'function') - t.end() -}) - -test('trust arg should accept pre-defined names in array', function (t) { - t.equal(typeof proxyaddr.compile(['loopback', '10.0.0.1']), 'function') - t.end() -}) - -test('trust arg should reject non-IP', function (t) { - t.throws(proxyaddr.compile.bind(null, 'blargh'), /invalid IP address/u) - t.throws(proxyaddr.compile.bind(null, '-1'), /invalid IP address/u) - t.end() -}) - -test('trust arg should reject bad CIDR', function (t) { - t.throws(proxyaddr.compile.bind(null, '10.0.0.1/6000'), /invalid range on address/u) - t.throws(proxyaddr.compile.bind(null, '::1/6000'), /invalid range on address/u) - t.throws(proxyaddr.compile.bind(null, '::ffff:a00:2/136'), /invalid range on address/u) - t.throws(proxyaddr.compile.bind(null, '::ffff:a00:2/-46'), /invalid range on address/u) - t.end() -}) - -test('trust arg should not alter input array', function (t) { - const arr = ['loopback', '10.0.0.1'] - t.equal(typeof proxyaddr.compile(arr), 'function') - t.same(arr, ['loopback', '10.0.0.1']) - t.end() -}) diff --git a/test/compile.test.js b/test/compile.test.js new file mode 100644 index 0000000..c3b732a --- /dev/null +++ b/test/compile.test.js @@ -0,0 +1,58 @@ +'use strict' + +const { test } = require('node:test') +const proxyaddr = require('..') + +test('trust arg should be required', function (t) { + t.assert.throws(proxyaddr.compile, /argument.*required/u) +}) + +test('trust arg should accept an array', function (t) { + t.assert.strictEqual(typeof proxyaddr.compile([]), 'function') +}) + +test('trust arg should accept a string', function (t) { + t.assert.strictEqual(typeof proxyaddr.compile('127.0.0.1'), 'function') +}) + +test('trust arg should reject a number', function (t) { + t.assert.throws(proxyaddr.compile.bind(null, 42), /unsupported trust argument/u) +}) + +test('trust arg should accept IPv4', function (t) { + t.assert.strictEqual(typeof proxyaddr.compile('127.0.0.1'), 'function') +}) + +test('trust arg should accept IPv6', function (t) { + t.assert.strictEqual(typeof proxyaddr.compile('::1'), 'function') +}) + +test('trust arg should accept IPv4-style IPv6', function (t) { + t.assert.strictEqual(typeof proxyaddr.compile('::ffff:127.0.0.1'), 'function') +}) + +test('trust arg should accept pre-defined names', function (t) { + t.assert.strictEqual(typeof proxyaddr.compile('loopback'), 'function') +}) + +test('trust arg should accept pre-defined names in array', function (t) { + t.assert.strictEqual(typeof proxyaddr.compile(['loopback', '10.0.0.1']), 'function') +}) + +test('trust arg should reject non-IP', function (t) { + t.assert.throws(proxyaddr.compile.bind(null, 'blargh'), /invalid IP address/u) + t.assert.throws(proxyaddr.compile.bind(null, '-1'), /invalid IP address/u) +}) + +test('trust arg should reject bad CIDR', function (t) { + t.assert.throws(proxyaddr.compile.bind(null, '10.0.0.1/6000'), /invalid range on address/u) + t.assert.throws(proxyaddr.compile.bind(null, '::1/6000'), /invalid range on address/u) + t.assert.throws(proxyaddr.compile.bind(null, '::ffff:a00:2/136'), /invalid range on address/u) + t.assert.throws(proxyaddr.compile.bind(null, '::ffff:a00:2/-46'), /invalid range on address/u) +}) + +test('trust arg should not alter input array', function (t) { + const arr = ['loopback', '10.0.0.1'] + t.assert.strictEqual(typeof proxyaddr.compile(arr), 'function') + t.assert.deepStrictEqual(arr, ['loopback', '10.0.0.1']) +})