Skip to content

Commit

Permalink
chore: Renable autoSelectFamily tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Jul 4, 2023
1 parent e66e2c6 commit 86c872b
Showing 1 changed file with 67 additions and 61 deletions.
128 changes: 67 additions & 61 deletions test/autoselectfamily.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ const { Resolver } = require('dns')
const dnsPacket = require('dns-packet')
const { createServer } = require('http')
const { Client, Agent, request } = require('..')
const { nodeHasAutoSelectFamily, nodeMajor } = require('../lib/core/util')

if (nodeMajor >= 20) {
skip('some tests are failing')
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()
}

Expand Down Expand Up @@ -59,84 +67,82 @@ function createDnsServer (ipv6Addr, ipv4Addr, cb) {
})
}

if (nodeHasAutoSelectFamily) {
test('with autoSelectFamily enable the request succeeds when using request', (t) => {
t.plan(3)
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')
})
createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
const server = createServer((req, res) => {
res.end('hello')
})

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

server.listen(0, '127.0.0.1', () => {
const agent = new Agent({ connect: { lookup }, autoSelectFamily: true })
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)
request(
`http://example.org:${server.address().port}/`, {
method: 'GET',
dispatcher: agent
}, (err, { statusCode, body }) => {
t.error(err)

let response = Buffer.alloc(0)
let response = Buffer.alloc(0)

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

body.on('end', () => {
t.strictSame(statusCode, 200)
t.strictSame(response.toString('utf-8'), 'hello')
})
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)
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')
})
createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
const server = createServer((req, res) => {
res.end('hello')
})

t.teardown(() => {
server.close()
dnsServer.close()
})
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 })
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))
t.teardown(client.destroy.bind(client))

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

let response = Buffer.alloc(0)
let response = Buffer.alloc(0)

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

body.on('end', () => {
t.strictSame(statusCode, 200)
t.strictSame(response.toString('utf-8'), 'hello')
})
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)
Expand All @@ -152,7 +158,7 @@ test('with autoSelectFamily disabled the request fails when using request', (t)
})

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

request(`http://example.org:${server.address().port}`, {
method: 'GET',
Expand All @@ -178,7 +184,7 @@ test('with autoSelectFamily disabled the request fails when using a client', (t)
})

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

client.request({
Expand Down

0 comments on commit 86c872b

Please sign in to comment.