Skip to content

Commit

Permalink
Update fastify 4 (#86)
Browse files Browse the repository at this point in the history
* Use fastify v4

Signed-off-by: Matteo Collina <[email protected]>

* fix

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored May 24, 2022
1 parent f3b000e commit 9b352a1
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 137 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ function expressPlugin (fastify, options, next) {
}

module.exports = fp(expressPlugin, {
fastify: '>=3.0.0',
fastify: '4.x',
name: '@fastify/express'
})
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"@types/node": "*",
"body-parser": "^1.20.0",
"cors": "^2.8.5",
"fastify": "^3.0.0",
"fastify-formbody": "^5.3.0",
"fastify": "^4.0.0-rc.3",
"@fastify/formbody": "^7.0.1",
"helmet": "^5.0.0",
"passport": "^0.6.0",
"passport-http-bearer": "^1.0.1",
Expand All @@ -44,9 +44,6 @@
"express": "^4.17.1",
"fastify-plugin": "^3.0.0"
},
"engines": {
"node": ">=10.0.0"
},
"tsd": {
"directory": "test/types"
},
Expand Down
34 changes: 17 additions & 17 deletions test/404s.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ test('run hooks and middleware on default 404', t => {
reply.send({ hello: 'world' })
})

t.tearDown(fastify.close.bind(fastify))
t.teardown(fastify.close.bind(fastify))

fastify.listen(0, err => {
fastify.listen({ port: 0 }, err => {
t.error(err)

sget({
Expand All @@ -56,7 +56,7 @@ test('run hooks and middleware on default 404', t => {
headers: { 'Content-Type': 'application/json' }
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
t.equal(response.statusCode, 404)
})
})
})
Expand Down Expand Up @@ -101,7 +101,7 @@ test('run non-encapsulated plugin hooks and middleware on default 404', t => {
reply.send({ hello: 'world' })
})

fastify.listen(0, (err, address) => {
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
sget({
method: 'POST',
Expand All @@ -110,7 +110,7 @@ test('run non-encapsulated plugin hooks and middleware on default 404', t => {
headers: { 'Content-Type': 'application/json' }
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
t.equal(response.statusCode, 404)
})
})
})
Expand Down Expand Up @@ -163,15 +163,15 @@ test('run non-encapsulated plugin hooks and middleware on custom 404', t => {

fastify.register(plugin) // Registering plugin after handler also works

fastify.listen(0, (err, address) => {
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
sget({
method: 'GET',
url: address + '/not-found'
}, (err, response, body) => {
t.error(err)
t.strictEqual(body.toString(), 'this was not found')
t.strictEqual(response.statusCode, 404)
t.equal(body.toString(), 'this was not found')
t.equal(response.statusCode, 404)
})
})
})
Expand Down Expand Up @@ -243,9 +243,9 @@ test('run hooks and middleware with encapsulated 404', t => {
next()
}, { prefix: '/test' })

t.tearDown(fastify.close.bind(fastify))
t.teardown(fastify.close.bind(fastify))

fastify.listen(0, err => {
fastify.listen({ port: 0 }, err => {
t.error(err)

sget({
Expand All @@ -255,7 +255,7 @@ test('run hooks and middleware with encapsulated 404', t => {
headers: { 'Content-Type': 'application/json' }
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
t.equal(response.statusCode, 404)
})
})
})
Expand All @@ -277,9 +277,9 @@ test('run middlewares on default 404', t => {
reply.send({ hello: 'world' })
})

t.tearDown(fastify.close.bind(fastify))
t.teardown(fastify.close.bind(fastify))

fastify.listen(0, err => {
fastify.listen({ port: 0 }, err => {
t.error(err)

sget({
Expand All @@ -289,7 +289,7 @@ test('run middlewares on default 404', t => {
headers: { 'Content-Type': 'application/json' }
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
t.equal(response.statusCode, 404)
})
})
})
Expand Down Expand Up @@ -320,9 +320,9 @@ test('run middlewares with encapsulated 404', t => {
next()
}, { prefix: '/test' })

t.tearDown(fastify.close.bind(fastify))
t.teardown(fastify.close.bind(fastify))

fastify.listen(0, err => {
fastify.listen({ port: 0 }, err => {
t.error(err)

sget({
Expand All @@ -332,7 +332,7 @@ test('run middlewares with encapsulated 404', t => {
headers: { 'Content-Type': 'application/json' }
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 404)
t.equal(response.statusCode, 404)
})
})
})
28 changes: 14 additions & 14 deletions test/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ test('Register express application', t => {
fastify.register(expressPlugin)
.after(() => { fastify.use(express) })

fastify.listen(0, (err, address) => {
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
sget({
method: 'GET',
url: address + '/hello'
}, (err, res, data) => {
t.error(err)
t.strictEqual(res.statusCode, 201)
t.equal(res.statusCode, 201)
t.match(res.headers, { 'x-custom': 'true' })
t.deepEqual(JSON.parse(data), { hello: 'world' })
t.same(JSON.parse(data), { hello: 'world' })
})
})
})
Expand Down Expand Up @@ -65,25 +65,25 @@ test('Register express application that uses Router', t => {
fastify.register(expressPlugin)
.after(() => { fastify.use(router) })

fastify.listen(0, (err, address) => {
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
sget({
method: 'GET',
url: address + '/hello'
}, (err, res, data) => {
t.error(err)
t.strictEqual(res.statusCode, 201)
t.equal(res.statusCode, 201)
t.match(res.headers, { 'x-custom': 'true' })
t.deepEqual(JSON.parse(data), { hello: 'world' })
t.same(JSON.parse(data), { hello: 'world' })
})
sget({
method: 'GET',
url: address + '/foo'
}, (err, res, data) => {
t.error(err)
t.strictEqual(res.statusCode, 400)
t.equal(res.statusCode, 400)
t.match(res.headers, { 'x-custom': 'true' })
t.deepEqual(JSON.parse(data), { foo: 'bar' })
t.same(JSON.parse(data), { foo: 'bar' })
})
})
})
Expand All @@ -104,14 +104,14 @@ test('Should remove x-powered-by header', t => {
.register(expressPlugin)
.after(() => { fastify.use(router) })

fastify.listen(0, (err, address) => {
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
sget({
method: 'GET',
url: address
}, (err, res) => {
t.error(err)
t.strictEqual(res.headers['x-powered-by'], undefined)
t.equal(res.headers['x-powered-by'], undefined)
})
})
})
Expand All @@ -132,14 +132,14 @@ test('Should expose the express app on the fastify instance', t => {
.register(expressPlugin)
.after(() => { fastify.use(router) })

fastify.listen(0, (err, address) => {
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
sget({
method: 'GET',
url: address
}, (err, res) => {
t.error(err)
t.strictEqual(fastify.express.disabled('x-powered-by'), true)
t.equal(fastify.express.disabled('x-powered-by'), true)
})
})
})
Expand All @@ -166,14 +166,14 @@ test('Should flush headers if express handles request', t => {
.register(expressPlugin)
.after(() => { fastify.use(router) })

fastify.listen(0, (err, address) => {
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
sget({
method: 'GET',
url: address
}, (err, res) => {
t.error(err)
t.strictEqual(res.headers.foo, 'bar')
t.equal(res.headers.foo, 'bar')
})
})
})
Loading

0 comments on commit 9b352a1

Please sign in to comment.