Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 12 additions & 13 deletions test/utils/date.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

const { describe, test } = require('node:test')
const { deepStrictEqual } = require('node:assert')
const { parseHttpDate } = require('../../lib/util/date')
const { runtimeFeatures } = require('../../lib/util/runtime-features')

const skipFuzzing = runtimeFeatures.has('crypto') === false

describe('parseHttpDate', () => {
test('IMF-fixdate', () => {
test('IMF-fixdate', (t) => {
const values = {
'Sun, 06 Nov 1994 08:49:37 GMT': new Date(Date.UTC(1994, 10, 6, 8, 49, 37)),
'Fri, 18 Aug 1950 02:01:18 GMT': new Date(Date.UTC(1950, 7, 18, 2, 1, 18)),
Expand Down Expand Up @@ -38,11 +37,11 @@ describe('parseHttpDate', () => {
}

for (const date of Object.keys(values)) {
deepStrictEqual(parseHttpDate(date), values[date], date)
t.assert.deepStrictEqual(parseHttpDate(date), values[date], date)
}
})

test('RFC850', () => {
test('RFC850', (t) => {
const values = {
'Sunday, 06-Nov-94 08:49:37 GMT': new Date(Date.UTC(1994, 10, 6, 8, 49, 37)),
'Monday, 07-Nov-94 08:49:37 GMT': new Date(Date.UTC(1994, 10, 7, 8, 49, 37)),
Expand Down Expand Up @@ -74,11 +73,11 @@ describe('parseHttpDate', () => {
}

for (const date of Object.keys(values)) {
deepStrictEqual(parseHttpDate(date), values[date], date)
t.assert.deepStrictEqual(parseHttpDate(date), values[date], date)
}
})

test('asctime()', () => {
test('asctime()', (t) => {
const values = {
'Sun Nov 6 08:49:37 1994': new Date(Date.UTC(1994, 10, 6, 8, 49, 37)),
'Fri Aug 18 02:01:18 1950': new Date(Date.UTC(1950, 7, 18, 2, 1, 18)),
Expand Down Expand Up @@ -265,7 +264,7 @@ describe('parseHttpDate', () => {
}

for (const date of Object.keys(values)) {
deepStrictEqual(parseHttpDate(date), values[date], date)
t.assert.deepStrictEqual(parseHttpDate(date), values[date], date)
}
})

Expand All @@ -274,7 +273,7 @@ describe('parseHttpDate', () => {
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
const fuzzingCount = 1e6

test('fuzzing asctime', { skip: skipFuzzing }, () => {
test('fuzzing asctime', { skip: skipFuzzing }, (t) => {
const { randomInt } = require('node:crypto')

function asctime (date) {
Expand Down Expand Up @@ -308,11 +307,11 @@ describe('parseHttpDate', () => {
))

const dateAsAscTime = asctime(date)
deepStrictEqual(parseHttpDate(dateAsAscTime), date, `Fuzzing failed for: ${dateAsAscTime}`)
t.assert.deepStrictEqual(parseHttpDate(dateAsAscTime), date, `Fuzzing failed for: ${dateAsAscTime}`)
}
})

test('fuzzing imf', { skip: skipFuzzing }, () => {
test('fuzzing imf', { skip: skipFuzzing }, (t) => {
const { randomInt } = require('node:crypto')

for (let i = 0; i < fuzzingCount; i++) {
Expand All @@ -326,11 +325,11 @@ describe('parseHttpDate', () => {
))

const dateAsImf = date.toUTCString()
deepStrictEqual(parseHttpDate(dateAsImf), date, `Fuzzing failed for: ${dateAsImf}`)
t.assert.deepStrictEqual(parseHttpDate(dateAsImf), date, `Fuzzing failed for: ${dateAsImf}`)
}
})

test('fuzzing rfc850', { skip: skipFuzzing }, () => {
test('fuzzing rfc850', { skip: skipFuzzing }, (t) => {
const { randomInt } = require('node:crypto')

function rfc850 (date) {
Expand Down Expand Up @@ -368,7 +367,7 @@ describe('parseHttpDate', () => {
))

const dateAsRfc850 = rfc850(date)
deepStrictEqual(parseHttpDate(dateAsRfc850), date, `Fuzzing failed for: ${dateAsRfc850}`)
t.assert.deepStrictEqual(parseHttpDate(dateAsRfc850), date, `Fuzzing failed for: ${dateAsRfc850}`)
}
})
})
94 changes: 45 additions & 49 deletions test/utils/esm-wrapper.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { tspl } from '@matteo.collina/tspl'
import { createServer } from 'node:http'
import { test, after } from 'node:test'
import { once } from 'node:events'
import {
Agent,
Client,
Expand All @@ -16,16 +14,16 @@ import {
stream
} from '../../index.js'

test('imported Client works with basic GET', async (t) => {
t = tspl(t, { plan: 10 })
test('imported Client works with basic GET', (t, done) => {
t.plan(10)

const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
t.strictEqual('/', req.url)
t.strictEqual('GET', req.method)
t.strictEqual(`localhost:${server.address().port}`, req.headers.host)
t.strictEqual(undefined, req.headers.foo)
t.strictEqual('bar', req.headers.bar)
t.strictEqual(undefined, req.headers['content-length'])
t.assert.strictEqual('/', req.url)
t.assert.strictEqual('GET', req.method)
t.assert.strictEqual(`localhost:${server.address().port}`, req.headers.host)
t.assert.strictEqual(undefined, req.headers.foo)
t.assert.strictEqual('bar', req.headers.bar)
t.assert.strictEqual(undefined, req.headers['content-length'])
res.setHeader('Content-Type', 'text/plain')
res.end('hello')
})
Expand All @@ -37,69 +35,67 @@ test('imported Client works with basic GET', async (t) => {
bar: 'bar'
}

server.listen(0)

await once(server, 'listening')

const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.close())

client.request({
path: '/',
method: 'GET',
headers: reqHeaders
}, (err, data) => {
t.ifError(err)
const { statusCode, headers, body } = data
t.strictEqual(statusCode, 200)
t.strictEqual(headers['content-type'], 'text/plain')
const bufs = []
body.on('data', (buf) => {
bufs.push(buf)
})
body.on('end', () => {
t.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.close())

client.request({
path: '/',
method: 'GET',
headers: reqHeaders
}, (err, data) => {
t.assert.ifError(err)
const { statusCode, headers, body } = data
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', () => {
t.assert.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
done()
})
})
})
await t.completed
})

test('imported errors work with request args validation', (t) => {
t = tspl(t, { plan: 2 })
t.plan(2)

const client = new Client('http://localhost:5000')

client.request(null, (err) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.assert.ok(err instanceof errors.InvalidArgumentError)
})

try {
client.request(null, 'asd')
} catch (err) {
t.ok(err instanceof errors.InvalidArgumentError)
t.assert.ok(err instanceof errors.InvalidArgumentError)
}
})

test('imported errors work with request args validation promise', (t) => {
t = tspl(t, { plan: 1 })
t.plan(1)

const client = new Client('http://localhost:5000')

client.request(null).catch((err) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.assert.ok(err instanceof errors.InvalidArgumentError)
})
})

test('named exports', (t) => {
t = tspl(t, { plan: 10 })
t.strictEqual(typeof Client, 'function')
t.strictEqual(typeof Pool, 'function')
t.strictEqual(typeof Agent, 'function')
t.strictEqual(typeof request, 'function')
t.strictEqual(typeof stream, 'function')
t.strictEqual(typeof pipeline, 'function')
t.strictEqual(typeof connect, 'function')
t.strictEqual(typeof upgrade, 'function')
t.strictEqual(typeof setGlobalDispatcher, 'function')
t.strictEqual(typeof getGlobalDispatcher, 'function')
t.plan(10)
t.assert.strictEqual(typeof Client, 'function')
t.assert.strictEqual(typeof Pool, 'function')
t.assert.strictEqual(typeof Agent, 'function')
t.assert.strictEqual(typeof request, 'function')
t.assert.strictEqual(typeof stream, 'function')
t.assert.strictEqual(typeof pipeline, 'function')
t.assert.strictEqual(typeof connect, 'function')
t.assert.strictEqual(typeof upgrade, 'function')
t.assert.strictEqual(typeof setGlobalDispatcher, 'function')
t.assert.strictEqual(typeof getGlobalDispatcher, 'function')
})
Loading