Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: port autoselectfamily.js tests to node:test runner #2570

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 34 additions & 25 deletions test/autoselectfamily.js → test/node-test/autoselectfamily.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict'

const { test, skip } = require('tap')
const { test, skip } = require('node:test')
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')
const { Client, Agent, request } = require('../..')
const { nodeHasAutoSelectFamily } = require('../../lib/core/util')
const { tspl } = require('@matteo.collina/tspl')

/*
* IMPORTANT
Expand Down Expand Up @@ -67,15 +68,15 @@ function createDnsServer (ipv6Addr, ipv4Addr, cb) {
})
}

test('with autoSelectFamily enable the request succeeds when using request', (t) => {
t.plan(3)
test('with autoSelectFamily enable the request succeeds when using request', async (t) => {
const p = tspl(t, { plan: 3 })

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

t.teardown(() => {
t.after(() => {
server.close()
dnsServer.close()
})
Expand All @@ -88,7 +89,7 @@ test('with autoSelectFamily enable the request succeeds when using request', (t)
method: 'GET',
dispatcher: agent
}, (err, { statusCode, body }) => {
t.error(err)
p.ifError(err)

let response = Buffer.alloc(0)

Expand All @@ -97,37 +98,39 @@ test('with autoSelectFamily enable the request succeeds when using request', (t)
})

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

await p.completed
})

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', async (t) => {
const p = tspl(t, { plan: 3 })

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

t.teardown(() => {
t.after(() => {
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))
t.after(client.destroy.bind(client))

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

let response = Buffer.alloc(0)

Expand All @@ -136,23 +139,25 @@ test('with autoSelectFamily enable the request succeeds when using a client', (t
})

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

await p.completed
})

test('with autoSelectFamily disabled the request fails when using request', (t) => {
t.plan(1)
test('with autoSelectFamily disabled the request fails when using request', async (t) => {
const p = tspl(t, { plan: 1 })

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

t.teardown(() => {
t.after(() => {
server.close()
dnsServer.close()
})
Expand All @@ -164,35 +169,39 @@ test('with autoSelectFamily disabled the request fails when using request', (t)
method: 'GET',
dispatcher: agent
}, (err, { statusCode, body }) => {
t.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
p.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
})
})
})

await p.completed
})

test('with autoSelectFamily disabled the request fails when using a client', (t) => {
t.plan(1)
test('with autoSelectFamily disabled the request fails when using a client', async (t) => {
const p = tspl(t, { plan: 1 })

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

t.teardown(() => {
t.after(() => {
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))
t.after(client.destroy.bind(client))

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

await p.completed
})
60 changes: 60 additions & 0 deletions test/utils/node-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 08f162d

* A port of tap's `t.type` that can be used with `node:assert`
* https://github.com/tapjs/tapjs/blob/511019b2ac0fa014370154c3a341a0e632f50b19/src/asserts/src/index.ts#L199
*/
function ttype (obj, klass) {
const name =
typeof klass === 'function'
? klass.name || '(anonymous constructor)'
: klass

if (obj === klass) {
return true
}

const tof = typeof obj
const type =
!obj && tof === 'object'
? 'null'
// treat as object, but not Object
// t.type(() => {}, Function)
: tof === 'function' &&
typeof klass === 'function' &&
klass !== Object
? 'object'
: tof

if (
(type === 'number' && klass === Number) ||
(type === 'string' && klass === String) ||
(type === 'bigint' && klass === BigInt) ||
(klass === 'array' && Array.isArray(obj)) ||
(type === 'symbol' && klass === Symbol)
) {
return true
}

// simplest case, it literally is the same thing
if (type === 'object' && klass !== 'object') {
if (typeof klass === 'function') {
return obj instanceof klass
}

// check prototype chain for name
// at this point, we already know klass is not a function
// if the klass specified is an obj in the proto chain, pass
// if the name specified is the name of a ctor in the chain, pass
for (let p = obj; p; p = Object.getPrototypeOf(p)) {
const ctor = p.constructor && p.constructor.name
if (p === klass || ctor === name) {
return true
}
}
}

return type === name
}

module.exports = {
ttype
}