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
108 changes: 40 additions & 68 deletions test/plugin.1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
const t = require('node:test')
const test = t.test
const Fastify = require('../fastify')
const sget = require('simple-get').concat
const fp = require('fastify-plugin')
const { waitForCb } = require('./toolkit')

test('require a plugin', (t, testDone) => {
t.plan(1)
Expand Down Expand Up @@ -59,8 +57,8 @@ test('plugin metadata - naming plugins', async t => {
await fastify.ready()
})

test('fastify.register with fastify-plugin should not encapsulate his code', (t, testDone) => {
t.plan(10)
test('fastify.register with fastify-plugin should not encapsulate his code', async t => {
t.plan(9)
const fastify = Fastify()

fastify.register((instance, opts, done) => {
Expand Down Expand Up @@ -89,25 +87,19 @@ test('fastify.register with fastify-plugin should not encapsulate his code', (t,
t.assert.ok(!fastify.test)
})

fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
t.after(() => { fastify.close() })

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
testDone()
})
})
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => { fastify.close() })

const result = await fetch(fastifyServer)
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 200)
const body = await result.text()
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
})

test('fastify.register with fastify-plugin should provide access to external fastify instance if opts argument is a function', (t, testDone) => {
t.plan(22)
test('fastify.register with fastify-plugin should provide access to external fastify instance if opts argument is a function', async t => {
t.plan(21)
const fastify = Fastify()

fastify.register((instance, opts, done) => {
Expand Down Expand Up @@ -169,25 +161,19 @@ test('fastify.register with fastify-plugin should provide access to external fas
t.assert.ok(!fastify.global)
})

fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
t.after(() => { fastify.close() })

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
testDone()
})
})
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => { fastify.close() })

const result = await fetch(fastifyServer)
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 200)
const body = await result.text()
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
})

test('fastify.register with fastify-plugin registers fastify level plugins', t => {
t.plan(15)
test('fastify.register with fastify-plugin registers fastify level plugins', async t => {
t.plan(14)
const fastify = Fastify()

function fastifyPlugin (instance, opts, done) {
Expand Down Expand Up @@ -225,34 +211,20 @@ test('fastify.register with fastify-plugin registers fastify level plugins', t =
reply.send({ test: fastify.test })
})

const { stepIn, patience } = waitForCb({ steps: 2 })

fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
t.after(() => { fastify.close() })

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { test: 'first' })
stepIn()
})

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/test2'
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { test2: 'second' })
stepIn()
})
})

return patience
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => { fastify.close() })

const result1 = await fetch(fastifyServer)
t.assert.ok(result1.ok)
t.assert.strictEqual(result1.status, 200)
const body1 = await result1.text()
t.assert.strictEqual(result1.headers.get('content-length'), '' + body1.length)
t.assert.deepStrictEqual(JSON.parse(body1), { test: 'first' })

const result2 = await fetch(fastifyServer + '/test2')
t.assert.ok(result2.ok)
t.assert.strictEqual(result2.status, 200)
const body2 = await result2.text()
t.assert.strictEqual(result2.headers.get('content-length'), '' + body2.length)
t.assert.deepStrictEqual(JSON.parse(body2), { test2: 'second' })
})
110 changes: 40 additions & 70 deletions test/plugin.2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

const { test } = require('node:test')
const Fastify = require('../fastify')
const sget = require('simple-get').concat
const fp = require('fastify-plugin')
const { waitForCb } = require('./toolkit')

test('check dependencies - should not throw', (t, testDone) => {
t.plan(12)
test('check dependencies - should not throw', async t => {
t.plan(11)
const fastify = Fastify()

fastify.register((instance, opts, done) => {
Expand Down Expand Up @@ -42,25 +40,19 @@ test('check dependencies - should not throw', (t, testDone) => {
t.assert.ok(!fastify.otherTest)
})

fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
t.after(() => { fastify.close() })

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
testDone()
})
})
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => { fastify.close() })

const result = await fetch(fastifyServer)
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 200)
const body = await result.text()
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
})

test('check dependencies - should throw', (t, testDone) => {
t.plan(12)
test('check dependencies - should throw', async t => {
t.plan(11)
const fastify = Fastify()

fastify.register((instance, opts, done) => {
Expand Down Expand Up @@ -95,21 +87,15 @@ test('check dependencies - should throw', (t, testDone) => {
t.assert.ok(!fastify.test)
})

fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
t.after(() => { fastify.close() })

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
testDone()
})
})
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => { fastify.close() })

const result = await fetch(fastifyServer)
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 200)
const body = await result.text()
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
})

test('set the plugin name based on the plugin displayName symbol', (t, testDone) => {
Expand Down Expand Up @@ -274,8 +260,8 @@ test('approximate a plugin name also when fastify-plugin has no meta data', (t,
})
})

test('plugin encapsulation', (t, testDone) => {
t.plan(10)
test('plugin encapsulation', async t => {
t.plan(9)
const fastify = Fastify()
t.after(() => fastify.close())

Expand Down Expand Up @@ -309,36 +295,20 @@ test('plugin encapsulation', (t, testDone) => {
t.assert.ok(!fastify.test)
})

fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
t.after(() => { fastify.close() })

const completion = waitForCb({
steps: 2
})

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/first'
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { plugin: 'first' })
completion.stepIn()
})

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/second'
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { plugin: 'second' })
completion.stepIn()
})

completion.patience.then(testDone)
})
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => { fastify.close() })

const result1 = await fetch(fastifyServer + '/first')
t.assert.ok(result1.ok)
t.assert.strictEqual(result1.status, 200)
const body1 = await result1.text()
t.assert.strictEqual(result1.headers.get('content-length'), '' + body1.length)
t.assert.deepStrictEqual(JSON.parse(body1), { plugin: 'first' })

const result2 = await fetch(fastifyServer + '/second')
t.assert.ok(result2.ok)
t.assert.strictEqual(result2.status, 200)
const body2 = await result2.text()
t.assert.strictEqual(result2.headers.get('content-length'), '' + body2.length)
t.assert.deepStrictEqual(JSON.parse(body2), { plugin: 'second' })
})
Loading