Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions test/node-test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
const { test } = require('node:test')
const { spawn } = require('node:child_process')
const { join } = require('node:path')
const { tspl } = require('@matteo.collina/tspl')

// eslint-disable-next-line no-control-regex
const removeEscapeColorsRE = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g

const isNode23Plus = process.versions.node.split('.')[0] >= 23
const isCITGM = !!process.env.CITGM

test('debug#websocket', { skip: !process.versions.icu || isCITGM || isNode23Plus }, async t => {
const assert = tspl(t, { plan: 6 })
test('debug#websocket', { skip: !process.versions.icu || isCITGM || isNode23Plus }, (t, done) => {
t.plan(6)
const child = spawn(
process.execPath,
[
Expand Down Expand Up @@ -40,17 +39,16 @@ test('debug#websocket', { skip: !process.versions.icu || isCITGM || isNode23Plus
})
child.stderr.on('end', () => {
const lines = extractLines(chunks)
assert.strictEqual(lines.length, assertions.length)
t.assert.strictEqual(lines.length, assertions.length)
for (let i = 1; i < lines.length; i++) {
assert.match(lines[i], assertions[i])
t.assert.match(lines[i], assertions[i])
}
done()
})

await assert.completed
})

test('debug#fetch', { skip: isCITGM || isNode23Plus }, async t => {
const assert = tspl(t, { plan: 7 })
test('debug#fetch', { skip: isCITGM || isNode23Plus }, (t, done) => {
t.plan(7)
const child = spawn(
process.execPath,
[
Expand All @@ -77,18 +75,17 @@ test('debug#fetch', { skip: isCITGM || isNode23Plus }, async t => {
})
child.stderr.on('end', () => {
const lines = extractLines(chunks)
assert.strictEqual(lines.length, assertions.length)
t.assert.strictEqual(lines.length, assertions.length)
for (let i = 0; i < lines.length; i++) {
assert.match(lines[i], assertions[i])
t.assert.match(lines[i], assertions[i])
}
done()
})

await assert.completed
})

test('debug#undici', { skip: isCITGM || isNode23Plus }, async t => {
test('debug#undici', { skip: isCITGM || isNode23Plus }, (t, done) => {
// Due to Node.js webpage redirect
const assert = tspl(t, { plan: 7 })
t.plan(7)
const child = spawn(
process.execPath,
[
Expand Down Expand Up @@ -117,13 +114,12 @@ test('debug#undici', { skip: isCITGM || isNode23Plus }, async t => {
})
child.stderr.on('end', () => {
const lines = extractLines(chunks)
assert.strictEqual(lines.length, assertions.length)
t.assert.strictEqual(lines.length, assertions.length)
for (let i = 0; i < lines.length; i++) {
assert.match(lines[i], assertions[i])
t.assert.match(lines[i], assertions[i])
}
done()
})

await assert.completed
})

function extractLines (chunks) {
Expand Down
7 changes: 3 additions & 4 deletions test/node-test/large-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const { test } = require('node:test')
const { createServer } = require('node:http')
const { request } = require('../../')
const { strictEqual } = require('node:assert')

test('socket should not be reused unless body is consumed', async (t) => {
const LARGE_BODY = 'x'.repeat(10000000)
Expand Down Expand Up @@ -39,7 +38,7 @@ test('socket should not be reused unless body is consumed', async (t) => {
const fooBody = await fooRes.body.text()
await barRes.body.text()

strictEqual(fooRes.headers['content-length'], String(LARGE_BODY.length))
strictEqual(fooBody.length, LARGE_BODY.length)
strictEqual(fooBody, LARGE_BODY)
t.assert.strictEqual(fooRes.headers['content-length'], String(LARGE_BODY.length))
t.assert.strictEqual(fooBody.length, LARGE_BODY.length)
t.assert.strictEqual(fooBody, LARGE_BODY)
})
31 changes: 15 additions & 16 deletions test/node-test/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,47 @@
const { TernarySearchTree, tree } = require('../../lib/core/tree')
const { wellknownHeaderNames, headerNameLowerCasedRecord } = require('../../lib/core/constants')
const { describe, test } = require('node:test')
const assert = require('node:assert')

describe('Ternary Search Tree', () => {
test('The empty key cannot be added.', () => {
assert.throws(() => new TernarySearchTree().insert('', ''))
test('The empty key cannot be added.', (t) => {
t.assert.throws(() => new TernarySearchTree().insert('', ''))
const tst = new TernarySearchTree()
tst.insert('a', 'a')
assert.throws(() => tst.insert('', ''))
t.assert.throws(() => tst.insert('', ''))
})

test('looking up not inserted key returns null', () => {
test('looking up not inserted key returns null', (t) => {
const tst = new TernarySearchTree()
tst.insert('a', 'a')
assert.strictEqual(tst.lookup(Buffer.from('non-existent')), null)
t.assert.strictEqual(tst.lookup(Buffer.from('non-existent')), null)
})

test('not ascii string', () => {
assert.throws(() => new TernarySearchTree().insert('\x80', 'a'))
test('not ascii string', (t) => {
t.assert.throws(() => new TernarySearchTree().insert('\x80', 'a'))
const tst = new TernarySearchTree()
tst.insert('a', 'a')
// throw on TstNode
assert.throws(() => tst.insert('\x80', 'a'))
t.assert.throws(() => tst.insert('\x80', 'a'))
})

test('duplicate key', () => {
test('duplicate key', (t) => {
const tst = new TernarySearchTree()
const key = 'a'
const lookupKey = Buffer.from(key)
tst.insert(key, 'a')
assert.strictEqual(tst.lookup(lookupKey), 'a')
t.assert.strictEqual(tst.lookup(lookupKey), 'a')
tst.insert(key, 'b')
assert.strictEqual(tst.lookup(lookupKey), 'b')
t.assert.strictEqual(tst.lookup(lookupKey), 'b')
})

test('tree', () => {
test('tree', (t) => {
for (let i = 0; i < wellknownHeaderNames.length; ++i) {
const key = wellknownHeaderNames[i]
assert.strictEqual(tree.lookup(Buffer.from(key)), headerNameLowerCasedRecord[key])
t.assert.strictEqual(tree.lookup(Buffer.from(key)), headerNameLowerCasedRecord[key])
}
})

test('fuzz', () => {
test('fuzz', (t) => {
const LENGTH = 2000
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const charactersLength = characters.length
Expand All @@ -71,7 +70,7 @@ describe('Ternary Search Tree', () => {
}

for (let i = 0; i < LENGTH; ++i) {
assert.strictEqual(tst.lookup(randomBuffer[i]), random[i])
t.assert.strictEqual(tst.lookup(randomBuffer[i]), random[i])
}
})
})
59 changes: 32 additions & 27 deletions test/node-test/unix.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ const http = require('node:http')
const https = require('node:https')
const pem = require('@metcoder95/https-pem')
const fs = require('node:fs')
const { tspl } = require('@matteo.collina/tspl')
const { once } = require('node:events')

const skip = process.platform === 'win32'

test('http unix get', { skip }, async (t) => {
let client
const p = tspl(t, { plan: 7 })
t.plan(7)

const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
p.equal('/', req.url)
p.equal('GET', req.method)
p.equal('localhost', req.headers.host)
t.assert.strictEqual('/', req.url)
t.assert.strictEqual('GET', req.method)
t.assert.strictEqual('localhost', req.headers.host)
res.setHeader('Content-Type', 'text/plain')
res.end('hello')
})
Expand All @@ -42,31 +42,32 @@ test('http unix get', { skip }, async (t) => {
})

client.request({ path: '/', method: 'GET' }, (err, data) => {
p.ifError(err)
t.assert.ifError(err)
const { statusCode, headers, body } = data
p.equal(statusCode, 200)
p.equal(headers['content-type'], 'text/plain')
t.assert.strictEqual(statusCode, 200)
t.assert.strictEqual(headers['content-type'], 'text/plain')
const bufs = []
body.on('data', (buf) => {
bufs.push(buf)
})
body.on('end', () => {
p.equal('hello', Buffer.concat(bufs).toString('utf8'))
t.assert.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
server.close()
})
})
})

await p.completed
await once(server, 'close')
})

test('http unix get pool', { skip }, async (t) => {
let client
const p = tspl(t, { plan: 7 })
t.plan(7)

const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
p.equal('/', req.url)
p.equal('GET', req.method)
p.equal('localhost', req.headers.host)
t.assert.strictEqual('/', req.url)
t.assert.strictEqual('GET', req.method)
t.assert.strictEqual('localhost', req.headers.host)
res.setHeader('Content-Type', 'text/plain')
res.end('hello')
})
Expand All @@ -91,30 +92,32 @@ test('http unix get pool', { skip }, async (t) => {
})

client.request({ path: '/', method: 'GET' }, (err, data) => {
p.ifError(err)
t.assert.ifError(err)
const { statusCode, headers, body } = data
p.equal(statusCode, 200)
p.equal(headers['content-type'], 'text/plain')
t.assert.strictEqual(statusCode, 200)
t.assert.strictEqual(headers['content-type'], 'text/plain')
const bufs = []
body.on('data', (buf) => {
bufs.push(buf)
})
body.on('end', () => {
p.equal('hello', Buffer.concat(bufs).toString('utf8'))
t.assert.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
server.close()
})
})
})

await p.completed
await once(server, 'close')
})

test('https get with tls opts', { skip }, async (t) => {
t.plan(6)

let client
const p = tspl(t, { plan: 6 })

const server = https.createServer({ ...pem, joinDuplicateHeaders: true }, (req, res) => {
p.equal('/', req.url)
p.equal('GET', req.method)
t.assert.strictEqual('/', req.url)
t.assert.strictEqual('GET', req.method)
res.setHeader('content-type', 'text/plain')
res.end('hello')
})
Expand Down Expand Up @@ -142,18 +145,20 @@ test('https get with tls opts', { skip }, async (t) => {
})

client.request({ path: '/', method: 'GET' }, (err, data) => {
p.ifError(err)
t.assert.ifError(err)
const { statusCode, headers, body } = data
p.equal(statusCode, 200)
p.equal(headers['content-type'], 'text/plain')
t.assert.strictEqual(statusCode, 200)
t.assert.strictEqual(headers['content-type'], 'text/plain')
const bufs = []
body.on('data', (buf) => {
bufs.push(buf)
})
body.on('end', () => {
p.equal('hello', Buffer.concat(bufs).toString('utf8'))
t.assert.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
server.close()
})
})
})
await p.completed

await once(server, 'close')
})
Loading
Loading