summaryrefslogtreecommitdiffstats
path: root/test/autoselectfamily.js
blob: 0b44a3eb8879608b5b18d33890722892d6efd269 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use strict'

const { test, skip } = require('tap')
const dgram = require('dgram')
const { Resolver } = require('dns')
const dnsPacket = require('dns-packet')
const { createServer } = require('http')
const { Client, Agent, request } = require('..')
const { nodeHasAutoSelectFamily } = require('../lib/core/util')

/*
 * IMPORTANT
 *
 * As only some version of Node have autoSelectFamily enabled by default (>= 20), make sure the option is always
 * explicitly passed in tests in this file to avoid compatibility problems across release lines.
 *
 */

if (!nodeHasAutoSelectFamily) {
  skip('autoSelectFamily is not supportee')
  process.exit()
}

function _lookup (resolver, hostname, options, cb) {
  resolver.resolve(hostname, 'ANY', (err, replies) => {
    if (err) {
      return cb(err)
    }

    const hosts = replies
      .map((r) => ({ address: r.address, family: r.type === 'AAAA' ? 6 : 4 }))
      .sort((a, b) => b.family - a.family)

    if (options.all === true) {
      return cb(null, hosts)
    }

    return cb(null, hosts[0].address, hosts[0].family)
  })
}

function createDnsServer (ipv6Addr, ipv4Addr, cb) {
  // Create a DNS server which replies with a AAAA and a A record for the same host
  const socket = dgram.createSocket('udp4')

  socket.on('message', (msg, { address, port }) => {
    const parsed = dnsPacket.decode(msg)

    const response = dnsPacket.encode({
      type: 'answer',
      id: parsed.id,
      questions: parsed.questions,
      answers: [
        { type: 'AAAA', class: 'IN', name: 'example.org', data: '::1', ttl: 123 },
        { type: 'A', class: 'IN', name: 'example.org', data: '127.0.0.1', ttl: 123 }
      ]
    })

    socket.send(response, port, address)
  })

  socket.bind(0, () => {
    const resolver = new Resolver()
    resolver.setServers([`127.0.0.1:${socket.address().port}`])

    cb(null, { dnsServer: socket, lookup: _lookup.bind(null, resolver) })
  })
}

test('with autoSelectFamily enable the request succeeds when using request', (t) => {
  t.plan(3)

  createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
    const server = createServer((req, res) => {
      res.end('hello')
    })

    t.teardown(() => {
      server.close()
      dnsServer.close()
    })

    server.listen(0, '127.0.0.1', () => {
      const agent = new Agent({ connect: { lookup }, autoSelectFamily: true })

      request(
        `http://example.org:${server.address().port}/`, {
          method: 'GET',
          dispatcher: agent
        }, (err, { statusCode, body }) => {
          t.error(err)

          let response = Buffer.alloc(0)

          body.on('data', chunk => {
            response = Buffer.concat([response, chunk])
          })

          body.on('end', () => {
            t.strictSame(statusCode, 200)
            t.strictSame(response.toString('utf-8'), 'hello')
          })
        })
    })
  })
})

test('with autoSelectFamily enable the request succeeds when using a client', (t) => {
  t.plan(3)

  createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
    const server = createServer((req, res) => {
      res.end('hello')
    })

    t.teardown(() => {
      server.close()
      dnsServer.close()
    })

    server.listen(0, '127.0.0.1', () => {
      const client = new Client(`http://example.org:${server.address().port}`, { connect: { lookup }, autoSelectFamily: true })

      t.teardown(client.destroy.bind(client))

      client.request({
        path: '/',
        method: 'GET'
      }, (err, { statusCode, body }) => {
        t.error(err)

        let response = Buffer.alloc(0)

        body.on('data', chunk => {
          response = Buffer.concat([response, chunk])
        })

        body.on('end', () => {
          t.strictSame(statusCode, 200)
          t.strictSame(response.toString('utf-8'), 'hello')
        })
      })
    })
  })
})

test('with autoSelectFamily disabled the request fails when using request', (t) => {
  t.plan(1)

  createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
    const server = createServer((req, res) => {
      res.end('hello')
    })

    t.teardown(() => {
      server.close()
      dnsServer.close()
    })

    server.listen(0, '127.0.0.1', () => {
      const agent = new Agent({ connect: { lookup, autoSelectFamily: false } })

      request(`http://example.org:${server.address().port}`, {
        method: 'GET',
        dispatcher: agent
      }, (err, { statusCode, body }) => {
        t.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
      })
    })
  })
})

test('with autoSelectFamily disabled the request fails when using a client', (t) => {
  t.plan(1)

  createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
    const server = createServer((req, res) => {
      res.end('hello')
    })

    t.teardown(() => {
      server.close()
      dnsServer.close()
    })

    server.listen(0, '127.0.0.1', () => {
      const client = new Client(`http://example.org:${server.address().port}`, { connect: { lookup, autoSelectFamily: false } })
      t.teardown(client.destroy.bind(client))

      client.request({
        path: '/',
        method: 'GET'
      }, (err, { statusCode, body }) => {
        t.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
      })
    })
  })
})