File size: 2,126 Bytes
23ac194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'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()
})