Aktueller Stand
This commit is contained in:
437
backend/node_modules/fastify/test/schema-serialization.test.js
generated
vendored
437
backend/node_modules/fastify/test/schema-serialization.test.js
generated
vendored
@@ -1,12 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
const t = require('tap')
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('..')
|
||||
const test = t.test
|
||||
const { waitForCb } = require('./toolkit')
|
||||
|
||||
const echoBody = (req, reply) => { reply.send(req.body) }
|
||||
|
||||
test('basic test', t => {
|
||||
test('basic test', (t, testDone) => {
|
||||
t.plan(3)
|
||||
|
||||
const fastify = Fastify()
|
||||
@@ -27,13 +27,14 @@ test('basic test', t => {
|
||||
})
|
||||
|
||||
fastify.inject('/', (err, res) => {
|
||||
t.error(err)
|
||||
t.same(res.json(), { name: 'Foo', work: 'Bar' })
|
||||
t.equal(res.statusCode, 200)
|
||||
t.assert.ifError(err)
|
||||
t.assert.deepStrictEqual(res.json(), { name: 'Foo', work: 'Bar' })
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('custom serializer options', t => {
|
||||
test('custom serializer options', (t, testDone) => {
|
||||
t.plan(3)
|
||||
|
||||
const fastify = Fastify({
|
||||
@@ -54,14 +55,15 @@ test('custom serializer options', t => {
|
||||
})
|
||||
|
||||
fastify.inject('/', (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, '5', 'it must use the ceil rounding')
|
||||
t.equal(res.statusCode, 200)
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, '5', 'it must use the ceil rounding')
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('Different content types', t => {
|
||||
t.plan(32)
|
||||
test('Different content types', (t, testDone) => {
|
||||
t.plan(46)
|
||||
|
||||
const fastify = Fastify()
|
||||
fastify.addSchema({
|
||||
@@ -81,9 +83,12 @@ test('Different content types', t => {
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
name: { type: 'string' },
|
||||
image: { type: 'string' },
|
||||
address: { type: 'string' }
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
image: { type: 'string' },
|
||||
address: { type: 'string' }
|
||||
}
|
||||
}
|
||||
},
|
||||
'application/vnd.v1+json': {
|
||||
@@ -95,17 +100,40 @@ test('Different content types', t => {
|
||||
}
|
||||
},
|
||||
201: {
|
||||
content: { type: 'string' }
|
||||
content: {
|
||||
'*/*': {
|
||||
schema: { type: 'string' }
|
||||
}
|
||||
}
|
||||
},
|
||||
202: {
|
||||
content: { const: 'Processing exclusive content' }
|
||||
content: {
|
||||
'*/*': {
|
||||
schema: { const: 'Processing exclusive content' }
|
||||
}
|
||||
}
|
||||
},
|
||||
'3xx': {
|
||||
content: {
|
||||
'application/vnd.v2+json': {
|
||||
schema: {
|
||||
fullName: { type: 'string' },
|
||||
phone: { type: 'string' }
|
||||
type: 'object',
|
||||
properties: {
|
||||
fullName: { type: 'string' },
|
||||
phone: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
'4xx': {
|
||||
content: {
|
||||
'*/*': {
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
details: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,7 +142,19 @@ test('Different content types', t => {
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
details: { type: 'string' }
|
||||
type: 'object',
|
||||
properties: {
|
||||
details: { type: 'string' }
|
||||
}
|
||||
}
|
||||
},
|
||||
'*/*': {
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
desc: { type: 'string' },
|
||||
details: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -160,6 +200,15 @@ test('Different content types', t => {
|
||||
reply.code(400)
|
||||
reply.send({ details: 'validation error' })
|
||||
break
|
||||
case 'application/vnd.v8+json':
|
||||
reply.header('Content-Type', 'application/vnd.v8+json')
|
||||
reply.code(500)
|
||||
reply.send({ desc: 'age is missing', details: 'validation error' })
|
||||
break
|
||||
case 'application/vnd.v9+json':
|
||||
reply.code(500)
|
||||
reply.send({ details: 'validation error' })
|
||||
break
|
||||
default:
|
||||
// to test if schema not found
|
||||
reply.header('Content-Type', 'application/vnd.v3+json')
|
||||
@@ -170,7 +219,7 @@ test('Different content types', t => {
|
||||
|
||||
fastify.get('/test', {
|
||||
serializerCompiler: ({ contentType }) => {
|
||||
t.equal(contentType, 'application/json')
|
||||
t.assert.strictEqual(contentType, 'application/json')
|
||||
return data => JSON.stringify(data)
|
||||
},
|
||||
schema: {
|
||||
@@ -179,9 +228,24 @@ test('Different content types', t => {
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
name: { type: 'string' },
|
||||
image: { type: 'string' },
|
||||
address: { type: 'string' }
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
image: { type: 'string' },
|
||||
address: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
default: {
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
details: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -189,72 +253,115 @@ test('Different content types', t => {
|
||||
}
|
||||
}
|
||||
}, function (req, reply) {
|
||||
reply.header('Content-Type', 'application/json')
|
||||
reply.send({ age: 18, city: 'AU' })
|
||||
switch (req.headers['code']) {
|
||||
case '200': {
|
||||
reply.header('Content-Type', 'application/json')
|
||||
reply.code(200).send({ age: 18, city: 'AU' })
|
||||
break
|
||||
}
|
||||
case '201': {
|
||||
reply.header('Content-Type', 'application/json')
|
||||
reply.code(201).send({ details: 'validation error' })
|
||||
break
|
||||
}
|
||||
default: {
|
||||
reply.header('Content-Type', 'application/vnd.v1+json')
|
||||
reply.code(201).send({ created: true })
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const completion = waitForCb({ steps: 14 })
|
||||
fastify.inject({ method: 'GET', url: '/', headers: { Accept: 'application/json' } }, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify({ name: 'Foo', image: 'profile picture', address: 'New Node' }))
|
||||
t.equal(res.statusCode, 200)
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ name: 'Foo', image: 'profile picture', address: 'New Node' }))
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
fastify.inject({ method: 'GET', url: '/', headers: { Accept: 'application/vnd.v1+json' } }, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify([{ name: 'Boo', age: 18, verified: false }, { name: 'Woo', age: 30, verified: true }]))
|
||||
t.equal(res.statusCode, 200)
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify([{ name: 'Boo', age: 18, verified: false }, { name: 'Woo', age: 30, verified: true }]))
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
fastify.inject({ method: 'GET', url: '/' }, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify([{ type: 'student', grade: 6 }, { type: 'student', grade: 9 }]))
|
||||
t.equal(res.statusCode, 200)
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify([{ type: 'student', grade: 6 }, { type: 'student', grade: 9 }]))
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
fastify.inject({ method: 'GET', url: '/', headers: { Accept: 'application/vnd.v2+json' } }, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify({ fullName: 'Jhon Smith', phone: '01090000000' }))
|
||||
t.equal(res.statusCode, 300)
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ fullName: 'Jhon Smith', phone: '01090000000' }))
|
||||
t.assert.strictEqual(res.statusCode, 300)
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
fastify.inject({ method: 'GET', url: '/', headers: { Accept: 'application/vnd.v3+json' } }, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify({ firstName: 'New', lastName: 'Hoo', country: 'eg', city: 'node' }))
|
||||
t.equal(res.statusCode, 300)
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ firstName: 'New', lastName: 'Hoo', country: 'eg', city: 'node' }))
|
||||
t.assert.strictEqual(res.statusCode, 300)
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
fastify.inject({ method: 'GET', url: '/', headers: { Accept: 'application/vnd.v4+json' } }, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify({ content: 'Games' }))
|
||||
t.equal(res.statusCode, 201)
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, '"[object Object]"')
|
||||
t.assert.strictEqual(res.statusCode, 201)
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
fastify.inject({ method: 'GET', url: '/', headers: { Accept: 'application/vnd.v5+json' } }, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify({ content: 'Processing exclusive content' }))
|
||||
t.equal(res.statusCode, 202)
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, '"Processing exclusive content"')
|
||||
t.assert.strictEqual(res.statusCode, 202)
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
fastify.inject({ method: 'GET', url: '/', headers: { Accept: 'application/vnd.v6+json' } }, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify({ desc: 'age is missing', details: 'validation error' }))
|
||||
t.equal(res.statusCode, 400)
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ details: 'validation error' }))
|
||||
t.assert.strictEqual(res.statusCode, 400)
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
fastify.inject({ method: 'GET', url: '/', headers: { Accept: 'application/vnd.v7+json' } }, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify({ details: 'validation error' }))
|
||||
t.equal(res.statusCode, 400)
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ details: 'validation error' }))
|
||||
t.assert.strictEqual(res.statusCode, 400)
|
||||
completion.stepIn()
|
||||
})
|
||||
fastify.inject({ method: 'GET', url: '/', headers: { Accept: 'application/vnd.v8+json' } }, (err, res) => {
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ desc: 'age is missing', details: 'validation error' }))
|
||||
t.assert.strictEqual(res.statusCode, 500)
|
||||
completion.stepIn()
|
||||
})
|
||||
fastify.inject({ method: 'GET', url: '/', headers: { Accept: 'application/vnd.v9+json' } }, (err, res) => {
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ details: 'validation error' }))
|
||||
t.assert.strictEqual(res.statusCode, 500)
|
||||
completion.stepIn()
|
||||
})
|
||||
fastify.inject({ method: 'GET', url: '/test', headers: { Code: '200' } }, (err, res) => {
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ age: 18, city: 'AU' }))
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
completion.stepIn()
|
||||
})
|
||||
fastify.inject({ method: 'GET', url: '/test', headers: { Code: '201' } }, (err, res) => {
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ details: 'validation error' }))
|
||||
t.assert.strictEqual(res.statusCode, 201)
|
||||
completion.stepIn()
|
||||
})
|
||||
fastify.inject({ method: 'GET', url: '/test', headers: { Accept: 'application/vnd.v1+json' } }, (err, res) => {
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ created: true }))
|
||||
t.assert.strictEqual(res.statusCode, 201)
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
fastify.inject({ method: 'GET', url: '/test' }, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify({ age: 18, city: 'AU' }))
|
||||
t.equal(res.statusCode, 200)
|
||||
})
|
||||
completion.patience.then(testDone)
|
||||
})
|
||||
|
||||
test('Invalid multiple content schema, throw FST_ERR_SCH_CONTENT_MISSING_SCHEMA error', t => {
|
||||
test('Invalid multiple content schema, throw FST_ERR_SCH_CONTENT_MISSING_SCHEMA error', (t, testDone) => {
|
||||
t.plan(3)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -265,8 +372,11 @@ test('Invalid multiple content schema, throw FST_ERR_SCH_CONTENT_MISSING_SCHEMA
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
fullName: { type: 'string' },
|
||||
phone: { type: 'string' }
|
||||
type: 'object',
|
||||
properties: {
|
||||
fullName: { type: 'string' },
|
||||
phone: { type: 'string' }
|
||||
}
|
||||
},
|
||||
example: {
|
||||
fullName: 'John Doe',
|
||||
@@ -284,13 +394,14 @@ test('Invalid multiple content schema, throw FST_ERR_SCH_CONTENT_MISSING_SCHEMA
|
||||
})
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.equal(err.message, "Schema is missing for the content type 'type'")
|
||||
t.equal(err.statusCode, 500)
|
||||
t.equal(err.code, 'FST_ERR_SCH_CONTENT_MISSING_SCHEMA')
|
||||
t.assert.strictEqual(err.message, "Schema is missing for the content type 'type'")
|
||||
t.assert.strictEqual(err.statusCode, 500)
|
||||
t.assert.strictEqual(err.code, 'FST_ERR_SCH_CONTENT_MISSING_SCHEMA')
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('Use the same schema id in different places', t => {
|
||||
test('Use the same schema id in different places', (t, testDone) => {
|
||||
t.plan(2)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -320,12 +431,13 @@ test('Use the same schema id in different places', t => {
|
||||
method: 'GET',
|
||||
url: '/123'
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
t.same(res.json(), [{ id: 1 }, { id: 2 }, { }])
|
||||
t.assert.ifError(err)
|
||||
t.assert.deepStrictEqual(res.json(), [{ id: 1 }, { id: 2 }, {}])
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('Use shared schema and $ref with $id in response ($ref to $id)', t => {
|
||||
test('Use shared schema and $ref with $id in response ($ref to $id)', (t, testDone) => {
|
||||
t.plan(5)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -375,32 +487,36 @@ test('Use shared schema and $ref with $id in response ($ref to $id)', t => {
|
||||
test: { id: Date.now() }
|
||||
}
|
||||
|
||||
const completion = waitForCb({ steps: 2 })
|
||||
fastify.inject({
|
||||
method: 'POST',
|
||||
url: '/',
|
||||
payload
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
t.same(res.json(), payload)
|
||||
t.assert.ifError(err)
|
||||
t.assert.deepStrictEqual(res.json(), payload)
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
fastify.inject({
|
||||
method: 'POST',
|
||||
url: '/',
|
||||
payload: { test: { id: Date.now() } }
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.statusCode, 400)
|
||||
t.same(res.json(), {
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.statusCode, 400)
|
||||
t.assert.deepStrictEqual(res.json(), {
|
||||
error: 'Bad Request',
|
||||
message: "body must have required property 'address'",
|
||||
statusCode: 400,
|
||||
code: 'FST_ERR_VALIDATION'
|
||||
})
|
||||
completion.stepIn()
|
||||
})
|
||||
|
||||
completion.patience.then(testDone)
|
||||
})
|
||||
|
||||
test('Shared schema should be pass to serializer and validator ($ref to shared schema /definitions)', t => {
|
||||
test('Shared schema should be pass to serializer and validator ($ref to shared schema /definitions)', (t, testDone) => {
|
||||
t.plan(5)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -488,8 +604,8 @@ test('Shared schema should be pass to serializer and validator ($ref to shared s
|
||||
url: '/',
|
||||
payload: locations
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
t.same(res.json(), locations)
|
||||
t.assert.ifError(err)
|
||||
t.assert.deepStrictEqual(res.json(), locations)
|
||||
|
||||
fastify.inject({
|
||||
method: 'POST',
|
||||
@@ -499,19 +615,20 @@ test('Shared schema should be pass to serializer and validator ($ref to shared s
|
||||
return _
|
||||
})
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.statusCode, 400)
|
||||
t.same(res.json(), {
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.statusCode, 400)
|
||||
t.assert.deepStrictEqual(res.json(), {
|
||||
error: 'Bad Request',
|
||||
message: 'body/0/location/email must match format "email"',
|
||||
statusCode: 400,
|
||||
code: 'FST_ERR_VALIDATION'
|
||||
})
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Custom setSerializerCompiler', t => {
|
||||
test('Custom setSerializerCompiler', (t, testDone) => {
|
||||
t.plan(7)
|
||||
const fastify = Fastify({ exposeHeadRoutes: false })
|
||||
|
||||
@@ -522,10 +639,10 @@ test('Custom setSerializerCompiler', t => {
|
||||
}
|
||||
|
||||
fastify.setSerializerCompiler(({ schema, method, url, httpStatus }) => {
|
||||
t.equal(method, 'GET')
|
||||
t.equal(url, '/foo/:id')
|
||||
t.equal(httpStatus, '200')
|
||||
t.same(schema, outSchema)
|
||||
t.assert.strictEqual(method, 'GET')
|
||||
t.assert.strictEqual(url, '/foo/:id')
|
||||
t.assert.strictEqual(httpStatus, '200')
|
||||
t.assert.deepStrictEqual(schema, outSchema)
|
||||
return data => JSON.stringify(data)
|
||||
})
|
||||
|
||||
@@ -540,7 +657,7 @@ test('Custom setSerializerCompiler', t => {
|
||||
}
|
||||
}
|
||||
})
|
||||
t.ok(instance.serializerCompiler, 'the serializer is set by the parent')
|
||||
t.assert.ok(instance.serializerCompiler, 'the serializer is set by the parent')
|
||||
done()
|
||||
}, { prefix: '/foo' })
|
||||
|
||||
@@ -548,12 +665,13 @@ test('Custom setSerializerCompiler', t => {
|
||||
method: 'GET',
|
||||
url: '/foo/123'
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify({ id: 1 }))
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ id: 1 }))
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('Custom setSerializerCompiler returns bad serialized output', t => {
|
||||
test('Custom setSerializerCompiler returns bad serialized output', (t, testDone) => {
|
||||
t.plan(4)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -565,7 +683,7 @@ test('Custom setSerializerCompiler returns bad serialized output', t => {
|
||||
|
||||
fastify.setSerializerCompiler(({ schema, method, url, httpStatus }) => {
|
||||
return data => {
|
||||
t.pass('returning an invalid serialization')
|
||||
t.assert.ok('returning an invalid serialization')
|
||||
return { not: 'a string' }
|
||||
}
|
||||
})
|
||||
@@ -583,17 +701,18 @@ test('Custom setSerializerCompiler returns bad serialized output', t => {
|
||||
method: 'GET',
|
||||
url: '/123'
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.statusCode, 500)
|
||||
t.strictSame(res.json(), {
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.statusCode, 500)
|
||||
t.assert.deepStrictEqual(res.json(), {
|
||||
code: 'FST_ERR_REP_INVALID_PAYLOAD_TYPE',
|
||||
message: 'Attempted to send payload of invalid type \'object\'. Expected a string or Buffer.',
|
||||
statusCode: 500
|
||||
})
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('Custom setSerializerCompiler with addSchema', t => {
|
||||
test('Custom setSerializerCompiler with addSchema', (t, testDone) => {
|
||||
t.plan(6)
|
||||
const fastify = Fastify({ exposeHeadRoutes: false })
|
||||
|
||||
@@ -604,10 +723,10 @@ test('Custom setSerializerCompiler with addSchema', t => {
|
||||
}
|
||||
|
||||
fastify.setSerializerCompiler(({ schema, method, url, httpStatus }) => {
|
||||
t.equal(method, 'GET')
|
||||
t.equal(url, '/foo/:id')
|
||||
t.equal(httpStatus, '200')
|
||||
t.same(schema, outSchema)
|
||||
t.assert.strictEqual(method, 'GET')
|
||||
t.assert.strictEqual(url, '/foo/:id')
|
||||
t.assert.strictEqual(httpStatus, '200')
|
||||
t.assert.deepStrictEqual(schema, outSchema)
|
||||
return _data => JSON.stringify({ id: 2 })
|
||||
})
|
||||
|
||||
@@ -629,8 +748,9 @@ test('Custom setSerializerCompiler with addSchema', t => {
|
||||
method: 'GET',
|
||||
url: '/foo/123'
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
t.equal(res.payload, JSON.stringify({ id: 2 }))
|
||||
t.assert.ifError(err)
|
||||
t.assert.strictEqual(res.payload, JSON.stringify({ id: 2 }))
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -673,23 +793,23 @@ test('Custom serializer per route', async t => {
|
||||
})
|
||||
|
||||
let res = await fastify.inject('/default')
|
||||
t.equal(res.json().mean, 'default')
|
||||
t.assert.strictEqual(res.json().mean, 'default')
|
||||
|
||||
res = await fastify.inject('/custom')
|
||||
t.equal(res.json().mean, 'custom')
|
||||
t.assert.strictEqual(res.json().mean, 'custom')
|
||||
|
||||
res = await fastify.inject('/route')
|
||||
t.equal(res.json().mean, 'route')
|
||||
t.assert.strictEqual(res.json().mean, 'route')
|
||||
|
||||
t.equal(hit, 4, 'the custom and route serializer has been called')
|
||||
t.assert.strictEqual(hit, 4, 'the custom and route serializer has been called')
|
||||
})
|
||||
|
||||
test('Reply serializer win over serializer ', t => {
|
||||
test('Reply serializer win over serializer ', (t, testDone) => {
|
||||
t.plan(6)
|
||||
|
||||
const fastify = Fastify()
|
||||
fastify.setReplySerializer(function (payload, statusCode) {
|
||||
t.same(payload, { name: 'Foo', work: 'Bar', nick: 'Boo' })
|
||||
t.assert.deepStrictEqual(payload, { name: 'Foo', work: 'Bar', nick: 'Boo' })
|
||||
return 'instance serializator'
|
||||
})
|
||||
|
||||
@@ -706,9 +826,9 @@ test('Reply serializer win over serializer ', t => {
|
||||
}
|
||||
},
|
||||
serializerCompiler: ({ schema, method, url, httpPart }) => {
|
||||
t.ok(method, 'the custom compiler has been created')
|
||||
t.assert.ok(method, 'the custom compiler has been created')
|
||||
return () => {
|
||||
t.fail('the serializer must not be called when there is a reply serializer')
|
||||
t.assert.fail('the serializer must not be called when there is a reply serializer')
|
||||
return 'fail'
|
||||
}
|
||||
}
|
||||
@@ -717,18 +837,19 @@ test('Reply serializer win over serializer ', t => {
|
||||
})
|
||||
|
||||
fastify.inject('/', (err, res) => {
|
||||
t.error(err)
|
||||
t.same(res.payload, 'instance serializator')
|
||||
t.equal(res.statusCode, 200)
|
||||
t.assert.ifError(err)
|
||||
t.assert.deepStrictEqual(res.payload, 'instance serializator')
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('Reply serializer win over serializer ', t => {
|
||||
test('Reply serializer win over serializer ', (t, testDone) => {
|
||||
t.plan(6)
|
||||
|
||||
const fastify = Fastify()
|
||||
fastify.setReplySerializer(function (payload, statusCode) {
|
||||
t.same(payload, { name: 'Foo', work: 'Bar', nick: 'Boo' })
|
||||
t.assert.deepStrictEqual(payload, { name: 'Foo', work: 'Bar', nick: 'Boo' })
|
||||
return 'instance serializator'
|
||||
})
|
||||
|
||||
@@ -745,9 +866,9 @@ test('Reply serializer win over serializer ', t => {
|
||||
}
|
||||
},
|
||||
serializerCompiler: ({ schema, method, url, httpPart }) => {
|
||||
t.ok(method, 'the custom compiler has been created')
|
||||
t.assert.ok(method, 'the custom compiler has been created')
|
||||
return () => {
|
||||
t.fail('the serializer must not be called when there is a reply serializer')
|
||||
t.assert.fail('the serializer must not be called when there is a reply serializer')
|
||||
return 'fail'
|
||||
}
|
||||
}
|
||||
@@ -756,13 +877,14 @@ test('Reply serializer win over serializer ', t => {
|
||||
})
|
||||
|
||||
fastify.inject('/', (err, res) => {
|
||||
t.error(err)
|
||||
t.same(res.payload, 'instance serializator')
|
||||
t.equal(res.statusCode, 200)
|
||||
t.assert.ifError(err)
|
||||
t.assert.deepStrictEqual(res.payload, 'instance serializator')
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('The schema compiler recreate itself if needed', t => {
|
||||
test('The schema compiler recreate itself if needed', (t, testDone) => {
|
||||
t.plan(1)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -792,7 +914,10 @@ test('The schema compiler recreate itself if needed', t => {
|
||||
done()
|
||||
})
|
||||
|
||||
fastify.ready(err => { t.error(err) })
|
||||
fastify.ready(err => {
|
||||
t.assert.ifError(err)
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('The schema changes the default error handler output', async t => {
|
||||
@@ -829,12 +954,12 @@ test('The schema changes the default error handler output', async t => {
|
||||
})
|
||||
|
||||
let res = await fastify.inject('/501')
|
||||
t.equal(res.statusCode, 501)
|
||||
t.same(res.json(), { message: '501 message' })
|
||||
t.assert.strictEqual(res.statusCode, 501)
|
||||
t.assert.deepStrictEqual(res.json(), { message: '501 message' })
|
||||
|
||||
res = await fastify.inject('/500')
|
||||
t.equal(res.statusCode, 500)
|
||||
t.same(res.json(), { error: 'Internal Server Error', message: '500 message', customId: 42 })
|
||||
t.assert.strictEqual(res.statusCode, 500)
|
||||
t.assert.deepStrictEqual(res.json(), { error: 'Internal Server Error', message: '500 message', customId: 42 })
|
||||
})
|
||||
|
||||
test('do not crash if status code serializer errors', async t => {
|
||||
@@ -863,7 +988,7 @@ test('do not crash if status code serializer errors', async t => {
|
||||
}
|
||||
},
|
||||
(request, reply) => {
|
||||
t.fail('handler, should not be called')
|
||||
t.assert.fail('handler, should not be called')
|
||||
}
|
||||
)
|
||||
|
||||
@@ -873,8 +998,8 @@ test('do not crash if status code serializer errors', async t => {
|
||||
notfoo: true
|
||||
}
|
||||
})
|
||||
t.equal(res.statusCode, 500)
|
||||
t.same(res.json(), {
|
||||
t.assert.strictEqual(res.statusCode, 500)
|
||||
t.assert.deepStrictEqual(res.json(), {
|
||||
statusCode: 500,
|
||||
code: 'FST_ERR_FAILED_ERROR_SERIALIZATION',
|
||||
message: 'Failed to serialize an error. Error: "customCode" is required!. ' +
|
||||
@@ -905,11 +1030,11 @@ test('custom schema serializer error, empty message', async t => {
|
||||
})
|
||||
|
||||
const res = await fastify.inject('/501')
|
||||
t.equal(res.statusCode, 501)
|
||||
t.same(res.json(), { message: '' })
|
||||
t.assert.strictEqual(res.statusCode, 501)
|
||||
t.assert.deepStrictEqual(res.json(), { message: '' })
|
||||
})
|
||||
|
||||
test('error in custom schema serialize compiler, throw FST_ERR_SCH_SERIALIZATION_BUILD error', t => {
|
||||
test('error in custom schema serialize compiler, throw FST_ERR_SCH_SERIALIZATION_BUILD error', (t, testDone) => {
|
||||
t.plan(3)
|
||||
|
||||
const fastify = Fastify()
|
||||
@@ -939,9 +1064,10 @@ test('error in custom schema serialize compiler, throw FST_ERR_SCH_SERIALIZATION
|
||||
})
|
||||
|
||||
fastify.ready((err) => {
|
||||
t.equal(err.message, 'Failed building the serialization schema for GET: /, due to error CUSTOM_ERROR')
|
||||
t.equal(err.statusCode, 500)
|
||||
t.equal(err.code, 'FST_ERR_SCH_SERIALIZATION_BUILD')
|
||||
t.assert.strictEqual(err.message, 'Failed building the serialization schema for GET: /, due to error CUSTOM_ERROR')
|
||||
t.assert.strictEqual(err.statusCode, 500)
|
||||
t.assert.strictEqual(err.code, 'FST_ERR_SCH_SERIALIZATION_BUILD')
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -973,20 +1099,19 @@ test('Errors in serializer send to errorHandler', async t => {
|
||||
|
||||
const res = await fastify.inject('/')
|
||||
|
||||
t.equal(res.statusCode, 500)
|
||||
t.assert.strictEqual(res.statusCode, 500)
|
||||
|
||||
// t.same(savedError, new Error('"name" is required!'));
|
||||
t.same(res.json(), {
|
||||
// t.assert.deepStrictEqual(savedError, new Error('"name" is required!'));
|
||||
t.assert.deepStrictEqual(res.json(), {
|
||||
statusCode: 500,
|
||||
error: 'Internal Server Error',
|
||||
message: '"name" is required!'
|
||||
})
|
||||
t.ok(savedError, 'error presents')
|
||||
t.ok(savedError.serialization, 'Serialization sign presents')
|
||||
t.end()
|
||||
t.assert.ok(savedError, 'error presents')
|
||||
t.assert.ok(savedError.serialization, 'Serialization sign presents')
|
||||
})
|
||||
|
||||
test('capital X', t => {
|
||||
test('capital X', (t, testDone) => {
|
||||
t.plan(3)
|
||||
|
||||
const fastify = Fastify()
|
||||
@@ -1007,13 +1132,14 @@ test('capital X', t => {
|
||||
})
|
||||
|
||||
fastify.inject('/', (err, res) => {
|
||||
t.error(err)
|
||||
t.same(res.json(), { name: 'Foo', work: 'Bar' })
|
||||
t.equal(res.statusCode, 200)
|
||||
t.assert.ifError(err)
|
||||
t.assert.deepStrictEqual(res.json(), { name: 'Foo', work: 'Bar' })
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
test('allow default as status code and used as last fallback', t => {
|
||||
test('allow default as status code and used as last fallback', (t, testDone) => {
|
||||
t.plan(3)
|
||||
const fastify = Fastify()
|
||||
|
||||
@@ -1037,8 +1163,9 @@ test('allow default as status code and used as last fallback', t => {
|
||||
})
|
||||
|
||||
fastify.inject('/', (err, res) => {
|
||||
t.error(err)
|
||||
t.same(res.json(), { name: 'Foo', work: 'Bar' })
|
||||
t.equal(res.statusCode, 200)
|
||||
t.assert.ifError(err)
|
||||
t.assert.deepStrictEqual(res.json(), { name: 'Foo', work: 'Bar' })
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user