UNPKG

fastify

Version:

Fast and low overhead web framework, for Node.js

345 lines (292 loc) 8.82 kB
'use strict' const t = require('tap') const test = t.test const Fastify = require('..') const fp = require('fastify-plugin') const request = require('request') test('server methods should exist', t => { t.plan(2) const fastify = Fastify() t.ok(fastify.decorate) t.ok(fastify.hasDecorator) }) test('server methods should be incapsulated via .register', t => { t.plan(2) const fastify = Fastify() fastify.register((instance, opts, next) => { instance.decorate('test', () => {}) t.ok(instance.test) next() }) fastify.ready(() => { t.notOk(fastify.test) }) }) test('hasServerMethod should check if the given method already exist', t => { t.plan(2) const fastify = Fastify() fastify.register((instance, opts, next) => { instance.decorate('test', () => {}) t.ok(instance.hasDecorator('test')) next() }) fastify.ready(() => { t.notOk(fastify.hasDecorator('test')) }) }) test('addServerMethod should throw if a declared dependency is not present', t => { t.plan(1) const fastify = Fastify() fastify.register((instance, opts, next) => { try { instance.decorate('test', () => {}, ['dependency']) t.fail() } catch (e) { t.is(e.message, 'Fastify decorator: missing dependency: \'dependency\'.') } }) }) test('decorateReply inside register', t => { t.plan(12) const fastify = Fastify() fastify.register((instance, opts, next) => { instance.decorateReply('test', 'test') t.ok(instance._Reply.prototype.test) instance.get('/yes', (req, reply) => { t.ok(reply.test, 'test exists') reply.send({ hello: 'world' }) }) next() }) fastify.get('/no', (req, reply) => { t.notOk(reply.test) reply.send({ hello: 'world' }) }) fastify.listen(0, err => { t.error(err) fastify.server.unref() request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/yes' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/no' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) }) }) test('decorateReply as plugin (inside .after)', t => { t.plan(11) const fastify = Fastify() fastify.register((instance, opts, next) => { instance.register(fp((i, o, n) => { instance.decorateReply('test', 'test') n() })).after(() => { instance.get('/yes', (req, reply) => { t.ok(reply.test) reply.send({ hello: 'world' }) }) }) next() }) fastify.get('/no', (req, reply) => { t.notOk(reply.test) reply.send({ hello: 'world' }) }) fastify.listen(0, err => { t.error(err) fastify.server.unref() request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/yes' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/no' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) }) }) test('decorateReply as plugin (outside .after)', t => { t.plan(11) const fastify = Fastify() fastify.register((instance, opts, next) => { instance.register(fp((i, o, n) => { instance.decorateReply('test', 'test') n() })) instance.get('/yes', (req, reply) => { t.ok(reply.test) reply.send({ hello: 'world' }) }) next() }) fastify.get('/no', (req, reply) => { t.notOk(reply.test) reply.send({ hello: 'world' }) }) fastify.listen(0, err => { t.error(err) fastify.server.unref() request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/yes' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/no' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) }) }) test('decorateRequest inside register', t => { t.plan(12) const fastify = Fastify() fastify.register((instance, opts, next) => { instance.decorateRequest('test', 'test') t.ok(instance._Request.prototype.test) instance.get('/yes', (req, reply) => { t.ok(req.test, 'test exists') reply.send({ hello: 'world' }) }) next() }) fastify.get('/no', (req, reply) => { t.notOk(req.test) reply.send({ hello: 'world' }) }) fastify.listen(0, err => { t.error(err) fastify.server.unref() request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/yes' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/no' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) }) }) test('decorateRequest as plugin (inside .after)', t => { t.plan(11) const fastify = Fastify() fastify.register((instance, opts, next) => { instance.register(fp((i, o, n) => { instance.decorateRequest('test', 'test') n() })).after(() => { instance.get('/yes', (req, reply) => { t.ok(req.test) reply.send({ hello: 'world' }) }) }) next() }) fastify.get('/no', (req, reply) => { t.notOk(req.test) reply.send({ hello: 'world' }) }) fastify.listen(0, err => { t.error(err) fastify.server.unref() request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/yes' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/no' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) }) }) test('decorateRequest as plugin (outside .after)', t => { t.plan(11) const fastify = Fastify() fastify.register((instance, opts, next) => { instance.register(fp((i, o, n) => { instance.decorateRequest('test', 'test') n() })) instance.get('/yes', (req, reply) => { t.ok(req.test) reply.send({ hello: 'world' }) }) next() }) fastify.get('/no', (req, reply) => { t.notOk(req.test) reply.send({ hello: 'world' }) }) fastify.listen(0, err => { t.error(err) fastify.server.unref() request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/yes' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) request({ method: 'GET', uri: 'http://localhost:' + fastify.server.address().port + '/no' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) }) })