Projektstart
This commit is contained in:
163
backend/node_modules/fastify/test/output-validation.test.js
generated
vendored
Normal file
163
backend/node_modules/fastify/test/output-validation.test.js
generated
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
'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({ port: 0 }, err => {
|
||||
t.error(err)
|
||||
t.teardown(() => { fastify.close() })
|
||||
|
||||
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.equal(response.statusCode, 200)
|
||||
t.equal(response.headers['content-length'], '' + body.length)
|
||||
t.same(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.equal(response.statusCode, 201)
|
||||
t.equal(response.headers['content-length'], '' + body.length)
|
||||
t.same(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.equal(response.statusCode, 500)
|
||||
t.equal(response.headers['content-length'], '' + body.length)
|
||||
t.same(JSON.parse(body), {
|
||||
statusCode: 500,
|
||||
error: 'Internal Server Error',
|
||||
message: 'The value "world" cannot be converted to a number.'
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
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.equal(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.equal(response.statusCode, 400)
|
||||
t.equal(response.headers['content-length'], '' + body.length)
|
||||
t.same(JSON.parse(body), { hello: 'DOOM' })
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user