Aktueller Stand
This commit is contained in:
158
backend/node_modules/fastify/test/internals/validation.test.js
generated
vendored
158
backend/node_modules/fastify/test/internals/validation.test.js
generated
vendored
@@ -1,7 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const t = require('tap')
|
||||
const test = t.test
|
||||
const { test } = require('node:test')
|
||||
|
||||
const Ajv = require('ajv')
|
||||
const ajv = new Ajv({ coerceTypes: true })
|
||||
@@ -13,11 +12,11 @@ const { kSchemaVisited } = require('../../lib/symbols')
|
||||
|
||||
test('Symbols', t => {
|
||||
t.plan(5)
|
||||
t.equal(typeof symbols.responseSchema, 'symbol')
|
||||
t.equal(typeof symbols.bodySchema, 'symbol')
|
||||
t.equal(typeof symbols.querystringSchema, 'symbol')
|
||||
t.equal(typeof symbols.paramsSchema, 'symbol')
|
||||
t.equal(typeof symbols.headersSchema, 'symbol')
|
||||
t.assert.strictEqual(typeof symbols.responseSchema, 'symbol')
|
||||
t.assert.strictEqual(typeof symbols.bodySchema, 'symbol')
|
||||
t.assert.strictEqual(typeof symbols.querystringSchema, 'symbol')
|
||||
t.assert.strictEqual(typeof symbols.paramsSchema, 'symbol')
|
||||
t.assert.strictEqual(typeof symbols.headersSchema, 'symbol')
|
||||
})
|
||||
|
||||
;['compileSchemasForValidation',
|
||||
@@ -26,15 +25,15 @@ test('Symbols', t => {
|
||||
t.plan(2)
|
||||
const context = {}
|
||||
validation[func](context)
|
||||
t.equal(typeof context[symbols.bodySchema], 'undefined')
|
||||
t.equal(typeof context[symbols.responseSchema], 'undefined')
|
||||
t.assert.strictEqual(typeof context[symbols.bodySchema], 'undefined')
|
||||
t.assert.strictEqual(typeof context[symbols.responseSchema], 'undefined')
|
||||
})
|
||||
|
||||
test(`${func} schema - missing output schema`, t => {
|
||||
t.plan(1)
|
||||
const context = { schema: {} }
|
||||
validation[func](context, null)
|
||||
t.equal(typeof context[symbols.responseSchema], 'undefined')
|
||||
t.assert.strictEqual(typeof context[symbols.responseSchema], 'undefined')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -59,8 +58,8 @@ test('build schema - output schema', t => {
|
||||
}
|
||||
}
|
||||
validation.compileSchemasForSerialization(opts, ({ schema, method, url, httpPart }) => ajv.compile(schema))
|
||||
t.equal(typeof opts[symbols.responseSchema]['2xx'], 'function')
|
||||
t.equal(typeof opts[symbols.responseSchema]['201'], 'function')
|
||||
t.assert.strictEqual(typeof opts[symbols.responseSchema]['2xx'], 'function')
|
||||
t.assert.strictEqual(typeof opts[symbols.responseSchema]['201'], 'function')
|
||||
})
|
||||
|
||||
test('build schema - body schema', t => {
|
||||
@@ -76,7 +75,7 @@ test('build schema - body schema', t => {
|
||||
}
|
||||
}
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => ajv.compile(schema))
|
||||
t.equal(typeof opts[symbols.bodySchema], 'function')
|
||||
t.assert.strictEqual(typeof opts[symbols.bodySchema], 'function')
|
||||
})
|
||||
|
||||
test('build schema - body with multiple content type schemas', t => {
|
||||
@@ -101,15 +100,13 @@ test('build schema - body with multiple content type schemas', t => {
|
||||
}
|
||||
}
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => ajv.compile(schema))
|
||||
t.type(opts[symbols.bodySchema]['application/json'], 'function')
|
||||
t.type(opts[symbols.bodySchema]['text/plain'], 'function')
|
||||
t.assert.ok(opts[symbols.bodySchema]['application/json'], 'function')
|
||||
t.assert.ok(opts[symbols.bodySchema]['text/plain'], 'function')
|
||||
})
|
||||
|
||||
test('build schema - avoid repeated normalize schema', t => {
|
||||
t.plan(3)
|
||||
const serverConfig = {
|
||||
jsonShorthand: true
|
||||
}
|
||||
const serverConfig = {}
|
||||
const opts = {
|
||||
schema: {
|
||||
query: {
|
||||
@@ -120,17 +117,15 @@ test('build schema - avoid repeated normalize schema', t => {
|
||||
}
|
||||
}
|
||||
}
|
||||
opts.schema = normalizeSchema({}, opts.schema, serverConfig)
|
||||
t.not(kSchemaVisited, undefined)
|
||||
t.equal(opts.schema[kSchemaVisited], true)
|
||||
t.equal(opts.schema, normalizeSchema({}, opts.schema, serverConfig))
|
||||
opts.schema = normalizeSchema(opts.schema, serverConfig)
|
||||
t.assert.notStrictEqual(kSchemaVisited, undefined)
|
||||
t.assert.strictEqual(opts.schema[kSchemaVisited], true)
|
||||
t.assert.strictEqual(opts.schema, normalizeSchema(opts.schema, serverConfig))
|
||||
})
|
||||
|
||||
test('build schema - query schema', t => {
|
||||
t.plan(2)
|
||||
const serverConfig = {
|
||||
jsonShorthand: true
|
||||
}
|
||||
const serverConfig = {}
|
||||
const opts = {
|
||||
schema: {
|
||||
query: {
|
||||
@@ -141,28 +136,29 @@ test('build schema - query schema', t => {
|
||||
}
|
||||
}
|
||||
}
|
||||
opts.schema = normalizeSchema({}, opts.schema, serverConfig)
|
||||
opts.schema = normalizeSchema(opts.schema, serverConfig)
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => ajv.compile(schema))
|
||||
t.type(opts[symbols.querystringSchema].schema.type, 'string')
|
||||
t.equal(typeof opts[symbols.querystringSchema], 'function')
|
||||
t.assert.ok(typeof opts[symbols.querystringSchema].schema.type === 'string')
|
||||
t.assert.strictEqual(typeof opts[symbols.querystringSchema], 'function')
|
||||
})
|
||||
|
||||
test('build schema - query schema abbreviated', t => {
|
||||
t.plan(2)
|
||||
const serverConfig = {
|
||||
jsonShorthand: true
|
||||
}
|
||||
const serverConfig = {}
|
||||
const opts = {
|
||||
schema: {
|
||||
query: {
|
||||
hello: { type: 'string' }
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
opts.schema = normalizeSchema({}, opts.schema, serverConfig)
|
||||
opts.schema = normalizeSchema(opts.schema, serverConfig)
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => ajv.compile(schema))
|
||||
t.type(opts[symbols.querystringSchema].schema.type, 'string')
|
||||
t.equal(typeof opts[symbols.querystringSchema], 'function')
|
||||
t.assert.ok(typeof opts[symbols.querystringSchema].schema.type === 'string')
|
||||
t.assert.strictEqual(typeof opts[symbols.querystringSchema], 'function')
|
||||
})
|
||||
|
||||
test('build schema - querystring schema', t => {
|
||||
@@ -178,34 +174,33 @@ test('build schema - querystring schema', t => {
|
||||
}
|
||||
}
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => ajv.compile(schema))
|
||||
t.type(opts[symbols.querystringSchema].schema.type, 'string')
|
||||
t.equal(typeof opts[symbols.querystringSchema], 'function')
|
||||
t.assert.ok(typeof opts[symbols.querystringSchema].schema.type === 'string')
|
||||
t.assert.strictEqual(typeof opts[symbols.querystringSchema], 'function')
|
||||
})
|
||||
|
||||
test('build schema - querystring schema abbreviated', t => {
|
||||
t.plan(2)
|
||||
const serverConfig = {
|
||||
jsonShorthand: true
|
||||
}
|
||||
const serverConfig = {}
|
||||
const opts = {
|
||||
schema: {
|
||||
querystring: {
|
||||
hello: { type: 'string' }
|
||||
type: 'object',
|
||||
properties: {
|
||||
hello: { type: 'string' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
opts.schema = normalizeSchema({}, opts.schema, serverConfig)
|
||||
opts.schema = normalizeSchema(opts.schema, serverConfig)
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => ajv.compile(schema))
|
||||
t.type(opts[symbols.querystringSchema].schema.type, 'string')
|
||||
t.equal(typeof opts[symbols.querystringSchema], 'function')
|
||||
t.assert.ok(typeof opts[symbols.querystringSchema].schema.type === 'string')
|
||||
t.assert.strictEqual(typeof opts[symbols.querystringSchema], 'function')
|
||||
})
|
||||
|
||||
test('build schema - must throw if querystring and query schema exist', t => {
|
||||
t.plan(2)
|
||||
try {
|
||||
const serverConfig = {
|
||||
jsonShorthand: true
|
||||
}
|
||||
const serverConfig = {}
|
||||
const opts = {
|
||||
schema: {
|
||||
query: {
|
||||
@@ -222,10 +217,10 @@ test('build schema - must throw if querystring and query schema exist', t => {
|
||||
}
|
||||
}
|
||||
}
|
||||
opts.schema = normalizeSchema({}, opts.schema, serverConfig)
|
||||
opts.schema = normalizeSchema(opts.schema, serverConfig)
|
||||
} catch (err) {
|
||||
t.equal(err.code, 'FST_ERR_SCH_DUPLICATE')
|
||||
t.equal(err.message, 'Schema with \'querystring\' already present!')
|
||||
t.assert.strictEqual(err.code, 'FST_ERR_SCH_DUPLICATE')
|
||||
t.assert.strictEqual(err.message, 'Schema with \'querystring\' already present!')
|
||||
}
|
||||
})
|
||||
|
||||
@@ -242,7 +237,7 @@ test('build schema - params schema', t => {
|
||||
}
|
||||
}
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => ajv.compile(schema))
|
||||
t.equal(typeof opts[symbols.paramsSchema], 'function')
|
||||
t.assert.strictEqual(typeof opts[symbols.paramsSchema], 'function')
|
||||
})
|
||||
|
||||
test('build schema - headers schema', t => {
|
||||
@@ -258,7 +253,7 @@ test('build schema - headers schema', t => {
|
||||
}
|
||||
}
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => ajv.compile(schema))
|
||||
t.equal(typeof opts[symbols.headersSchema], 'function')
|
||||
t.assert.strictEqual(typeof opts[symbols.headersSchema], 'function')
|
||||
})
|
||||
|
||||
test('build schema - headers are lowercase', t => {
|
||||
@@ -274,38 +269,38 @@ test('build schema - headers are lowercase', t => {
|
||||
}
|
||||
}
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => {
|
||||
t.ok(schema.properties['content-type'], 'lowercase content-type exists')
|
||||
return () => {}
|
||||
t.assert.ok(schema.properties['content-type'], 'lowercase content-type exists')
|
||||
return () => { }
|
||||
})
|
||||
})
|
||||
|
||||
test('build schema - headers are not lowercased in case of custom object', t => {
|
||||
t.plan(1)
|
||||
|
||||
class Headers {}
|
||||
class Headers { }
|
||||
const opts = {
|
||||
schema: {
|
||||
headers: new Headers()
|
||||
}
|
||||
}
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => {
|
||||
t.type(schema, Headers)
|
||||
return () => {}
|
||||
t.assert.ok(schema, Headers)
|
||||
return () => { }
|
||||
})
|
||||
})
|
||||
|
||||
test('build schema - headers are not lowercased in case of custom validator provided', t => {
|
||||
t.plan(1)
|
||||
|
||||
class Headers {}
|
||||
class Headers { }
|
||||
const opts = {
|
||||
schema: {
|
||||
headers: new Headers()
|
||||
}
|
||||
}
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => {
|
||||
t.type(schema, Headers)
|
||||
return () => {}
|
||||
t.assert.ok(schema, Headers)
|
||||
return () => { }
|
||||
}, true)
|
||||
})
|
||||
|
||||
@@ -322,29 +317,22 @@ test('build schema - uppercased headers are not included', t => {
|
||||
}
|
||||
}
|
||||
validation.compileSchemasForValidation(opts, ({ schema, method, url, httpPart }) => {
|
||||
t.notOk('Content-Type' in schema.properties, 'uppercase does not exist')
|
||||
return () => {}
|
||||
t.assert.ok(!('Content-Type' in schema.properties), 'uppercase does not exist')
|
||||
return () => { }
|
||||
})
|
||||
})
|
||||
|
||||
test('build schema - mixed schema types are individually skipped or normalized', t => {
|
||||
t.plan(6)
|
||||
t.plan(2)
|
||||
|
||||
class CustomSchemaClass {}
|
||||
const nonNormalizedSchema = {
|
||||
hello: { type: 'string' }
|
||||
}
|
||||
const normalizedSchema = {
|
||||
type: 'object',
|
||||
properties: nonNormalizedSchema
|
||||
}
|
||||
class CustomSchemaClass { }
|
||||
|
||||
const testCases = [{
|
||||
schema: {
|
||||
body: new CustomSchemaClass()
|
||||
},
|
||||
assertions: (schema) => {
|
||||
t.type(schema.body, CustomSchemaClass)
|
||||
t.assert.ok(schema.body, CustomSchemaClass)
|
||||
}
|
||||
}, {
|
||||
schema: {
|
||||
@@ -353,34 +341,12 @@ test('build schema - mixed schema types are individually skipped or normalized',
|
||||
}
|
||||
},
|
||||
assertions: (schema) => {
|
||||
t.type(schema.response[200], CustomSchemaClass)
|
||||
}
|
||||
}, {
|
||||
schema: {
|
||||
body: nonNormalizedSchema,
|
||||
response: {
|
||||
200: new CustomSchemaClass()
|
||||
}
|
||||
},
|
||||
assertions: (schema) => {
|
||||
t.same(schema.body, normalizedSchema)
|
||||
t.type(schema.response[200], CustomSchemaClass)
|
||||
}
|
||||
}, {
|
||||
schema: {
|
||||
body: new CustomSchemaClass(),
|
||||
response: {
|
||||
200: nonNormalizedSchema
|
||||
}
|
||||
},
|
||||
assertions: (schema) => {
|
||||
t.type(schema.body, CustomSchemaClass)
|
||||
t.same(schema.response[200], normalizedSchema)
|
||||
t.assert.ok(schema.response[200], CustomSchemaClass)
|
||||
}
|
||||
}]
|
||||
|
||||
testCases.forEach((testCase) => {
|
||||
const result = normalizeSchema({}, testCase.schema, { jsonShorthand: true })
|
||||
const result = normalizeSchema(testCase.schema, {})
|
||||
testCase.assertions(result)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user