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

chore: migrate to node test runner #163

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 0 additions & 2 deletions .taprc

This file was deleted.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:unit": "c8 --100 node --test",
"test:typescript": "tsd"
},
"precommit": [
Expand Down Expand Up @@ -61,12 +61,11 @@
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"@types/node": "^22.0.0",
"c8": "^10.1.3",
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"h2url": "^0.2.0",
"neostandard": "^0.12.0",
"semver": "^7.5.4",
"tap": "^18.6.1",
"tsd": "~0.31.0"
},
"dependencies": {
Expand Down
190 changes: 82 additions & 108 deletions test/tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

const fs = require('node:fs')
const join = require('node:path').join
const http = require('node:http')
const test = require('tap').test
const { test } = require('node:test')
const Fastify = require('fastify')
const plugin = require('../')
const semver = require('semver')
const plugin = require('..')

const urlHost = 'localhost'
const urlForwardedHost = 'example.com'
Expand All @@ -15,173 +13,149 @@ const urlQuery = 'a=b&c=d'
const httpScheme = 'http'
const httpsScheme = 'https'

test('parses a full URI', (t) => {
t.plan(10)
let port
test('parses a full URI', async (t) => {
t.plan(11)
const fastify = Fastify()

fastify
.register(plugin)
.after((err) => {
if (err) t.error(err)
t.assert.ok(!err)
ilteoood marked this conversation as resolved.
Show resolved Hide resolved
})

fastify.get(urlPath, (req, reply) => {
const uriData = req.urlData()
t.equal(uriData.host, urlHost)
t.equal(uriData.port, port)
t.equal(uriData.path, urlPath)
t.equal(uriData.query, urlQuery)
t.equal(uriData.scheme, httpScheme)
t.equal(req.urlData('host'), urlHost)
t.equal(req.urlData('port'), port)
t.equal(req.urlData('path'), urlPath)
t.equal(req.urlData('query'), urlQuery)
t.equal(req.urlData('scheme'), httpScheme)
t.assert.deepStrictEqual(uriData.host, urlHost)
t.assert.deepStrictEqual(uriData.port, port)
t.assert.deepStrictEqual(uriData.path, urlPath)
t.assert.deepStrictEqual(uriData.query, urlQuery)
t.assert.deepStrictEqual(uriData.scheme, httpScheme)
t.assert.deepStrictEqual(req.urlData('host'), urlHost)
t.assert.deepStrictEqual(req.urlData('port'), port)
t.assert.deepStrictEqual(req.urlData('path'), urlPath)
t.assert.deepStrictEqual(req.urlData('query'), urlQuery)
t.assert.deepStrictEqual(req.urlData('scheme'), httpScheme)
reply.send()
})

fastify.listen({ port: 0 }, (err) => {
fastify.server.unref()
if (err) t.threw(err)
await fastify.listen({ port: 0 })
const port = fastify.server.address().port
fastify.server.unref()

port = fastify.server.address().port
http
.get(`http://${urlHost}:${port}${urlPath}?${urlQuery}#foo`, () => {})
.on('error', t.threw)
})
await fetch(`http://${urlHost}:${port}${urlPath}?${urlQuery}#foo`)

t.teardown(() => fastify.close())
t.after(() => fastify.close())
})

test('parses a full URI in HTTP2', { skip: semver.lt(process.versions.node, '8.8.0') }, (t) => {
test('parses a full URI in HTTP2', async (t) => {
t.plan(11)

const h2url = require('h2url')

let port
let fastify
try {
fastify = Fastify({
http2: true,
https: {
key: fs.readFileSync(join(__dirname, 'https', 'fastify.key')),
cert: fs.readFileSync(join(__dirname, 'https', 'fastify.cert'))
}
})
t.pass('Key/cert successfully loaded')
} catch (e) {
t.fail('Key/cert loading failed', e)
}
const fastify = Fastify({
http2: true,
https: {
key: fs.readFileSync(join(__dirname, 'https', 'fastify.key')),
cert: fs.readFileSync(join(__dirname, 'https', 'fastify.cert'))
}
})

fastify
.register(plugin)
.after((err) => {
if (err) t.error(err)
t.assert.ok(!err)
ilteoood marked this conversation as resolved.
Show resolved Hide resolved
})

fastify.get(urlPath, (req, reply) => {
const uriData = req.urlData()
t.equal(uriData.host, urlHost)
t.equal(uriData.port, port)
t.equal(uriData.path, urlPath)
t.equal(uriData.query, urlQuery)
t.equal(uriData.scheme, httpsScheme)
t.equal(req.urlData('host'), urlHost)
t.equal(req.urlData('port'), port)
t.equal(req.urlData('path'), urlPath)
t.equal(req.urlData('query'), urlQuery)
t.equal(req.urlData('scheme'), httpsScheme)
t.assert.deepStrictEqual(uriData.host, urlHost)
t.assert.deepStrictEqual(uriData.port, port)
t.assert.deepStrictEqual(uriData.path, urlPath)
t.assert.deepStrictEqual(uriData.query, urlQuery)
t.assert.deepStrictEqual(uriData.scheme, httpsScheme)
t.assert.deepStrictEqual(req.urlData('host'), urlHost)
t.assert.deepStrictEqual(req.urlData('port'), port)
t.assert.deepStrictEqual(req.urlData('path'), urlPath)
t.assert.deepStrictEqual(req.urlData('query'), urlQuery)
t.assert.deepStrictEqual(req.urlData('scheme'), httpsScheme)
reply.send()
})

fastify.listen({ port: 0 }, (err) => {
fastify.server.unref()
if (err) t.threw(err)
await fastify.listen({ port: 0 })
const port = fastify.server.address().port
fastify.server.unref()

port = fastify.server.address().port
h2url.concat({ url: `https://${urlHost}:${port}${urlPath}?${urlQuery}#foo` }).then(() => {})
})
await h2url.concat({ url: `https://${urlHost}:${port}${urlPath}?${urlQuery}#foo` })

t.teardown(() => fastify.close())
t.after(() => fastify.close())
})

test('parses a full URI using X-Forwarded-Host when trustProxy is set', (t) => {
t.plan(10)
let port
test('parses a full URI using X-Forwarded-Host when trustProxy is set', async (t) => {
t.plan(11)
const fastify = Fastify({ trustProxy: true }) // Setting trustProxy true will use X-Forwarded-Host header if set

fastify
.register(plugin)
.after((err) => {
if (err) t.error(err)
t.assert.ok(!err)
ilteoood marked this conversation as resolved.
Show resolved Hide resolved
})

fastify.get(urlPath, (req, reply) => {
const uriData = req.urlData()
t.equal(uriData.host, urlForwardedHost)
t.equal(uriData.port, port)
t.equal(uriData.path, urlPath)
t.equal(uriData.query, urlQuery)
t.equal(uriData.scheme, httpScheme)
t.equal(req.urlData('host'), urlForwardedHost)
t.equal(req.urlData('port'), port)
t.equal(req.urlData('path'), urlPath)
t.equal(req.urlData('query'), urlQuery)
t.equal(req.urlData('scheme'), httpScheme)
t.assert.deepStrictEqual(uriData.host, urlForwardedHost)
t.assert.deepStrictEqual(uriData.port, port)
t.assert.deepStrictEqual(uriData.path, urlPath)
t.assert.deepStrictEqual(uriData.query, urlQuery)
t.assert.deepStrictEqual(uriData.scheme, httpScheme)
t.assert.deepStrictEqual(req.urlData('host'), urlForwardedHost)
t.assert.deepStrictEqual(req.urlData('port'), port)
t.assert.deepStrictEqual(req.urlData('path'), urlPath)
t.assert.deepStrictEqual(req.urlData('query'), urlQuery)
t.assert.deepStrictEqual(req.urlData('scheme'), httpScheme)
reply.send()
})

fastify.listen({ port: 0 }, (err) => {
fastify.server.unref()
if (err) t.threw(err)
await fastify.listen({ port: 0 })
const port = fastify.server.address().port
fastify.server.unref()

port = fastify.server.address().port
http
.get(`http://${urlHost}:${port}${urlPath}?${urlQuery}#foo`, { headers: { 'X-Forwarded-Host': `${urlForwardedHost}:${port}` } }, () => {})
.on('error', t.threw)
})
await fetch(`http://${urlHost}:${fastify.server.address().port}${urlPath}?${urlQuery}#foo`, { headers: { 'X-Forwarded-Host': `${urlForwardedHost}:${port}` } })

t.teardown(() => fastify.close())
t.after(() => fastify.close())
})

test('parses a full URI ignoring X-Forwarded-Host when trustProxy is not set', (t) => {
t.plan(10)
let port
test('parses a full URI ignoring X-Forwarded-Host when trustProxy is not set', async (t) => {
t.plan(11)
const fastify = Fastify()

fastify
.register(plugin)
.after((err) => {
if (err) t.error(err)
t.assert.ok(!err)
ilteoood marked this conversation as resolved.
Show resolved Hide resolved
})

fastify.get(urlPath, (req, reply) => {
const uriData = req.urlData()
t.equal(uriData.host, urlHost)
t.equal(uriData.port, port)
t.equal(uriData.path, urlPath)
t.equal(uriData.query, urlQuery)
t.equal(uriData.scheme, httpScheme)
t.equal(req.urlData('host'), urlHost)
t.equal(req.urlData('port'), port)
t.equal(req.urlData('path'), urlPath)
t.equal(req.urlData('query'), urlQuery)
t.equal(req.urlData('scheme'), httpScheme)
t.assert.deepStrictEqual(uriData.host, urlHost)
t.assert.deepStrictEqual(uriData.port, port)
t.assert.deepStrictEqual(uriData.path, urlPath)
t.assert.deepStrictEqual(uriData.query, urlQuery)
t.assert.deepStrictEqual(uriData.scheme, httpScheme)
t.assert.deepStrictEqual(req.urlData('host'), urlHost)
t.assert.deepStrictEqual(req.urlData('port'), port)
t.assert.deepStrictEqual(req.urlData('path'), urlPath)
t.assert.deepStrictEqual(req.urlData('query'), urlQuery)
t.assert.deepStrictEqual(req.urlData('scheme'), httpScheme)
reply.send()
})

fastify.listen({ port: 0 }, (err) => {
fastify.server.unref()
if (err) t.threw(err)
await fastify.listen({ port: 0 })
const port = fastify.server.address().port
fastify.server.unref()

port = fastify.server.address().port
http
.get(`http://${urlHost}:${port}${urlPath}?${urlQuery}#foo`, { headers: { 'X-Forwarded-Host': `${urlForwardedHost}:${port}` } }, () => {})
.on('error', t.threw)
})
await fetch(`http://${urlHost}:${fastify.server.address().port}${urlPath}?${urlQuery}#foo`, { headers: { 'X-Forwarded-Host': `${urlForwardedHost}:${port}` } })

t.teardown(() => fastify.close())
t.after(() => fastify.close())
})

test('should parse path without a port specified', async (t) => {
Expand All @@ -196,6 +170,6 @@ test('should parse path without a port specified', async (t) => {
})

const res = await fastify.inject({ url: '/', headers: { host: 'localhost' } })
t.equal(res.statusCode, 200)
t.equal(res.body, 'That worked, path is /')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.body, 'That worked, path is /')
})
Loading