Aktueller Stand

This commit is contained in:
2026-01-22 19:05:45 +01:00
parent 85dee61a4d
commit e280e4eadb
1967 changed files with 397327 additions and 74093 deletions

View File

@@ -1,6 +1,6 @@
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifyHelmet = require('@fastify/helmet')
const fastifySwagger = require('@fastify/swagger')
@@ -33,9 +33,9 @@ test('staticCSP = undefined', async (t) => {
method: 'GET',
url: '/documentation'
})
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
t.equal(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(typeof res.headers['content-security-policy'], 'undefined')
t.assert.deepStrictEqual(typeof res.payload, 'string')
})
test('staticCSP = true', async (t) => {
@@ -59,9 +59,9 @@ test('staticCSP = true', async (t) => {
method: 'GET',
url: '/documentation'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], `default-src 'self'; base-uri 'self'; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data: validator.swagger.io; object-src 'none'; script-src 'self' ${csp.script.join(' ')}; script-src-attr 'none'; style-src 'self' https: ${csp.style.join(' ')}; upgrade-insecure-requests;`)
t.equal(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-security-policy'], `default-src 'self'; base-uri 'self'; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data: validator.swagger.io; object-src 'none'; script-src 'self' ${csp.script.join(' ')}; script-src-attr 'none'; style-src 'self' https: ${csp.style.join(' ')}; upgrade-insecure-requests;`)
t.assert.deepStrictEqual(typeof res.payload, 'string')
}
{
@@ -69,8 +69,8 @@ test('staticCSP = true', async (t) => {
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(typeof res.headers['content-security-policy'], 'undefined')
}
})
@@ -95,9 +95,9 @@ test('staticCSP = "default-src \'self\';"', async (t) => {
method: 'GET',
url: '/documentation'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], "default-src 'self';")
t.equal(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-security-policy'], "default-src 'self';")
t.assert.deepStrictEqual(typeof res.payload, 'string')
}
{
@@ -105,8 +105,8 @@ test('staticCSP = "default-src \'self\';"', async (t) => {
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(typeof res.headers['content-security-policy'], 'undefined')
}
})
@@ -134,9 +134,9 @@ test('staticCSP = object', async (t) => {
method: 'GET',
url: '/documentation'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';")
t.equal(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';")
t.assert.deepStrictEqual(typeof res.payload, 'string')
}
{
@@ -144,8 +144,8 @@ test('staticCSP = object', async (t) => {
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(typeof res.headers['content-security-policy'], 'undefined')
}
})
@@ -157,7 +157,7 @@ test('transformStaticCSP = function', async (t) => {
await fastify.register(fastifySwaggerUi, {
staticCSP: "default-src 'self';",
transformStaticCSP: function (header) {
t.equal(header, "default-src 'self';")
t.assert.deepStrictEqual(header, "default-src 'self';")
return "default-src 'self'; script-src 'self';"
}
})
@@ -174,9 +174,9 @@ test('transformStaticCSP = function', async (t) => {
method: 'GET',
url: '/documentation'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';")
t.equal(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';")
t.assert.deepStrictEqual(typeof res.payload, 'string')
}
{
@@ -184,8 +184,8 @@ test('transformStaticCSP = function', async (t) => {
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(typeof res.headers['content-security-policy'], 'undefined')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(typeof res.headers['content-security-policy'], 'undefined')
}
})
@@ -197,7 +197,7 @@ test('transformStaticCSP = function, with @fastify/helmet', async (t) => {
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, {
transformStaticCSP: function (header) {
t.equal(header, "default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests")
t.assert.deepStrictEqual(header, "default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests")
return "default-src 'self'; script-src 'self';"
}
})
@@ -214,9 +214,9 @@ test('transformStaticCSP = function, with @fastify/helmet', async (t) => {
method: 'GET',
url: '/documentation'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';")
t.equal(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-security-policy'], "default-src 'self'; script-src 'self';")
t.assert.deepStrictEqual(typeof res.payload, 'string')
}
{
@@ -224,7 +224,7 @@ test('transformStaticCSP = function, with @fastify/helmet', async (t) => {
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-security-policy'], "default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests")
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-security-policy'], "default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests")
}
})

View File

@@ -1,6 +1,6 @@
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifySwagger = require('@fastify/swagger')
const fastifySwaggerUi = require('../index')
@@ -12,7 +12,7 @@ test('fastify.swaggerCSP should exist', async (t) => {
await fastify.register(fastifySwagger)
await fastify.register(fastifySwaggerUi)
t.ok(fastify.swaggerCSP)
t.ok(Array.isArray(fastify.swaggerCSP.script))
t.ok(Array.isArray(fastify.swaggerCSP.style))
t.assert.ok(fastify.swaggerCSP)
t.assert.ok(Array.isArray(fastify.swaggerCSP.script))
t.assert.ok(Array.isArray(fastify.swaggerCSP.style))
})

View File

@@ -1,6 +1,6 @@
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const yaml = require('yaml')
@@ -9,7 +9,7 @@ const fastifySwaggerUi = require('../index')
const { swaggerOption, schemaBody } = require('../examples/options')
const authOptions = {
validate (username, password, req, reply, done) {
validate (username, password, _req, _reply, done) {
if (username === 'admin' && password === 'admin') {
done()
} else {
@@ -39,36 +39,36 @@ test('hooks on static swagger', async t => {
})
let res = await fastify.inject('/documentation')
t.equal(res.statusCode, 401, 'root auth required')
t.assert.deepStrictEqual(res.statusCode, 401, 'root auth required')
res = await fastify.inject('/documentation/yaml')
t.equal(res.statusCode, 401, 'auth required yaml')
t.assert.deepStrictEqual(res.statusCode, 401, 'auth required yaml')
res = await fastify.inject({
method: 'GET',
url: '/documentation/yaml',
headers: { authorization: basicAuthEncode('admin', 'admin') }
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/x-yaml')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml')
try {
yaml.parse(res.payload)
t.pass('valid swagger yaml')
t.assert.ok(true, 'valid swagger yaml')
} catch (err) {
t.fail(err)
t.assert.fail(err)
}
res = await fastify.inject('/documentation/json')
t.equal(res.statusCode, 401, 'auth required json')
t.assert.deepStrictEqual(res.statusCode, 401, 'auth required json')
res = await fastify.inject({
method: 'GET',
url: '/documentation/json',
headers: { authorization: basicAuthEncode('admin', 'admin') }
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
try {
yaml.parse(res.payload)
t.pass('valid swagger json')
t.assert.ok(true, 'valid swagger json')
} catch (err) {
t.fail(err)
}
@@ -91,24 +91,24 @@ test('hooks on dynamic swagger', async t => {
fastify.post('/fooBar123', schemaBody, () => {})
let res = await fastify.inject('/documentation')
t.equal(res.statusCode, 401, 'root auth required')
t.assert.deepStrictEqual(res.statusCode, 401, 'root auth required')
res = await fastify.inject('/documentation/yaml')
t.equal(res.statusCode, 401, 'auth required yaml')
t.assert.deepStrictEqual(res.statusCode, 401, 'auth required yaml')
res = await fastify.inject('/documentation/json')
t.equal(res.statusCode, 401, 'auth required json')
t.assert.deepStrictEqual(res.statusCode, 401, 'auth required json')
res = await fastify.inject({
method: 'GET',
url: '/documentation/json',
headers: { authorization: basicAuthEncode('admin', 'admin') }
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
const swaggerObject = res.json()
t.ok(swaggerObject.paths)
t.ok(swaggerObject.paths['/fooBar123'])
t.assert.ok(swaggerObject.paths)
t.assert.ok(swaggerObject.paths['/fooBar123'])
})
test('catch all added schema', async t => {
@@ -116,7 +116,7 @@ test('catch all added schema', async t => {
await fastify.register(fastifySwagger, {
openapi: {},
refResolver: {
buildLocalReference: (json, baseUri, fragment, i) => {
buildLocalReference: (json, _baseUri, _fragment, i) => {
return json.$id || `def-${i}`
}
}
@@ -136,5 +136,5 @@ test('catch all added schema', async t => {
await fastify.ready()
const openapi = fastify.swagger()
t.same(Object.keys(openapi.components.schemas), ['Root', 'Instance', 'Sub-Instance'])
t.assert.deepStrictEqual(Object.keys(openapi.components.schemas), ['Root', 'Instance', 'Sub-Instance'])
})

View File

@@ -1,6 +1,6 @@
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifySwagger = require('@fastify/swagger')
const fastifyHelmet = require('@fastify/helmet')
@@ -33,7 +33,7 @@ test('fastify will response swagger csp', async (t) => {
})
// route for testing CSP headers
fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send({
foo: 'bar'
})
@@ -43,5 +43,5 @@ test('fastify will response swagger csp', async (t) => {
method: 'GET',
url: '/'
})
t.same(res.headers['content-security-policy'], csp)
t.assert.deepStrictEqual(res.headers['content-security-policy'], csp)
})

View File

@@ -1,6 +1,6 @@
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifySwagger = require('@fastify/swagger')
const fastifySwaggerUi = require('../index')
@@ -17,8 +17,8 @@ test('Swagger source does not contain sourceMaps', async (t) => {
})
const includesSourceMap = res.payload.includes('sourceMappingURL')
t.equal(includesSourceMap, false)
t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')
t.assert.deepStrictEqual(includesSourceMap, false)
t.assert.deepStrictEqual(res.headers['content-type'], 'application/javascript; charset=utf-8')
})
test('Swagger css does not contain sourceMaps', async (t) => {
@@ -33,6 +33,6 @@ test('Swagger css does not contain sourceMaps', async (t) => {
})
const includesSourceMap = res.payload.includes('sourceMappingURL')
t.equal(includesSourceMap, false)
t.equal(res.headers['content-type'], 'text/css; charset=UTF-8')
t.assert.deepStrictEqual(includesSourceMap, false)
t.assert.deepStrictEqual(res.headers['content-type'], 'text/css; charset=utf-8')
})

View File

@@ -1,7 +1,7 @@
'use strict'
const t = require('tap')
const test = t.test
const nodeTest = require('node:test')
const test = nodeTest.test
const Fastify = require('fastify')
const Swagger = require('@apidevtools/swagger-parser')
const yaml = require('yaml')
@@ -16,6 +16,7 @@ const {
} = require('../examples/options')
const resolve = require('node:path').resolve
const join = require('node:path').join
const readFileSync = require('node:fs').readFileSync
const schemaParamsWithoutDesc = {
@@ -71,7 +72,7 @@ test('/documentation/json route', async (t) => {
const payload = JSON.parse(res.payload)
await Swagger.validate(payload)
t.pass('valid swagger object')
t.assert.ok(true, 'valid swagger object')
})
test('fastify.swagger should return a valid swagger yaml', async (t) => {
@@ -94,10 +95,10 @@ test('fastify.swagger should return a valid swagger yaml', async (t) => {
url: '/documentation/yaml'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml')
yaml.parse(res.payload)
t.pass('valid swagger yaml')
t.assert.ok(true, 'valid swagger yaml')
})
test('/documentation should display index html', async (t) => {
@@ -117,10 +118,10 @@ test('/documentation should display index html', async (t) => {
method: 'GET',
url: '/documentation'
})
t.equal(res.statusCode, 200)
t.equal(res.headers.location, undefined)
t.equal(typeof res.payload, 'string')
t.equal('text/html; charset=utf-8', res.headers['content-type'])
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.location, undefined)
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type'])
})
test('/documentation/ should display index html ', async (t) => {
@@ -140,10 +141,10 @@ test('/documentation/ should display index html ', async (t) => {
method: 'GET',
url: '/documentation/'
})
t.equal(res.statusCode, 200)
t.equal(res.headers.location, undefined)
t.equal(typeof res.payload, 'string')
t.equal('text/html; charset=utf-8', res.headers['content-type'])
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.location, undefined)
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type'])
})
test('/v1/documentation should display index html', async (t) => {
@@ -163,10 +164,10 @@ test('/v1/documentation should display index html', async (t) => {
method: 'GET',
url: '/v1/documentation'
})
t.equal(res.statusCode, 200)
t.equal(res.headers.location, undefined)
t.equal(typeof res.payload, 'string')
t.equal('text/html; charset=utf-8', res.headers['content-type'])
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.location, undefined)
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type'])
})
test('/v1/documentation/ should display index html', async (t) => {
@@ -186,17 +187,17 @@ test('/v1/documentation/ should display index html', async (t) => {
method: 'GET',
url: '/v1/documentation/'
})
t.equal(res.statusCode, 200)
t.equal(res.headers.location, undefined)
t.equal(typeof res.payload, 'string')
t.equal('text/html; charset=utf-8', res.headers['content-type'])
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.location, undefined)
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type'])
})
test('/v1/foobar should display index html', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(async function (fastify, options) {
fastify.register(async function (fastify) {
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, { routePrefix: '/foobar', noRedirect: true })
@@ -212,17 +213,17 @@ test('/v1/foobar should display index html', async (t) => {
method: 'GET',
url: '/v1/foobar'
})
t.equal(res.statusCode, 200)
t.equal(res.headers.location, undefined)
t.equal(typeof res.payload, 'string')
t.equal('text/html; charset=utf-8', res.headers['content-type'])
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.location, undefined)
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type'])
})
test('/v1/foobar/ should display index html', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.register(async function (fastify, options) {
fastify.register(async function (fastify) {
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, { routePrefix: '/foobar' })
@@ -238,10 +239,10 @@ test('/v1/foobar/ should display index html', async (t) => {
method: 'GET',
url: '/v1/foobar/'
})
t.equal(res.statusCode, 200)
t.equal(res.headers.location, undefined)
t.equal(typeof res.payload, 'string')
t.equal('text/html; charset=utf-8', res.headers['content-type'])
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.location, undefined)
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type'])
})
test('with routePrefix: \'/\' should display index html', async (t) => {
@@ -257,10 +258,10 @@ test('with routePrefix: \'/\' should display index html', async (t) => {
method: 'GET',
url: '/'
})
t.equal(res.statusCode, 200)
t.equal(res.headers.location, undefined)
t.equal(typeof res.payload, 'string')
t.equal('text/html; charset=utf-8', res.headers['content-type'])
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.location, undefined)
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type'])
})
test('/documentation/static/:file should send back the correct file', async (t) => {
@@ -284,8 +285,8 @@ test('/documentation/static/:file should send back the correct file', async (t)
method: 'GET',
url: '/documentation/static/index.html'
})
t.equal(res.statusCode, 302)
t.equal(res.headers.location, '/documentation/')
t.assert.deepStrictEqual(res.statusCode, 302)
t.assert.deepStrictEqual(res.headers.location, '/documentation/')
}
{
@@ -293,16 +294,16 @@ test('/documentation/static/:file should send back the correct file', async (t)
method: 'GET',
url: '/documentation/static/'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'text/html; charset=UTF-8')
t.equal(
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8')
t.assert.deepStrictEqual(
readFileSync(
resolve(__dirname, '..', 'static', 'index.html'),
'utf8'
),
res.payload
)
t.ok(res.payload.indexOf('swagger-initializer.js') !== -1)
t.assert.ok(res.payload.indexOf('swagger-initializer.js') !== -1)
}
{
@@ -310,9 +311,9 @@ test('/documentation/static/:file should send back the correct file', async (t)
method: 'GET',
url: '/documentation/static/swagger-initializer.js'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/javascript; charset=utf-8')
t.ok(res.payload.indexOf('resolveUrl') !== -1)
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/javascript; charset=utf-8')
t.assert.ok(res.payload.indexOf('resolveUrl') !== -1)
}
{
@@ -320,9 +321,9 @@ test('/documentation/static/:file should send back the correct file', async (t)
method: 'GET',
url: '/documentation/static/oauth2-redirect.html'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'text/html; charset=UTF-8')
t.equal(
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8')
t.assert.deepStrictEqual(
readFileSync(
resolve(__dirname, '..', 'static', 'oauth2-redirect.html'),
'utf8'
@@ -336,9 +337,9 @@ test('/documentation/static/:file should send back the correct file', async (t)
method: 'GET',
url: '/documentation/static/swagger-ui.css'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'text/css; charset=UTF-8')
t.equal(
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'text/css; charset=utf-8')
t.assert.deepStrictEqual(
readFileSync(
resolve(__dirname, '..', 'static', 'swagger-ui.css'),
'utf8'
@@ -352,9 +353,9 @@ test('/documentation/static/:file should send back the correct file', async (t)
method: 'GET',
url: '/documentation/static/swagger-ui-bundle.js'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')
t.equal(
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/javascript; charset=utf-8')
t.assert.deepStrictEqual(
readFileSync(
resolve(__dirname, '..', 'static', 'swagger-ui-bundle.js'),
'utf8'
@@ -368,9 +369,9 @@ test('/documentation/static/:file should send back the correct file', async (t)
method: 'GET',
url: '/documentation/static/swagger-ui-standalone-preset.js'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')
t.equal(
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/javascript; charset=utf-8')
t.assert.deepStrictEqual(
readFileSync(
resolve(__dirname, '..', 'static', 'swagger-ui-standalone-preset.js'),
'utf8'
@@ -396,8 +397,8 @@ test('/documentation/static/:file should send back file from baseDir', async (t)
method: 'GET',
url: '/documentation/static/example-logo.svg'
})
t.equal(res.statusCode, 200)
t.equal(
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(
res.payload,
readFileSync(
resolve(__dirname, '..', 'examples', 'static', 'example-logo.svg'),
@@ -426,9 +427,10 @@ test('/documentation/static/:file 404', async (t) => {
url: '/documentation/static/stuff.css'
})
const payload = JSON.parse(res.payload)
t.equal(res.statusCode, 404)
t.match(payload, {
t.assert.deepStrictEqual(res.statusCode, 404)
t.assert.deepStrictEqual(payload, {
error: 'Not Found',
message: 'Route GET:/documentation/static/stuff.css not found',
statusCode: 404
})
})
@@ -455,7 +457,7 @@ test('/documentation2/json route (overwrite)', async (t) => {
const payload = JSON.parse(res.payload)
await Swagger.validate(payload)
t.pass('valid swagger object')
t.assert.ok(true, 'valid swagger object')
})
test('/documentation/:myfile should return 404 in dynamic mode', async (t) => {
@@ -468,13 +470,13 @@ test('/documentation/:myfile should return 404 in dynamic mode', async (t) => {
method: 'GET',
url: '/documentation/swagger-ui.js'
})
t.equal(res.statusCode, 404)
t.assert.deepStrictEqual(res.statusCode, 404)
})
test('/documentation/:myfile should run custom NotFoundHandler in dynamic mode', async (t) => {
t.plan(1)
const fastify = Fastify()
const notFoundHandler = function (req, reply) {
const notFoundHandler = function (_req, reply) {
reply.code(410).send()
}
fastify.setNotFoundHandler(notFoundHandler)
@@ -485,7 +487,7 @@ test('/documentation/:myfile should run custom NotFoundHandler in dynamic mode',
method: 'GET',
url: '/documentation/swagger-ui.js'
})
t.equal(res.statusCode, 410)
t.assert.deepStrictEqual(res.statusCode, 410)
})
test('/documentation/* should not return module files when baseDir not set', async (t) => {
@@ -498,14 +500,14 @@ test('/documentation/* should not return module files when baseDir not set', asy
method: 'GET',
url: '/documentation/README.md'
})
t.equal(res.statusCode, 404)
t.assert.deepStrictEqual(res.statusCode, 404)
})
test('should return silent log level of route /documentation', async (t) => {
const fastify = Fastify()
fastify.addHook('onRoute', function (route) {
t.equal(route.logLevel, 'silent')
t.assert.deepStrictEqual(route.logLevel, 'silent')
})
await fastify.register(fastifySwagger, swaggerOption)
@@ -515,15 +517,15 @@ test('should return silent log level of route /documentation', async (t) => {
method: 'GET',
url: '/documentation/'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8')
})
test('should return empty log level of route /documentation', async (t) => {
const fastify = Fastify()
fastify.addHook('onRoute', function (route) {
t.equal(route.logLevel, '')
t.assert.deepStrictEqual(route.logLevel, '')
})
await fastify.register(fastifySwagger, swaggerOption)
@@ -533,12 +535,39 @@ test('should return empty log level of route /documentation', async (t) => {
method: 'GET',
url: '/documentation/'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8')
})
const assertIndexUrls = (t, indexHtml, prefix) => {
t.assert.deepStrictEqual(indexHtml.includes(`href="${prefix}/static/index.css"`), true)
t.assert.deepStrictEqual(indexHtml.includes(`src="${prefix}/static/theme/theme-js.js"`), true)
t.assert.deepStrictEqual(indexHtml.includes(`href="${prefix}/index.css"`), false)
t.assert.deepStrictEqual(indexHtml.includes(`src="${prefix}/theme/theme-js.js"`), false)
}
const validateIndexUrls = async (t, fastify, indexHtml, prefix = '') => {
const hrefs = indexHtml.matchAll(/href="([^"]*)"/g)
for (const [, path] of hrefs) {
const res = await fastify.inject({
method: 'GET',
url: join(prefix, path)
})
t.assert.equal(res.statusCode, 200)
}
const srcs = indexHtml.matchAll(/src="([^"]*)"/g)
for (const [, path] of srcs) {
const res = await fastify.inject({
method: 'GET',
url: join(prefix, path)
})
t.assert.equal(res.statusCode, 200)
}
}
test('/documentation should display index html with correct asset urls', async (t) => {
t.plan(6)
t.plan(13)
const fastify = Fastify()
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] } })
@@ -547,59 +576,66 @@ test('/documentation should display index html with correct asset urls', async (
method: 'GET',
url: '/documentation'
})
t.assert.equal(res.statusCode, 200)
t.equal(res.payload.includes('href="./documentation/static/index.css"'), true)
t.equal(res.payload.includes('src="./documentation/static/theme/theme-js.js"'), true)
t.equal(res.payload.includes('href="./documentation/index.css"'), false)
t.equal(res.payload.includes('src="./documentation/theme/theme-js.js"'), false)
let cssRes = await fastify.inject({
method: 'GET',
url: '/documentation/static/index.css'
})
t.equal(cssRes.statusCode, 200)
cssRes = await fastify.inject({
method: 'GET',
url: './documentation/static/index.css'
})
t.equal(cssRes.statusCode, 200)
assertIndexUrls(t, res.payload, '/documentation')
await validateIndexUrls(t, fastify, res.payload)
})
/**
* This emulates when the server is inside an NGINX application that routes by path
*/
test('/documentation should display index html with correct asset urls when nested', async (t) => {
t.plan(5)
const testCases = [
['/swagger-app', undefined],
['/swagger-app/', undefined],
['/swagger-app', 'documentation']
]
testCases.forEach(([prefix, pluginPrefix]) => {
test(`${prefix} ${pluginPrefix} should display index html with correct asset urls when nested`, async (t) => {
t.plan(13)
const fastify = Fastify()
await fastify.register(
async (childFastify) => {
await childFastify.register(fastifySwagger, swaggerOption)
await childFastify.register(fastifySwaggerUi, { indexPrefix: prefix, routePrefix: pluginPrefix, theme: { js: [{ filename: 'theme-js.js' }] } })
},
{
prefix: '/swagger-app'
}
)
const res = await fastify.inject({
method: 'GET',
url: '/swagger-app/documentation'
})
t.assert.equal(res.statusCode, 200)
assertIndexUrls(t, res.payload, '/swagger-app/documentation')
await validateIndexUrls(t, fastify, res.payload)
})
})
/**
* This emulates when the server is inside an NGINX application that routes by path
*/
test('/api/v1/docs should display index html with correct asset urls', async (t) => {
t.plan(13)
const fastify = Fastify()
await fastify.register(
async () => {
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] } })
},
{
prefix: '/swagger-app'
}
)
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, { routePrefix: '/api/v1/docs', theme: { js: [{ filename: 'theme-js.js' }] } })
const res = await fastify.inject({
method: 'GET',
url: '/swagger-app/documentation'
url: '/api/v1/docs'
})
t.equal(res.payload.includes('href="./documentation/static/index.css"'), true)
t.equal(res.payload.includes('src="./documentation/static/theme/theme-js.js"'), true)
t.equal(res.payload.includes('href="./documentation/index.css"'), false)
t.equal(res.payload.includes('src="./documentation/theme/theme-js.js"'), false)
const cssRes = await fastify.inject({
method: 'GET',
url: '/swagger-app/documentation/static/index.css'
})
t.equal(cssRes.statusCode, 200)
t.assert.equal(res.statusCode, 200)
assertIndexUrls(t, res.payload, '/api/v1/docs')
await validateIndexUrls(t, fastify, res.payload)
})
test('/documentation/ should display index html with correct asset urls', async (t) => {
t.plan(4)
t.plan(13)
const fastify = Fastify()
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] } })
@@ -608,15 +644,14 @@ test('/documentation/ should display index html with correct asset urls', async
method: 'GET',
url: '/documentation/'
})
t.assert.equal(res.statusCode, 200)
t.equal(res.payload.includes('href="./static/index.css"'), true)
t.equal(res.payload.includes('src="./static/theme/theme-js.js"'), true)
t.equal(res.payload.includes('href="./index.css"'), false)
t.equal(res.payload.includes('src="./theme/theme-js.js"'), false)
assertIndexUrls(t, res.payload, '.')
await validateIndexUrls(t, fastify, res.payload, '/documentation/')
})
test('/docs should display index html with correct asset urls when documentation prefix is set', async (t) => {
t.plan(4)
t.plan(13)
const fastify = Fastify()
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] }, routePrefix: '/docs' })
@@ -625,11 +660,10 @@ test('/docs should display index html with correct asset urls when documentation
method: 'GET',
url: '/docs'
})
t.assert.equal(res.statusCode, 200)
t.equal(res.payload.includes('href="./docs/static/index.css"'), true)
t.equal(res.payload.includes('src="./docs/static/theme/theme-js.js"'), true)
t.equal(res.payload.includes('href="./docs/index.css"'), false)
t.equal(res.payload.includes('src="./docs/theme/theme-js.js"'), false)
assertIndexUrls(t, res.payload, '/docs')
await validateIndexUrls(t, fastify, res.payload)
})
test('/docs should display index html with correct asset urls when documentation prefix is set with no leading slash', async (t) => {
@@ -643,10 +677,10 @@ test('/docs should display index html with correct asset urls when documentation
url: '/docs'
})
t.equal(res.payload.includes('href="docs/static/index.css"'), true)
t.equal(res.payload.includes('src="docs/static/theme/theme-js.js"'), true)
t.equal(res.payload.includes('href="docs/index.css"'), false)
t.equal(res.payload.includes('src="docs/theme/theme-js.js"'), false)
t.assert.strictEqual(res.payload.includes('href="docs/static/index.css"'), true)
t.assert.strictEqual(res.payload.includes('src="docs/static/theme/theme-js.js"'), true)
t.assert.strictEqual(res.payload.includes('href="docs/index.css"'), false)
t.assert.strictEqual(res.payload.includes('src="docs/theme/theme-js.js"'), false)
})
test('/docs/ should display index html with correct asset urls when documentation prefix is set', async (t) => {
@@ -660,14 +694,14 @@ test('/docs/ should display index html with correct asset urls when documentatio
url: '/docs/'
})
t.equal(res.payload.includes('href="./static/index.css"'), true)
t.equal(res.payload.includes('src="./static/theme/theme-js.js"'), true)
t.equal(res.payload.includes('href="./index.css"'), false)
t.equal(res.payload.includes('src="./theme/theme-js.js"'), false)
t.assert.strictEqual(res.payload.includes('href="./static/index.css"'), true)
t.assert.strictEqual(res.payload.includes('src="./static/theme/theme-js.js"'), true)
t.assert.strictEqual(res.payload.includes('href="./index.css"'), false)
t.assert.strictEqual(res.payload.includes('src="./theme/theme-js.js"'), false)
})
test('/documentation/ should display index html with correct asset urls', async (t) => {
t.plan(4)
t.plan(13)
const fastify = Fastify()
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] } })
@@ -676,15 +710,15 @@ test('/documentation/ should display index html with correct asset urls', async
method: 'GET',
url: '/documentation/'
})
t.assert.equal(res.statusCode, 200)
t.equal(res.payload.includes('href="./static/index.css"'), true)
t.equal(res.payload.includes('src="./static/theme/theme-js.js"'), true)
t.equal(res.payload.includes('href="./index.css"'), false)
t.equal(res.payload.includes('src="./theme/theme-js.js"'), false)
assertIndexUrls(t, res.payload, '.')
await validateIndexUrls(t, fastify, res.payload, '/documentation')
})
test('/docs should display index html with correct asset urls when documentation prefix is set', async (t) => {
t.plan(4)
t.plan(13)
const fastify = Fastify()
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] }, routePrefix: '/docs' })
@@ -693,11 +727,11 @@ test('/docs should display index html with correct asset urls when documentation
method: 'GET',
url: '/docs'
})
t.assert.equal(res.statusCode, 200)
t.equal(res.payload.includes('href="./docs/static/index.css"'), true)
t.equal(res.payload.includes('src="./docs/static/theme/theme-js.js"'), true)
t.equal(res.payload.includes('href="./docs/index.css"'), false)
t.equal(res.payload.includes('src="./docs/theme/theme-js.js"'), false)
assertIndexUrls(t, res.payload, '/docs')
await validateIndexUrls(t, fastify, res.payload)
})
test('/docs/ should display index html with correct asset urls when documentation prefix is set', async (t) => {
@@ -711,8 +745,26 @@ test('/docs/ should display index html with correct asset urls when documentatio
url: '/docs/'
})
t.equal(res.payload.includes('href="./static/index.css"'), true)
t.equal(res.payload.includes('src="./static/theme/theme-js.js"'), true)
t.equal(res.payload.includes('href="./index.css"'), false)
t.equal(res.payload.includes('src="./theme/theme-js.js"'), false)
t.assert.strictEqual(res.payload.includes('href="./static/index.css"'), true)
t.assert.strictEqual(res.payload.includes('src="./static/theme/theme-js.js"'), true)
t.assert.strictEqual(res.payload.includes('href="./index.css"'), false)
t.assert.strictEqual(res.payload.includes('src="./theme/theme-js.js"'), false)
})
test('should ignore prefix when register plugin', async (t) => {
t.plan(4)
const fastify = Fastify()
await fastify.register(fastifySwagger, swaggerOption)
await fastify.register(fastifySwaggerUi, { routePrefix: '/v1/documentation', prefix: '/' })
fastify.get('/', () => {})
const res = await fastify.inject({
method: 'GET',
url: '/v1/documentation'
})
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.location, undefined)
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual('text/html; charset=utf-8', res.headers['content-type'])
})

View File

@@ -1,128 +1,119 @@
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const serialize = require('../lib/serialize')
test('serialize', async (t) => {
t.plan(8)
test('serialize boolean', t => {
t.plan(2)
t.test('boolean', t => {
t.plan(2)
t.equal(serialize(true), 'true')
t.equal(serialize(false), 'false')
})
t.test('number', t => {
t.plan(7)
t.equal(serialize(0), '0')
t.equal(serialize(1), '1')
t.equal(serialize(1.0), '1')
t.equal(serialize(1.01), '1.01')
t.equal(serialize(Infinity), 'Infinity')
t.equal(serialize(-Infinity), '-Infinity')
t.equal(serialize(NaN), 'NaN')
})
t.test('string', t => {
t.plan(3)
t.equal(serialize('0'), '"0"')
t.equal(serialize('abc'), '"abc"')
t.equal(serialize('"a'), '"\\\"a"') // eslint-disable-line no-useless-escape
})
t.test('bigint', t => {
t.plan(3)
t.equal(serialize(0n), '0n')
t.equal(serialize(1000000000n), '1000000000n')
t.equal(serialize(-9999n), '-9999n')
})
t.test('function', t => {
t.plan(7)
t.equal(serialize(function a () {}), 'function a () {}')
t.equal(serialize(async function a () {}), 'async function a () {}')
t.equal(serialize(() => {}), '() => {}')
t.equal(serialize(async () => {}), 'async () => {}')
t.equal(serialize(() => Date.now), '() => Date.now')
t.equal(serialize(function () {}), 'function () {}')
t.equal(serialize(async function () {}), 'async function () {}')
})
t.test('undefined', t => {
t.plan(1)
t.equal(serialize(undefined), 'undefined')
})
t.test('symbol', t => {
t.plan(2)
t.equal(serialize(Symbol('a')), 'Symbol("a")')
t.equal(serialize(Symbol()), 'Symbol()') // eslint-disable-line symbol-description
})
t.test('object', t => {
t.plan(7)
t.test('null', t => {
t.plan(1)
t.equal(serialize(null), 'null')
})
t.test('RegExp', t => {
t.plan(1)
t.equal(serialize(/0-9/gi), '/0-9/gi')
})
t.test('Date', t => {
t.plan(1)
t.equal(serialize(new Date(0)), 'new Date(0)')
})
t.test('Array', t => {
t.plan(5)
t.equal(serialize([]), '[]')
t.equal(serialize(['a']), '["a"]')
t.equal(serialize([1, 1n, 'a', true]), '[1,1n,"a",true]')
t.equal(serialize([{}]), '[{}]')
t.equal(serialize([{ a: [{}] }]), '[{"a":[{}]}]')
})
t.test('POJO', t => {
t.plan(3)
t.equal(serialize({}), '{}')
t.equal(serialize({ key: 'value' }), '{"key":"value"}')
t.equal(serialize({ null: null, undefined }), '{"null":null,"undefined":undefined}')
})
t.test('Set', t => {
t.plan(3)
t.equal(serialize(new Set()), 'new Set([])')
t.equal(serialize(new Set(['a'])), 'new Set(["a"])')
t.equal(serialize(new Set(['a', {}])), 'new Set(["a",{}])')
})
t.test('Map', t => {
t.plan(3)
t.equal(serialize(new Map()), 'new Map([])')
t.equal(serialize(new Map([['a', 1]])), 'new Map([["a",1]])')
const map = new Map()
map.set('b', 1)
t.equal(serialize(map), 'new Map([["b",1]])')
})
})
t.assert.deepStrictEqual(serialize(true), 'true')
t.assert.deepStrictEqual(serialize(false), 'false')
})
test('serialize number', t => {
t.plan(7)
t.assert.deepStrictEqual(serialize(0), '0')
t.assert.deepStrictEqual(serialize(1), '1')
t.assert.deepStrictEqual(serialize(1.0), '1')
t.assert.deepStrictEqual(serialize(1.01), '1.01')
t.assert.deepStrictEqual(serialize(Infinity), 'Infinity')
t.assert.deepStrictEqual(serialize(-Infinity), '-Infinity')
t.assert.deepStrictEqual(serialize(NaN), 'NaN')
})
test('serialize string', t => {
t.plan(3)
t.assert.deepStrictEqual(serialize('0'), '"0"')
t.assert.deepStrictEqual(serialize('abc'), '"abc"')
t.assert.deepStrictEqual(serialize('"a'), '"\\\"a"') // eslint-disable-line no-useless-escape
})
test('serialize bigint', t => {
t.plan(3)
t.assert.deepStrictEqual(serialize(0n), '0n')
t.assert.deepStrictEqual(serialize(1000000000n), '1000000000n')
t.assert.deepStrictEqual(serialize(-9999n), '-9999n')
})
test('serialize function', t => {
t.plan(7)
t.assert.deepStrictEqual(serialize(function a () { }), 'function a () { }')
t.assert.deepStrictEqual(serialize(async function a () { }), 'async function a () { }')
t.assert.deepStrictEqual(serialize(() => { }), '() => { }')
t.assert.deepStrictEqual(serialize(async () => { }), 'async () => { }')
t.assert.deepStrictEqual(serialize(() => Date.now), '() => Date.now')
t.assert.deepStrictEqual(serialize(function () { }), 'function () { }')
t.assert.deepStrictEqual(serialize(async function () { }), 'async function () { }')
})
test('serialize undefined', t => {
t.plan(1)
t.assert.deepStrictEqual(serialize(undefined), 'undefined')
})
test('serialize symbol', t => {
t.plan(2)
t.assert.deepStrictEqual(serialize(Symbol('a')), 'Symbol("a")')
t.assert.deepStrictEqual(serialize(Symbol()), 'Symbol()') // eslint-disable-line symbol-description
})
test('serialize null', t => {
t.plan(1)
t.assert.deepStrictEqual(serialize(null), 'null')
})
test('serialize RegExp', t => {
t.plan(1)
t.assert.deepStrictEqual(serialize(/0-9/gi), '/0-9/gi')
})
test('serialize Date', t => {
t.plan(1)
t.assert.deepStrictEqual(serialize(new Date(0)), 'new Date(0)')
})
test('serialize Array', t => {
t.plan(5)
t.assert.deepStrictEqual(serialize([]), '[]')
t.assert.deepStrictEqual(serialize(['a']), '["a"]')
t.assert.deepStrictEqual(serialize([1, 1n, 'a', true]), '[1,1n,"a",true]')
t.assert.deepStrictEqual(serialize([{}]), '[{}]')
t.assert.deepStrictEqual(serialize([{ a: [{}] }]), '[{"a":[{}]}]')
})
test('serialize POJO', t => {
t.plan(3)
t.assert.deepStrictEqual(serialize({}), '{}')
t.assert.deepStrictEqual(serialize({ key: 'value' }), '{"key":"value"}')
t.assert.deepStrictEqual(serialize({ null: null, undefined }), '{"null":null,"undefined":undefined}')
})
test('serialize Set', t => {
t.plan(3)
t.assert.deepStrictEqual(serialize(new Set()), 'new Set([])')
t.assert.deepStrictEqual(serialize(new Set(['a'])), 'new Set(["a"])')
t.assert.deepStrictEqual(serialize(new Set(['a', {}])), 'new Set(["a",{}])')
})
test('serialize Map', t => {
t.plan(3)
t.assert.deepStrictEqual(serialize(new Map()), 'new Map([])')
t.assert.deepStrictEqual(serialize(new Map([['a', 1]])), 'new Map([["a",1]])')
const map = new Map()
map.set('b', 1)
t.assert.deepStrictEqual(serialize(map), 'new Map([["b",1]])')
})

View File

@@ -2,7 +2,7 @@
const fs = require('node:fs')
const resolve = require('node:path').resolve
const { test } = require('tap')
const { test } = require('node:test')
const yaml = require('yaml')
const Fastify = require('fastify')
const fastifySwagger = require('@fastify/swagger')
@@ -34,10 +34,10 @@ test('swagger route returns yaml', async (t) => {
url: '/documentation/yaml'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml')
yaml.parse(res.payload)
t.pass('valid swagger yaml')
t.assert.ok(true, 'valid swagger yaml')
})
test('swagger route returns json', async (t) => {
@@ -60,10 +60,10 @@ test('swagger route returns json', async (t) => {
url: '/documentation/json'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
yaml.parse(res.payload)
t.pass('valid swagger json')
t.assert.ok(true, 'valid swagger json')
})
test('postProcessor works, swagger route returns updated yaml', async (t) => {
@@ -90,11 +90,42 @@ test('postProcessor works, swagger route returns updated yaml', async (t) => {
url: '/documentation/yaml'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml')
yaml.parse(res.payload)
t.matchSnapshot(res.payload)
t.pass('valid swagger yaml')
t.assert.strictEqual(res.payload, `openapi: 3.0.0
info:
description: Test swagger specification
version: 1.0.0
title: Test swagger specification
contact:
email: super.developer@gmail.com
servers:
- url: http://localhost:4000/
description: Localhost (uses test data)
paths:
/status:
get:
description: Status route, so we can check if server is alive
tags:
- Status
responses:
"200":
description: Server is alive
content:
application/json:
schema:
type: object
properties:
health:
type: boolean
date:
type: string
example:
health: true
date: 2018-02-19T15:36:46.758Z
`)
t.assert.ok(true, 'valid swagger yaml')
})
test('swagger route returns explicitly passed doc', async (t) => {
@@ -125,8 +156,12 @@ test('swagger route returns explicitly passed doc', async (t) => {
})
const payload = JSON.parse(res.payload)
t.matchSnapshot(JSON.stringify(payload, null, 2))
t.pass('valid explicitly passed spec document')
t.assert.deepStrictEqual(payload, {
message: 'Route GET:/documentation/json not found',
error: 'Not Found',
statusCode: 404
})
t.assert.ok(true, 'valid explicitly passed spec document')
})
test('/documentation/:file should serve static file from the location of main specification file', async (t) => {
@@ -153,7 +188,7 @@ test('/documentation/:file should serve static file from the location of main sp
url: '/documentation/non-existing-file'
})
t.equal(res.statusCode, 404)
t.assert.deepStrictEqual(res.statusCode, 404)
}
{
@@ -162,8 +197,8 @@ test('/documentation/:file should serve static file from the location of main sp
url: '/documentation/example-static-specification.yaml'
})
t.equal(res.statusCode, 200)
t.equal(exampleStaticSpecificationYaml, res.payload)
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(exampleStaticSpecificationYaml, res.payload)
}
{
@@ -172,7 +207,7 @@ test('/documentation/:file should serve static file from the location of main sp
url: '/documentation/dynamic-swagger.js'
})
t.equal(res.statusCode, 200)
t.assert.deepStrictEqual(res.statusCode, 200)
}
})
@@ -189,7 +224,7 @@ test('/documentation/non-existing-file calls custom NotFoundHandler', async (t)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi)
fastify.setNotFoundHandler((request, reply) => {
fastify.setNotFoundHandler((_request, reply) => {
reply.code(410).send()
})
@@ -198,7 +233,7 @@ test('/documentation/non-existing-file calls custom NotFoundHandler', async (t)
url: '/documentation/some-file-that-does-not-exist.js'
})
t.equal(res.statusCode, 410)
t.assert.deepStrictEqual(res.statusCode, 410)
})
test('/documentation/:file should be served from custom location', async (t) => {
@@ -225,8 +260,8 @@ test('/documentation/:file should be served from custom location', async (t) =>
url: '/documentation/oauth2-redirect.html'
})
t.equal(res.statusCode, 200)
t.equal(oauthRedirectHtml, res.payload)
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(oauthRedirectHtml, res.payload)
})
test('/documentation/:file should be served from custom location with trailing slash(es)', async (t) => {
@@ -252,8 +287,8 @@ test('/documentation/:file should be served from custom location with trailing s
url: '/documentation/oauth2-redirect.html'
})
t.equal(res.statusCode, 200)
t.equal(oauthRedirectHtml, res.payload)
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(oauthRedirectHtml, res.payload)
})
test('/documentation/yaml returns cache.swaggerString on second request in static mode', async (t) => {
@@ -276,10 +311,10 @@ test('/documentation/yaml returns cache.swaggerString on second request in stati
url: '/documentation/yaml'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml')
yaml.parse(res.payload)
t.pass('valid swagger yaml')
t.assert.ok(true, 'valid swagger yaml')
}
{
@@ -288,10 +323,10 @@ test('/documentation/yaml returns cache.swaggerString on second request in stati
url: '/documentation/yaml'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml')
yaml.parse(res.payload)
t.pass('valid swagger yaml')
t.assert.ok(true, 'valid swagger yaml')
}
})
@@ -315,9 +350,9 @@ test('/documentation/json returns cache.swaggerObject on second request in stati
url: '/documentation/json'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
t.assert.ok(true, 'valid swagger json')
}
{
@@ -326,9 +361,9 @@ test('/documentation/json returns cache.swaggerObject on second request in stati
url: '/documentation/json'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
t.assert.ok(true, 'valid swagger json')
}
})
@@ -351,10 +386,10 @@ test('/documentation/yaml returns cache.swaggerString on second request in dynam
url: '/documentation/yaml'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml')
yaml.parse(res.payload)
t.pass('valid swagger yaml')
t.assert.ok(true, 'valid swagger yaml')
}
{
@@ -363,10 +398,10 @@ test('/documentation/yaml returns cache.swaggerString on second request in dynam
url: '/documentation/yaml'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/x-yaml')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/x-yaml')
yaml.parse(res.payload)
t.pass('valid swagger yaml')
t.assert.ok(true, 'valid swagger yaml')
}
})
@@ -389,9 +424,9 @@ test('/documentation/json returns cache.swaggerObject on second request in dynam
url: '/documentation/json'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
t.assert.ok(true, 'valid swagger json')
}
{
@@ -400,8 +435,8 @@ test('/documentation/json returns cache.swaggerObject on second request in dynam
url: '/documentation/json'
})
t.equal(typeof res.payload, 'string')
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.pass('valid swagger json')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
t.assert.ok(true, 'valid swagger json')
}
})

View File

@@ -1,6 +1,6 @@
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifySwagger = require('@fastify/swagger')
const fastifySwaggerUi = require('../index')
@@ -17,8 +17,8 @@ test('/documentation/static/swagger-initializer.js should have default uiConfig'
url: '/documentation/static/swagger-initializer.js'
})
t.equal(res.statusCode, 200)
t.ok(res.payload.includes('const config = {}'))
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.ok(res.payload.includes('const config = {}'))
})
test('/documentation/static/swagger-initializer.js should have configurable uiConfig', async (t) => {
@@ -37,8 +37,8 @@ test('/documentation/static/swagger-initializer.js should have configurable uiCo
url: '/documentation/static/swagger-initializer.js'
})
t.equal(res.statusCode, 200)
t.ok(res.payload.includes("const config = {\"onComplete\":() => { alert('test') }}"))
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.ok(res.payload.includes("const config = {\"onComplete\":() => { alert('test') }}"))
})
test('/documentation/static/swagger-initializer.js should have default initOAuth', async (t) => {
@@ -53,8 +53,8 @@ test('/documentation/static/swagger-initializer.js should have default initOAuth
url: '/documentation/static/swagger-initializer.js'
})
t.equal(res.statusCode, 200)
t.ok(res.payload.includes('ui.initOAuth({})'))
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.ok(res.payload.includes('ui.initOAuth({})'))
})
test('/documentation/static/swagger-initializer.js should have configurable initOAuth', async (t) => {
@@ -73,8 +73,8 @@ test('/documentation/static/swagger-initializer.js should have configurable init
url: '/documentation/static/swagger-initializer.js'
})
t.equal(res.statusCode, 200)
t.ok(res.payload.includes('ui.initOAuth({"clientId":"someId"})'))
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.ok(res.payload.includes('ui.initOAuth({"clientId":"someId"})'))
})
test('customize logo', async (t) => {
@@ -90,5 +90,54 @@ test('customize logo', async (t) => {
await fastify.register(fastifySwaggerUi, { logo: { type: 'image/png', content: 'foobar' } })
const res = await fastify.inject('/documentation/static/swagger-initializer.js')
t.equal(res.body.indexOf(Buffer.from('foobar').toString('base64')) > -1, true)
t.assert.deepStrictEqual(res.body.includes('// Replace the logo'), true)
t.assert.deepStrictEqual(res.body.indexOf(Buffer.from('foobar').toString('base64')) > -1, true)
})
test('customized logo has target', async (t) => {
const config = {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml'
}
}
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi, { logo: { type: 'image/png', content: 'foobar', target: '_self' } })
const res = await fastify.inject('/documentation/static/swagger-initializer.js')
t.assert.deepStrictEqual(res.body.includes("img.target = '_self'"), true)
})
test('customized logo has href', async (t) => {
const config = {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml'
}
}
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi, { logo: { type: 'image/png', content: 'foobar', href: 'http://www.example.com' } })
const res = await fastify.inject('/documentation/static/swagger-initializer.js')
t.assert.deepStrictEqual(res.body.includes("img.href = 'http://www.example.com'"), true)
})
test('no customized logo', async (t) => {
const config = {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml'
}
}
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi, { logo: null })
const res = await fastify.inject('/documentation/static/swagger-initializer.js')
t.assert.deepStrictEqual(res.body.includes('// Replace the logo'), false)
})

View File

@@ -1,6 +1,6 @@
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifySwagger = require('@fastify/swagger')
const fastifySwaggerUi = require('../index')
@@ -23,11 +23,11 @@ test('swagger route does not return additional theme', async (t) => {
url: '/documentation'
})
t.equal(typeof res.payload, 'string')
t.notMatch(res.payload, /theme\/special\.js/)
t.notMatch(res.payload, /theme\/favicon\.png/)
t.notMatch(res.payload, /theme\/theme\.css/)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.deepStrictEqual(/theme\/special\.js/.test(res.payload), false)
t.assert.deepStrictEqual(/theme\/favicon\.png/.test(res.payload), false)
t.assert.deepStrictEqual(/theme\/theme\.css/.test(res.payload), false)
t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8')
})
test('swagger route returns additional theme', async (t) => {
@@ -66,18 +66,18 @@ test('swagger route returns additional theme', async (t) => {
url: '/documentation'
})
t.equal(typeof res.payload, 'string')
t.match(res.payload, /theme\/special\.js/)
t.match(res.payload, /theme\/favicon\.png/)
t.match(res.payload, /theme\/theme\.css/)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.match(res.payload, /theme\/special\.js/)
t.assert.match(res.payload, /theme\/favicon\.png/)
t.assert.match(res.payload, /theme\/theme\.css/)
t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8')
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/theme/special.js'
})
t.equal(res.payload, 'alert("loaded test-theme")')
t.assert.deepStrictEqual(res.payload, 'alert("loaded test-theme")')
}
{
@@ -85,8 +85,8 @@ test('swagger route returns additional theme', async (t) => {
method: 'GET',
url: '/documentation/static/theme/favicon.png'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'image/png')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-type'], 'image/png')
}
{
@@ -94,7 +94,7 @@ test('swagger route returns additional theme', async (t) => {
method: 'GET',
url: '/documentation/static/theme/theme.css'
})
t.equal(res.payload, '* {border: 1px red solid;}')
t.assert.deepStrictEqual(res.payload, '* {border: 1px red solid;}')
}
})
@@ -122,16 +122,16 @@ test('swagger route returns additional theme - only js', async (t) => {
url: '/documentation'
})
t.equal(typeof res.payload, 'string')
t.match(res.payload, /theme\/special\.js/)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.match(res.payload, /theme\/special\.js/)
t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8')
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/theme/special.js'
})
t.equal(res.payload, 'alert("loaded test-theme")')
t.assert.deepStrictEqual(res.payload, 'alert("loaded test-theme")')
}
})
@@ -159,16 +159,16 @@ test('swagger route returns additional theme - only css', async (t) => {
url: '/documentation'
})
t.equal(typeof res.payload, 'string')
t.match(res.payload, /theme\/theme\.css/)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.match(res.payload, /theme\/theme\.css/)
t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8')
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/theme/theme.css'
})
t.equal(res.payload, '* {border: 1px red solid;}')
t.assert.deepStrictEqual(res.payload, '* {border: 1px red solid;}')
}
})
@@ -202,17 +202,17 @@ test('swagger route returns additional theme - only favicon', async (t) => {
url: '/documentation'
})
t.equal(typeof res.payload, 'string')
t.match(res.payload, /theme\/favicon\.png/)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.match(res.payload, /theme\/favicon\.png/)
t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8')
{
const res = await fastify.inject({
method: 'GET',
url: '/documentation/static/theme/favicon.png'
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'image/png')
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers['content-type'], 'image/png')
}
})
@@ -238,7 +238,7 @@ test('swagger route returns additional theme - only title', async (t) => {
url: '/documentation'
})
t.equal(typeof res.payload, 'string')
t.match(res.payload, /<title>My custom title<\/title>/)
t.equal(res.headers['content-type'], 'text/html; charset=utf-8')
t.assert.deepStrictEqual(typeof res.payload, 'string')
t.assert.match(res.payload, /<title>My custom title<\/title>/)
t.assert.deepStrictEqual(res.headers['content-type'], 'text/html; charset=utf-8')
})

View File

@@ -1,6 +1,6 @@
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifySwagger = require('@fastify/swagger')
const fastifySwaggerUi = require('../index')
@@ -52,9 +52,9 @@ test('transformSpecification should modify the json', async (t) => {
})
await fastify.register(fastifySwaggerUi, {
transformSpecification: function (swaggerObject, req, reply) {
t.not(swaggerObject, fastify.swagger())
t.ok(req)
t.ok(reply)
t.assert.notEqual(swaggerObject, fastify.swagger())
t.assert.ok(req)
t.assert.ok(reply)
swaggerObject.swagger = '2.1'
return swaggerObject
}
@@ -67,8 +67,8 @@ test('transformSpecification should modify the json', async (t) => {
url: '/documentation/json'
})
t.notSame(fastify.swagger(), JSON.parse(res.body))
t.equal(JSON.parse(res.body).swagger, '2.1')
t.assert.notDeepEqual(fastify.swagger(), JSON.parse(res.body))
t.assert.strictEqual(JSON.parse(res.body).swagger, '2.1')
})
test('transformSpecificationClone false should not deepclone fastify.swagger() /1', async (t) => {
@@ -118,9 +118,9 @@ test('transformSpecificationClone false should not deepclone fastify.swagger() /
await fastify.register(fastifySwaggerUi, {
transformSpecificationClone: false,
transformSpecification: function (swaggerObject, req, reply) {
t.equal(swaggerObject, fastify.swagger())
t.ok(req)
t.ok(reply)
t.assert.deepStrictEqual(swaggerObject, fastify.swagger())
t.assert.ok(req)
t.assert.ok(reply)
return swaggerObject
}
})
@@ -132,10 +132,7 @@ test('transformSpecificationClone false should not deepclone fastify.swagger() /
url: '/documentation/json'
})
const swagger = fastify.swagger()
Object.getOwnPropertySymbols(swagger.definitions.User).forEach((symbol) => delete swagger.definitions.User[symbol])
t.strictSame(swagger, JSON.parse(res.body))
t.assert.deepStrictEqual(fastify.swagger(), JSON.parse(res.body))
})
test('transformSpecification should modify the yaml', async (t) => {
@@ -185,8 +182,8 @@ test('transformSpecification should modify the yaml', async (t) => {
await fastify.register(fastifySwaggerUi, {
transformSpecification: function (swaggerObject, req, reply) {
swaggerObject.swagger = '2.1'
t.ok(req)
t.ok(reply)
t.assert.ok(req)
t.assert.ok(reply)
return swaggerObject
}
})
@@ -199,8 +196,8 @@ test('transformSpecification should modify the yaml', async (t) => {
url: '/documentation/yaml'
})
t.same(fastify.swagger(), swaggerPre)
t.equal(yaml.parse(res.body).swagger, '2.1')
t.assert.deepStrictEqual(fastify.swagger(), swaggerPre)
t.assert.deepStrictEqual(yaml.parse(res.body).swagger, '2.1')
})
test('transformSpecificationClone false should not deepclone fastify.swagger() /2', async (t) => {
@@ -250,9 +247,9 @@ test('transformSpecificationClone false should not deepclone fastify.swagger() /
await fastify.register(fastifySwaggerUi, {
transformSpecificationClone: false,
transformSpecification: function (swaggerObject, req, reply) {
t.equal(swaggerObject, fastify.swagger())
t.ok(req)
t.ok(reply)
t.assert.deepStrictEqual(swaggerObject, fastify.swagger())
t.assert.ok(req)
t.assert.ok(reply)
return swaggerObject
}
})
@@ -265,5 +262,5 @@ test('transformSpecificationClone false should not deepclone fastify.swagger() /
url: '/documentation/yaml'
})
t.same(fastify.swagger(), swaggerPre)
t.assert.deepStrictEqual(fastify.swagger(), swaggerPre)
})