You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.5 KiB

4 years ago
'use strict'
const t = require('tap')
const test = t.test
const sget = require('simple-get').concat
const fastify = require('..')()
const opts = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: {
type: 'string'
}
}
},
'2xx': {
type: 'object',
properties: {
hello: {
type: 'number'
}
}
}
}
}
}
test('shorthand - output string', t => {
t.plan(1)
try {
fastify.get('/string', opts, function (req, reply) {
reply.code(200).send({ hello: 'world' })
})
t.pass()
} catch (e) {
t.fail()
}
})
test('shorthand - output number', t => {
t.plan(1)
try {
fastify.get('/number', opts, function (req, reply) {
reply.code(201).send({ hello: 55 })
})
t.pass()
} catch (e) {
t.fail()
}
})
test('wrong object for schema - output', t => {
t.plan(1)
try {
fastify.get('/wrong-object-for-schema', opts, function (req, reply) {
// will send { }
reply.code(201).send({ hello: 'world' })
})
t.pass()
} catch (e) {
t.fail()
}
})
test('empty response', t => {
t.plan(1)
try {
// no checks
fastify.get('/empty', opts, function (req, reply) {
reply.code(204).send()
})
t.pass()
} catch (e) {
t.fail()
}
})
test('unlisted response code', t => {
t.plan(1)
try {
fastify.get('/400', opts, function (req, reply) {
reply.code(400).send({ hello: 'DOOM' })
})
t.pass()
} catch (e) {
t.fail()
}
})
fastify.listen(0, err => {
t.error(err)
fastify.server.unref()
test('shorthand - string get ok', t => {
t.plan(4)
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/string'
}, (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('shorthand - number get ok', t => {
t.plan(4)
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/number'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 201)
t.strictEqual(response.headers['content-length'], '' + body.length)
t.deepEqual(JSON.parse(body), { hello: 55 })
})
})
test('shorthand - wrong-object-for-schema', t => {
t.plan(4)
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/wrong-object-for-schema'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 201)
t.strictEqual(response.headers['content-length'], '' + body.length)
t.deepEqual(JSON.parse(body), {})
})
})
test('shorthand - empty', t => {
t.plan(2)
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/empty'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 204)
})
})
test('shorthand - 400', t => {
t.plan(4)
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/400'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 400)
t.strictEqual(response.headers['content-length'], '' + body.length)
t.deepEqual(JSON.parse(body), { hello: 'DOOM' })
})
})
})