strong-tie/inbound-calls
0
1'use strict'2 3const t = require('tap')4const test = t.test5const sget = require('simple-get').concat6const fastify = require('..')()7 8const opts = {9 schema: {10 response: {11 200: {12 type: 'object',13 properties: {14 hello: {15 type: 'string'16 }17 }18 },19 '2xx': {20 type: 'object',21 properties: {22 hello: {23 type: 'number'24 }25 }26 }27 }28 }29}30 31test('shorthand - output string', t => {32 t.plan(1)33 try {34 fastify.get('/string', opts, function (req, reply) {35 reply.code(200).send({ hello: 'world' })36 })37 t.pass()38 } catch (e) {39 t.fail()40 }41})42 43test('shorthand - output number', t => {44 t.plan(1)45 try {46 fastify.get('/number', opts, function (req, reply) {47 reply.code(201).send({ hello: 55 })48 })49 t.pass()50 } catch (e) {51 t.fail()52 }53})54 55test('wrong object for schema - output', t => {56 t.plan(1)57 try {58 fastify.get('/wrong-object-for-schema', opts, function (req, reply) {59 // will send { }60 reply.code(201).send({ hello: 'world' })61 })62 t.pass()63 } catch (e) {64 t.fail()65 }66})67 68test('empty response', t => {69 t.plan(1)70 try {71 // no checks72 fastify.get('/empty', opts, function (req, reply) {73 reply.code(204).send()74 })75 t.pass()76 } catch (e) {77 t.fail()78 }79})80 81test('unlisted response code', t => {82 t.plan(1)83 try {84 fastify.get('/400', opts, function (req, reply) {85 reply.code(400).send({ hello: 'DOOM' })86 })87 t.pass()88 } catch (e) {89 t.fail()90 }91})92 93fastify.listen({ port: 0 }, err => {94 t.error(err)95 t.teardown(() => { fastify.close() })96 97 test('shorthand - string get ok', t => {98 t.plan(4)99 sget({100 method: 'GET',101 url: 'http://localhost:' + fastify.server.address().port + '/string'102 }, (err, response, body) => {103 t.error(err)104 t.equal(response.statusCode, 200)105 t.equal(response.headers['content-length'], '' + body.length)106 t.same(JSON.parse(body), { hello: 'world' })107 })108 })109 110 test('shorthand - number get ok', t => {111 t.plan(4)112 sget({113 method: 'GET',114 url: 'http://localhost:' + fastify.server.address().port + '/number'115 }, (err, response, body) => {116 t.error(err)117 t.equal(response.statusCode, 201)118 t.equal(response.headers['content-length'], '' + body.length)119 t.same(JSON.parse(body), { hello: 55 })120 })121 })122 123 test('shorthand - wrong-object-for-schema', t => {124 t.plan(4)125 sget({126 method: 'GET',127 url: 'http://localhost:' + fastify.server.address().port + '/wrong-object-for-schema'128 }, (err, response, body) => {129 t.error(err)130 t.equal(response.statusCode, 500)131 t.equal(response.headers['content-length'], '' + body.length)132 t.same(JSON.parse(body), {133 statusCode: 500,134 error: 'Internal Server Error',135 message: 'The value "world" cannot be converted to a number.'136 })137 })138 })139 140 test('shorthand - empty', t => {141 t.plan(2)142 sget({143 method: 'GET',144 url: 'http://localhost:' + fastify.server.address().port + '/empty'145 }, (err, response, body) => {146 t.error(err)147 t.equal(response.statusCode, 204)148 })149 })150 151 test('shorthand - 400', t => {152 t.plan(4)153 sget({154 method: 'GET',155 url: 'http://localhost:' + fastify.server.address().port + '/400'156 }, (err, response, body) => {157 t.error(err)158 t.equal(response.statusCode, 400)159 t.equal(response.headers['content-length'], '' + body.length)160 t.same(JSON.parse(body), { hello: 'DOOM' })161 })162 })163})164 