const fastify = require('fastify') const he = require('he') const got = require('got') const assert = require('assert').strict function server() { const app = fastify() app.get('/q', (request, response) => { try { if (request.query.message) { response.send(request.query.message.toUpperCase()) return } } catch (error) { response.code(400).send('ERROR') } }) return app } async function test () { const app = server() await app.listen(0) app.server.unref() const address = app.server.address() const url = `http://${address.address}:${address.port}` const response = await got(url + '/q?message=ciao', { throwHttpErrors: false }) assert.equal(response.body, 'CIAO') assert.equal(response.headers['content-type'], 'text/plain; charset=utf-8') assert.equal(response.statusCode, 200) await app.close() } test()